Thursday, July 18, 2013

C language - facts of sizeof operator


sizeof is operator of 'C' language, which returns integer value that represents
the number of bytes allocated by the given operand.

Note : I have used Turbo C compiler to compile and run the program i.e. I am following the concept of 16 bits Turbo C compiler.


Since sizeof is operator, therefore we can use sizeof in two different ways as follows.

1. sizeof <operand>;
2. sizeof(<operand>);

Using syntax 1 or 2 will return the same result.

Once you got full understanding of sizeof operator, will give you different types of
basic technical understanding related to sizeof and other concepts.

I divided the complete concepts of sizeof operator in 3 different Levels as

Level I : Find the size of primitive type of variable

main()
{
char ch;
int n;
float f;
double d;
long l;
clrscr();
printf("%d\n",sizeof(ch));
printf("%d\n",sizeof(n));
printf("%d\n",sizeof(f));
printf("%d\n",sizeof(d));
printf("%d\n",sizeof(l));
}
output :
1
2
4
8
4

In the above example, five variables are defined as char,int,float,double and long.

During the execution of program, variables will allocate memory as per the memory size of datatype as follows.

char    1
int    2
float    4
double    8
long    4

sizeof returns the number of bytes of allocated memory of given variable.

Level II : Find the size of datatypes and constants 

 
main()
{
printf("%d\n",sizeof(char));
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(float));
printf("%d\n",sizeof(double));
printf("%d\n",sizeof(long));
}

output :
1
2
4
8
4

Most important concept in constants i.e. to calculate the size of constant, one must have knowledge of internal
datatype of compiler for different types of constants.
Constants Number of Bytes

character    2
Floating    8
Integer        2


When we write any character constant (e.g. 'A','@','#') then at the time execution, execution control will allocate
2 bytes of memory to hold ascii code number of given character. For floating constant number, it will allocate
8 bytes (double) of memory to hold floating number.

Level III : Find the size of structure, pointers & Arrays

Let us move towards, more higher level concept of sizeof operator.

Structure


    If the structure is empty then size of structure is 1 and this 1 byte of memory is allocated to indicate
type i.e. structure (user defined data type).
    If the structure contain some variables then sizeof calculates the size of each variable of structure and
returns the sum of number of bytes of all member variables of structure.

Example 1:

main()
{
struct Data
{
};
struct Data d;
printf("%d\n",sizeof(struct Data));
printf("%d\n",sizeof(d));
}
output :
1
1

Example 2:
 main()
{
struct Data
{
int n;
char ch;
};
struct Data d;
printf("%d\n",sizeof(struct Data));
printf("%d\n",sizeof(d));
}
output :
3
3

Pointers & Arrays


    Here pointer means address of variable. In turbo c (16 bits compiler), it provides 16 bits (2 bytes) of
memory space to hold number of address of any variable.
Example 1:          
main()
{
int a;
int a1[10];
char ch[10];
struct Data
{
    int n;
    char ch;
};
struct Data d;
printf("%d\n",sizeof(&a));
printf("%d\n",sizeof(a1));
printf("%d\n",sizeof(ch));
printf("%d\n",sizeof(&d));
}
output :
2
20
10
2
    when we pass array variable as argument to sizeof operator, it calculates the total size of array as follows.
Example1 :

int a1[10];
sizeof(a1); ==> sizeof(int) * 10 ==> 2 * 10 ==> 20

Example2 :

char ch[10];
sizeof(ch); ==> sizeof(char) * 10 ==> 1 * 10 ==> 10



Other Blogs

Engineers View