Day 1: Data types in C Programming language
C has a concept of 'data types' which are used to define a variable before its use. A C programmer use appropriate data type as per his requirement. C supports various data types such as character, integer and float etc. All the data types are made up of units of memory called bytes. n order of size, starting with the smallest, the integer types are char, short, int, long. The smaller types take less memory, the larger types takes more space in memory.
Integer Data Type
Integers are whole numbers with a range of values, Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32768. To control the range of numbers and storage space, C has three classes of integer storage namely short int, int and long int. All three data types have signed and unsigned forms. Integer Data Types are denoted by int.
int <variable name> /* Syntax */
int num1;// Range of short int is -32768 to -32767
short int num2;// Range of short int is -128 to 127
long int num3;// Range of long integer is -2147483648 to 2147483647
signed int num4;// Range of signed int is -32768 to 32767
unsigned int num5;// Range of unsigned int is 0 - 65535.
unsigned long int num6;// Range of unsigned int is 0 to 4294967295.
Memory occupied by Integer
- short int (1 Byte = 8 Bits)
- int or signed int (2 Bytes = 16 Bits )
- long int or signed long int (4 Bytes = 32 Bits)
Data type | Keyword Equivalent |
Signed Integer | signed int (or) int |
Signed Short Integer | signed short int (or) short int (or) short |
Signed Long Integer | signed long int (or) long int (or) long |
Unsigned Integer | unsigned int (or) unsigned |
Unsigned Short Integer | unsigned short int (or) unsigned short |
Unsigned Long Integer | unsigned long int (or) unsigned long |
Floating data types
The float data type is used to store fractional numbers (real numbers). Floating point numbers are denoted by the keyword float. The double is same as float but it takes double space (8 bytes) than float.
float <variable name> /* Syntax */
float num1;// Range of float is 3.4 e-38 to 3.4 e+38
double int num2;// Range of double is 1.7e-308 to 1.7e+308
long double int num3;// Range of long double is 3.4 e-4932 to 3.4 e+4932
Memory occupied by Float
- float (4 Bytes = 32 Bits)
- double (8 Bytes = 64 Bits)
- long double (10 Bytes = 80 Bits)
Data type | Keyword Equivalent |
Floating Point | float |
Double Precision Floating Point | double |
Extended Double Precision Floating Point | long double |
Character data types
Character type variable can hold a single character. As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters have values between 0 and 255, signed characters have values from –128 to 127. Character is declared by char.
char <variable name> /* Syntax */
char ch; // Range of char is -128 to 127
unsigned char name; // Range of unsigned char float is 0 to 255
Memory occupied by char
- char or unsigned char (1 Byte = 8 Bits)
Data type | Keyword Equivalent |
Character | char |
Unsigned Character | unsigned char |
Some of the data types are with their range and how much space they required
Data Types |
Range |
Bytes |
Format |
signed char | -128 to + 127 | 1 | %c |
unsigned char | 0 to 255 | 1 | %c |
short signed int | -32768 to +32767 | 2 | %d |
short unsigned int | 0 to 65535 | 2 | %u |
signed int | -32768 to +32767 | 2 | %d |
unsigned int | 0 to 65535 | 2 | %u |
long signed int | -2147483648 to +2147483647 | 4 | %ld |
long unsigned int | 0 to 4294967295 | 4 | %lu |
float | -3.4e38 to +3.4e38 | 4 | %f |
double | -1.7e308 to +1.7e308 | 8 | %lf |
long double | -1.7e4932 to +1.7e4932 | 10 | %lf |