Data Types in C :
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.
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;
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.
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
Syntax:
char variable_name;
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
Syntax:
float variable_name;
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
Syntax:
double variable_name;
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.
Syntax:
void functionName(parameters) {
// Function code
}
Size of Data Types in C:
The size of the data types in C is dependent on the size of the architecture, so we cannot define the
universal size of the data types. For that, the C language provides the sizeof() operator to check the
size of the data types.
Different data types also have different ranges up to which can vary from compiler to compiler.
Below is a list of ranges along with the memory requirement and format specifiers on the 64-bit GCC
compiler.
Format Specifier
Data Type Size (bytes) Range
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long
8 0 to 18,446,744,073,709,551,615 %llu
int
Format Specifier
Data Type Size (bytes) Range
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf
Literals in C:
In C, literals are constant values assigned to variables. They represent fixed values that cannot be
changed. Literals occupy memory but do not have references like variables. Often, the terms
constants and literals are used interchangeably.
Type Conversion:
In C, type conversion is the process of changing one data type into another. This can happen
automatically by the compiler or manually by the programmer. Type conversion is only performed
between data types where such a conversion is possible.
Data Type Modifiers in C:
In C, data type modifiers are the keywords used to modify the original sign or length/range of values
that various primitive data types hold such as int, char, and double.
In C, we have 4 data type modifiers:
short Modifier:
The short modifier in C works with integer data type. It decreases the size of the int to 2 bytes along
with the range of values int type can store.
Explanation: We initialized a normal integer 'a' and short integer 'b'. After that, we have printed the
size of both the variables and we can clearly see in the output that variable declared with data
modifier with 'short' became 2 bytes
long Modifier:
The long modifier in C is used to increase the original size of an int or double data type by two times.
As the consequence, the range of values these data types can store is also increased. For example, if
int take 4 bytes of space it will take 8 bytes of space after using long with int.
Explanation: We have declared the variables of integer and double type with and without using long
modifier and then printed the size of each variable. In the output, we can see that the size of data
type with long modifier becomes double.
unsigned Modifier:
By default, we can store positive and negative values in integer data type but many times, we don't
need to store the negatives values leading to the waste of memory space. We can utilize this space
to store positive values using unsigned data type modifier.
The unsigned modifier shifts the data type range to the positive part of the whole numbers. For
example, if the default range of int is -2,147,483,648 to 2,147,483,647, then after using unsigned
with int, the range became 0 to 4,294,967,295.
Unsigned modifier can also be combined with long and short modifiers to create unsigned long,
unsigned short, etc.
signed Modifier:
It is default modifier of int and char data type if no modifier is specified. It indicates that we can
store both negative and positive values and the range is equally divided to the positive and negative
values.
Size and Range of Data Types with Modifiers:
The below table lists the size and the range of data type (in 64-bit compiler) that is changed with the
help of modifiers:
Size
Data Type Modifiers (bytes) Range
signed 1 -128 to 127
char
unsigned
1 0 to 255
(default)
signed
2 -32,768 to 32,767
(default)
short int
unsigned 2 0 to 65,535
signed
4 -2,147,483,648 to 2,147,483,647
(default)
int
unsigned 4 0 to 4,294,967,295
Size
Data Type Modifiers (bytes) Range
signed -9,223,372,036,854,775,808 to
8
(default) 9,223,372,036,854,775,807
long int
unsigned 8 0 to 18,446,744,073,709,551,615
signed -9,223,372,036,854,775,808 to
8
(default) 9,223,372,036,854,775,807
long long
int
unsigned 8 0 to 18,446,744,073,709,551,615
double None 8 ~1.7E-308 to 1.7E+308
long Higher precision, range varies depending on
None 16
double implementation