0% found this document useful (0 votes)
18 views8 pages

C Programming Data Types Explained

The document provides an overview of data types in C programming, detailing basic (primitive) types such as int, char, float, double, and void, along with their sizes, ranges, and examples. It also covers derived data types like arrays and pointers, as well as user-defined types including enums and structs. The document emphasizes that C is a statically typed language, requiring explicit type declarations for variables.

Uploaded by

jeevanjo2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

C Programming Data Types Explained

The document provides an overview of data types in C programming, detailing basic (primitive) types such as int, char, float, double, and void, along with their sizes, ranges, and examples. It also covers derived data types like arrays and pointers, as well as user-defined types including enums and structs. The document emphasizes that C is a statically typed language, requiring explicit type declarations for variables.

Uploaded by

jeevanjo2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA TYPES

Each variable in C has an associated data type.


It specifies the type of data that the variable can store like integer, character, floating,
double, etc.

Example:
int number;

The above statement declares a variable with name number that can
store integer values.

C is a statically type language where each variable's type must be specified at the
declaration and once specified, it cannot be changed.

Basic (Primitive) Data Type:


Integer Data Type
The integer datatype in C is used to store the integer numbers (any number including
positive, negative and zero without decimal part). Octal values, hexadecimal values,
and decimal values can also be stored in int data type in C.

• Range: -2,147,483,648 to 2,147,483,647


• Size: 4 bytes
• Format Specifier: %d

Format specifiers are the symbols that are used for printing and scanning values of
given data types.

Example:
We use int keyword to declare the integer variable:
int val;

#include<stdio.h>
int main()
{
int var = 22;

printf("%d", var);
return 0;
}

Output
22

A variable of given data type can only contains the values of the same type.
So, var can only store numbers, not text or anything else.

The integer data type can also be used as:


1. unsigned int: It can store the data values from zero to positive numbers, but it can’t
store negative values

2. short int: It is lesser in size than the int by 2 bytes so can only store values from -
32,768 to 32,767.

3. long int: Larger version of the int datatype so can store values greater than int.

4. unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler dependent. We can use sizeof operator
to check the actual size of any data type. In this article, we are discussing the sizes
according to 64-bit compilers.

Character Data Type


• Character data type allows its variable to store only a single character.
• The size of the character is 1 byte.
• It is the most basic data type in C.
• It stores a single character and requires a single byte of memory in almost all
compilers.

• Range: (-128 to 127) or (0 to 255)


• Size: 1 byte
• Format Specifier: %c

#include<stdio.h>
int main()
{
char ch = 'A';

printf("%c", ch);
return 0;
}

Output
A

Float Data Type


In C programming, float data type is used to store single precision floating-point values.
These values are decimal and exponential numbers.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f

#include <stdio.h>

int main()
{
float val = 12.45;
printf("%f", val);
return 0;
}

Output
12.450000

Double Data Type


The double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision.

It can easily accommodate about 16 to 17 digits after or before a decimal point.

• Range: 1.7E-308 to 1.7E+308


• Size: 8 bytes
• Format Specifier: %lf

#include <stdio.h>
int main()
{
double val = 1.4521;

printf("%lf", val);
return 0;
}

Output
val = 1.452100

Void Data Type


The void data type in C is used to indicate the absence of a value.

Variables of void data type are not allowed.

It can only be used for pointers and function return type and parameters.
Example:
void fun(int a, int b)
{
// function body
}
where function fun is a void type of function means it doesn't return any value.

Summary:
Basic (Primitive) Data Types

These are the fundamental types used to represent simple values.

Data Memory
Description Example
Type (Typical)

Integer numbers (whole numbers,


int 2 or 4 bytes int age = 20;
no decimal)

Single character (stored as ASCII


char 1 byte char grade = 'A';
value)

Single-precision decimal (6–7


float 4 bytes float pi = 3.14;
digits)

Double-precision decimal (15–16 double value =


double 8 bytes
digits) 3.1415926535;

void Represents no value / empty 0 bytes Used in functions

Derived Data Types

These are built from basic types.

• Arrays → collection of elements of same type.

Example: int marks[5];

• Pointers → It is a variable used to store memory addresses.


Example: int *ptr;

• Functions → It is a block of code that can perform some specific task.

Example: int sum(int a, int b);

User-defined Data types

Enumeration (enum)

User-defined type consisting of named integer constants.

enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};

• SUN = 0
• MON = 1
• TUE = 2
• WED = 3
• THU = 4
• FRI = 5
• SAT = 6

enum n { a = 1, b, c = -5, z, d = 7, e, f = 9 };

a = 1 (explicit assignment).

b → 2 (auto-increment from a).

c = -5 (explicit assignment).

z → -4 (auto-increment from c).

d = 7 (explicit assignment)

e → 8 (auto-increment from d).


f = 9 (explicit assignment).

enum names must be unique in their scope.


#include <stdio.h>
// Defining enum
enum direction { EAST, NORTH, WEST, SOUTH };
int main()
{
// Creating enum variable
enum direction dir = NORTH;
printf("%d\n", dir);
}

Output
1

User-Defined Data Types

C allows you to create your own data types using:

• struct → group of different data types.

struct Student
{
int id;
char name[20];
float marks;
};

• union → similar to struct but shares memory space.


Data Type Size (in bytes) Range
char 1 byte -128 to 127 (signed char) 0 to 255 (unsigned char)
bool (_Bool) 1 byte 0 (false) or 1 (true)
short int 2 bytes -32,768 to 32,767
unsigned
2 bytes 0 to 65,535
short
int (signed
4 bytes -2,147,483,648 to 2,147,483,647
int)
unsigned int 4 bytes 0 to 4,294,967,295
-9,223,372,036,854,775,808 to
long int 8 bytes
9,223,372,036,854,775,807
unsigned
8 bytes 0 to 18,446,744,073,709,551,615
long
float 4 bytes ~6–7 decimal digits precision
double 8 bytes ~15–16 decimal digits precision
16 bytes (on GCC for
long double ~18–19 decimal digits precision
x86-64)

You might also like