Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These data types have different storage capacities.
C language supports 2 different types of data types:
Primary data types(primitive):
These are fundamental data types in C namely integer(int), floating-point(float), character(char) and void.
Derived data types:
Derived data types are nothing but primary data types but a little twisted or grouped like array, structure, union and pointer. These are discussed in detail later.
Here we will study primary or primitive datatype
Primary data types:
- Char: The most basic data type in C. It stores a single character and requires a single byte (8bits) memory in almost all compilers. It can store a range
- Int: As the name suggests, an int variable is used to store an integer.
The regular integer that we use has 2 bytes size (16 bits) on a 16-bit machine. However, most the modern systems have 32 or 64-bit configurations. The size of an integer in such an environment is 4 bytes.
- Float: It is used to store decimal numbers (numbers with floating-point value) with single precision. Its size is 4bytes irrespective of 32 or 64 bit.
- Double: It is used to store decimal numbers (numbers with floating-point value) with double precision. Its size is 8bytes irrespective of 32 or 64 bit.
Different data types also have different ranges up to which they can store numbers. These ranges may vary from compiler to compiler. Below is the list of ranges, with the memory requirement and format specifiers on the 32-bit GCC compiler.
DATATYPE: Int
TYPE OF DATA: Integer
MEMORY SIZE: 2 Bytes
RANGE: -32768 to 32767
DATATYPE: Char
TYPE OF DATA: Character
MEMORY SIZE: 1 Byte
RANGE: -128 to 127
DATATYPE: Float
TYPE OF DATA: Floating point number
MEMORY SIZE: 4 Bytes
RANGE: 3.4e-38 to 3.4e+38
DATATYPE: Double
TYPE OF DATA: Floating point number with higher precision
MEMORY SIZE: 8 Bytes
RANGE: 1.7e-308 to 1.7e+308
Program to check size of an datatypes:
#include <stdio.h>
int main()
{
int a = 1;
char b =’G’;
float f = 7.77;
double c = 3.14;
printf(“Hello World!\n”);
printf(“I am a character. My value is %c and “
“my size is %lu byte.\n”, b,sizeof(char));
printf(“I am an integer. My value is %d and “
“my size is %lu bytes.\n”, a,sizeof(int));
printf(“I am a float with single floating point variable.”” My value is %f and my size is %lu bytes.\n”,f,sizeof(float));
printf(“I am a double floating point variable.”” My value is %lf and my size is %lu bytes.\n”,c,sizeof(double));
printf(“Bye! See you soon. :)\n”);
return 0;
}
The output of the program:
Hello World!
I am a character. My value is G, and my size is 1 byte.
I am an integer. My value is 1, and my size is 4 bytes.
I am a float with a single floating point variable. My value is 7.770000, and my size is 4 bytes.
I am a double floating-point variable. My value is 3.140000, and my size is 8 bytes.
Bye! See you soon. 🙂