Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:
Following are the examples of some very common data types used in C:
- char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
- int: As the name suggests, an int variable is used to store an integer.
- float: It is used to store decimal numbers (numbers with floating point value) with single precision.
- double: It is used to store decimal numbers (numbers with floating point value) with double precision.
Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.
|Data Type||Memory (bytes)||Range||Format Specifier|
|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 int||8||0 to 18,446,744,073,709,551,615||%llu|
|signed char||1||-128 to 127||%c|
|unsigned char||1||0 to 255||%c|
We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:
Hello World! Hello! I am a character. My value is G and my size is 1 byte. Hello! I am an integer. My value is 1 and my size is 4 bytes. Hello! I am a double floating point variable. My value is 3.140000 and my size i s 8 bytes. Bye! See you soon. :)
This article is contributed by Ayush Jaggi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Difference between fundamental data types and derived data types
- C++ Data Types
- C | Data Types | Question 2
- C | Data Types | Question 1
- C | Data Types | Question 4
- C | Data Types | Question 5
- C | Data Types | Question 9
- C | Data Types | Question 8
- C | Data Types | Question 7
- C | Data Types | Question 6
- What are the data types for which it is not possible to create an array?
- Uninitialized primitive data types in C/C++
- Interesting facts about data-types and modifiers in C/C++
- Linking Files having same variables with different data types in C
- What happen when we exceed valid range of built-in data types in C++?