Back to: C Programming for Beginners
In this lesson, we will talk about various Data Types in C language both predefined as well as derived. We will also go over their memory requirements for storing data.
What are Data Types in C Language?
Data types in C actually specify how we enter data into our programs and their type. C has some predefined data types with various storage capacities. C also has the option of derived data types: the amalgamation of predefined data types into a new data type per user needs . To summarize, data types in C language can be classified into three broad types:
- Primary or Fundamental Data Types: These are arithmetic and character-based data types predefined in the language.
- Void Data Type: This data type specifies that there is no value available.
- Derived Data Type: These are primary data types grouped as per the user’s need.
Primary Data Types in C Language
As mentioned above Primary data types in C are arithmetic and character-based datatypes predefined in the language itself. C supports four fundamental data types:
- Integer (
- Floating Point(
As the name suggests this data type is used to store integer values (whole numbers). It can have zero, positive as well as negative values but no decimal numbers. For example,
int a = 0;
int b = -2, c = 10, d = 15;
We can use the keyword
int for declaring an integer variable as shown in the above code snippet. Multiple variables of the same data types in C can be declared together. Here,
d are all declared as integers. Unlike more modern languages such as Java, C# and more; the size of a variable in C is not fixed and depends on the operating system. In an old 16 bit machine, the size is 2 bytes whereas, in a newer 32/64 bit machine, the size is 4 bytes. This means it can store
232 distinct states from
char is one of the primary data types in C and it is used to store single characters like
'Z'. The size of the character variable is 1 byte and it can only store one character at a time.
char a = 'a';
char b = 'b', z = 'Z';
Floating Point Type
In C, floating-point numbers are stored using the
float keyword. The size of this data type 4 bytes.
float number = 4.034;
The double data type is also used to store floating-point numbers using the
double keyword. However, its size is 8 bytes.
double value = 4.0434433;
Float Vs Double
Both float and double data types are used to declare and store floating-point numbers. The only difference between them is precision. Float (single-precision float data type) provides 6 digits of precision while double (double-precision float data type) provides 14 digits of precision. This simply means that the double data type can store more digits to the right of the decimal point than the float type.
The C language has provision for something called Type Qualifiers. These can be applied to the Primary data types to get few more customized data types. The reason for this provision is that it allows the programmer to precisely choose a range of numbers according to the need. This, in turn, makes the program more efficient. There are two types of qualifiers as mentioned below:
- Size Qualifiers –
- Sign Qualifiers –
These qualifiers are used to modify the range of some primary data types as per the programmer’s need. When the
short qualifier is used, the range of the data type is reduced while the
long qualifier increases the range of the data type. We can use both the qualifiers on
int datatype whereas only a long qualifier can be used with the double datatype.
These qualifiers are used to decide if a data type will contain only positive numbers or both positive and negative numbers. When the
unsigned qualifier is used on a data type then the numbers stored in it are always positive. With
signed qualifiers, the numbers can be both positive and negative. If the qualifier is not mentioned then by default signed qualifier is assumed. The range of data types with unsigned qualifiers is larger than the same data type with signed qualifiers. This becomes useful when the programmer knows in advance that the numbers will always be positive and gets more range in return.
As mentioned above the size of data types in C is not fixed and it depends on the machine being used. The below table shows the size and range of various data types and their qualifiers on a 32/64 bit machine.
|Data Type||Qualifier||Size in Byte||Range|
You can run the code below to determine the range and size of various data types and their qualifiers on your system.
Program to determine size and range of data types
Project: Data Types in C
Author: the Bored Engineer
printf("%30s %12s %28s\n", "", "Size", "Range");
printf("%-30s %10lu %25d - %d\n", "char or signed char", sizeof(char), CHAR_MIN, CHAR_MAX);
printf("%-30s %10lu %25d - %d\n", "unsigned char", sizeof(unsigned char), 0, UCHAR_MAX);
printf("%-30s %10lu %25d - %d\n", "int or signed int", sizeof(int), INT_MIN, INT_MAX);
printf("%-30s %10lu %25d - %ud\n", "unsigned int", sizeof(unsigned int), 0, UINT_MAX);
printf("%-30s %10lu %25hd - %hd\n", "short int or short signed int", sizeof(short int), SHRT_MIN, SHRT_MAX);
printf("%-30s %10lu %25d - %d\n", "unsigned short int", sizeof(unsigned short int), 0, USHRT_MAX);
printf("%-30s %10lu %25ld - %ld\n", "long int or signed long int", sizeof(long int), LONG_MIN, LONG_MAX);
printf("%-30s %10lu %25d - %lu\n", "unsigned long int", sizeof(unsigned long int), 0, ULONG_MAX);
printf("%-30s %10lu %25le - %le\n", "float", sizeof(float), FLT_MIN, FLT_MAX);
printf("%-30s %10lu %25le - %le\n", "double", sizeof(double), DBL_MIN, DBL_MAX);
printf("%-30s %10lu %25Le - %Le\n", "long double", sizeof(long double), LDBL_MIN, LDBL_MAX);
return 0; // return 0 to operating system
char or signed char 1 -128 - 127
unsigned char 1 0 - 255
int or signed int 4 -2147483648 - 2147483647
unsigned int 4 0 - 4294967295d
short int or short signed int 2 -32768 - 32767
unsigned short int 2 0 - 65535
long int or signed long int 8 -9223372036854775808 - 9223372036854775807
unsigned long int 8 0 - 18446744073709551615
float 4 1.175494e-38 - 3.402823e+38
double 8 2.225074e-308 - 1.797693e+308
long double 16 3.362103e-4932 - 1.189731e+4932
...Program finished with exit code 0
Void Data Type
Void is an incomplete data type denoted by the keyword
void. It means no-type or nothing. It is used when we want to represent no-value. For example, when a function is not returning any value then its return type should be
Derived Data Types in C Language
These data types in C are derived from the primary data types by mixing them in various ways. For example arrays, pointers, structures, unions and more are all derived data types. We will learn about them in detail individually in later lessons.
Best C Programming Books
List of C Programming books curated for beginners as well as experienced programmers.
Sharing is Caring
If you liked this post, then feel free to share it with your loved ones!