3.2 – Data types in C language

by subbu on August 26, 2013

Why data types?

As we learned in the previous session memory is organized in number of bytes, each byte is again divided into number of bits. We also know that any type of data is first translated into its equal binary and then stored into the memory as 1’s and 0’s. The maximum value that a variable can store is depends on the size of variable. Say for example if a variable takes 3 bits of memory then the maximum value that it can store is 111 (23 -1 or 7), 4 bits can store the maximum value of 1111 (24 -1 or 15), 8 bits can store the maximum value of 11111111 (28 -1 or 255) and n bits can store the maximum value of 2n -1.

So the maximum value that a variable can store is depends on the size of variable. Bigger variables can store larger numbers and smaller variables can store smaller numbers. Different data types (keywords) are used to declare variable of different sizes to store the values of different ranges. Now we will see different available data types and their sizes in C language.

Data types in C language:

C language is rich in data types, provides number of data types to allocate memory of different types of different ranges. Proper data type is selected according to the value that we need to store. Say for example data type that takes 1 byte is enough to store a year and a data type that takes 4 bytes is needed to store a cell phone number. Total data types in C language are classified into

  • Primary data types.
  • Derived data types.
  • User defined types.

data types in C language

sizeof() operator in C language:

Size of a data type is compiler dependent and the range of data type is size dependent. Size of data types in Turbo C differs from gcc. Latest specification to C introduces some more data types to effectively use the memory. sizeof() is a unary operator available in C language to find the size of a data type or a variable.

Sizes of data types in Turbo C:

#include<stdio.h>
#include<stddef.h>/* for wchar_t type */
int main()
{
printf("\nSize of char is      %d bytes:",sizeof(char));
printf("\nSize of wchar_t is   %d bytes:",sizeof(wchar_t));
printf("\nSize of short is     %d bytes:",sizeof(short));
printf("\nSize of int is       %d bytes:",sizeof(int));
printf("\nSize of long is      %d bytes:",sizeof(long));
printf("\nSize of float is     %d bytes:",sizeof(float));
printf("\nSize of double is    %d bytes:",sizeof(double));
printf("\nSize of long double is %d bytes",sizeof(long double));
return 0;
}

Output:
Size of char is              1 bytes
Size of wchar_t is      1 bytes
Size of short is            2 bytes
Size of int is                 2 bytes
Size of long is              4 bytes
Size of float is              4 bytes
Size of double is         8 bytes
Size of long double is 10 bytes

Sizes of data types in gcc(C11):

Because gcc implements ISO/IEC C 2011 standards, short and int, char and wchar_t (wide character type), long and long long are treated as separate types. It implements bool type in <stdbool.h> to store either true or false. The primary types take more size in gcc comparatively with Turbo C.

Note: Execute this in any IDE like eclipse, Notepad++ or EditPlus which is configured with gcc.

#include<stdio.h>
#include<stddef.h>/* for wchar_t type */
#include<stdbool.h>/* for bool type */
int main()
{
printf("\nSize of char is      %d bytes",sizeof(char));
printf("\nSize of wchar_t is   %d bytes",sizeof(wchar_t));
printf("\nSize of short is     %d bytes",sizeof(short));
printf("\nSize of int is       %d bytes",sizeof(int));
printf("\nSize of long is      %d bytes",sizeof(long));
printf("\nSize of long long is %d bytes",sizeof(long long));
printf("\nSize of float is     %d bytes",sizeof(float));
printf("\nSize of double is    %d bytes",sizeof(double));
printf("\nSize of long double is %d bytes",sizeof(long double));
printf("\nSize of bool is      %d bytes",sizeof(bool));
return 0;
}

Output:
Size of char is             1 bytes
Size of wchar_t is     2 bytes
Size of short is           2 bytes
Size of int is                4 bytes
Size of long is             4 bytes
Size of long long is   8 bytes
Size of float is            4 bytes
Size of double is       8 bytes
Size of long double is 12 bytes
Size of bool is      1 bytes

Sizeof() using with variable name:

So far we have used sizeof() operator with data types, even we can use it with variable names to find the size of a variable.

#include<stdio.h>
int main()
{
char ch;
printf("\nSize of ch is %d bytes",sizeof(ch));
return 0;
}

Output:
Size of ch is 1 bytes

Guess the answers to the following questions

1) Size of short in Turbo C
2) Size of int in gcc(C11)
3) Size of bool in Turbo C
4) Size of long long in Turbo C
5) Size of wchar_t in gcc(C11)
6) Size of long double in gcc(C11)

Show Answers

1) In Turbo C both int, short or short int are same, takes just 2 bytes of memory
2) In Turbo C int takes 2 bytes, where as in gcc(C11) int takes 4bytes of memory
3) Turbo C doesn’t support bool type
4) Turbo C treats long and long long as same types, takes 4 bytes. Where as in gcc(C11) it takes 8 bytes used to store integers with huge range
5) wchar_t is a wide character type, takes 2 bytes in gcc(C11) and only 1 byte in Turbo C
6) Size of long double in gcc(C11) is 12 bytes and in Turbo C is just 8 bytes.

Previous post:

Next post: