Cyberithub

10 Useful Examples of sizeof function in C

In this tutorial, I will take you through sizeof function in C. sizeof operator or method is used to determine the size that occupied by a variable or data type or pointer variable in the memory. It returns the total number of bytes it occupies in the memory. It is an inbuilt C function.

10 Useful Examples of sizeof function in C 1

sizeof function in C

Also Read: Top 17 GIT Command Examples on Linux

1. Check the Size of Int Data Type

If you want find the size of Int data type, then you can find it by using sizeof(int) function with int passed as an argument in below program. Here the output will be 4 bytes as Int data type takes 4 bytes of space in the memory.

#include <stdio.h>
void main()
{
printf("Size of Int is:%d\n",sizeof(int));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Int is: 4

2. Check the Size of Char Data Type

If you want to find out the size of a char data type then you can use sizeof(char) function with char as argument. You can see from the output, it returns 1 byte of size which is the size it takes in memory.

#include <stdio.h>
void main()
{
printf("Size of Int is:%d\n",sizeof(char));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Char is: 1

3. Check the Size of Float Data Type

If you need to determine the size of float data type, then you can do it by using sizeof(float) function with float passed as an argument. Notice that it also returns 4 bytes of space same as int data type size.

#include <stdio.h>
void main()
{
printf("Size of Float is:%d\n",sizeof(float));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Float is: 4

4. Check the Size of Double Data Type

If you want to find out the size of double data type, then you need to use sizeof(double) function with double passed as an argument to sizeof operator as shown in below program. You can see from the output,it returns 8 bytes which is the size it takes in memory.

#include <stdio.h>
void main()
{
printf("Size of Double is:%d\n",sizeof(double));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Double is: 8

5. Check the size of Structure without Padding

If you want to check the size of the structure which contains same kind of datatype then compiler does not need to do padding here and shows the total number of bytes contained by the member variables inside the Structure. Here there are 2 char variables, so it will be 1 + 1 = 2 bytes. Hence you need to use sizeof function in C program as shown below.

#include <stdio.h>

struct test
{
char a;
char b;
}d;

void main()
{
printf("Size of Structure is:%d\n",sizeof(d));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Structure is: 2

NOTE:

In order to align the data in memory, one or more empty bytes are inserted between memory address which are allocated for other structure members while memory allocation. This concept is called Structure Padding.

6. Check the size of Structure with Padding

If you want to check the size of structure which contains different kinds of data type variables, then the size of structure will be determined by the size of member variable followed by the largest size variable. For example, In this case i have taken char variable which requires 1 byte and a double variable which requires 8 bytes in memory but longest datatype is double so total size of structure will be multiple of double datatype to avoid any alignment issues. Since there are 2 datatype here so it will be 8 + 8 = 16 bytes.

#include <stdio.h>

struct test
{
char a;
double b;
}d;

void main()
{
printf("Size of Structure is:%d\n",sizeof(d));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Structure is: 16

7. Check the Size of 1-D Array

If you want to check the size of 1-Dimensional array, then you need to use sizeof function in C program as shown below. Here we have declared 10 array elements of double types, hence below program should return 10 x 8 = 80 bytes which is indeed the case. It is due to the fact that each double data type takes 8 bytes of space in memory.

#include <stdio.h>
void main()
{
double a[10];
printf("Size of Array is:%d\n",sizeof(a));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of Array is: 80

8. Check the Size of 2-D Array

If you want to check the size of 2-Dimensional array a[2][3] having 2 rows and 3 columns, then you need to use sizeof function in C Program as shown below. Here each element is of Int type, so it should return 2 rows x 3 cols x 4 = 24 bytes which is the same output we got below.

#include <stdio.h>
void main()
{
int a[2][3]= {{6,8,5},{7,4,9}};
printf("Size of 2-D Array is:%d\n",sizeof(a));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of 2-D Array is: 24

9. Check the Size of Pointer Variable

If you want to check the size of pointer variable *a, then you need to use sizeof function in C Program as shown below.

#include <stdio.h>
void main()
{
char *a = NULL;
printf("Size of pointer variable is:%d\n",sizeof(*a));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of block pointed by a pointer is: 1

10. Check the Size of Structure member Using Structure Pointer

If you want to check the size of a Structure member variable for example int *a in this case, then you can check it by using sizeof(*c->a) function as shown in below program.

#include <stdio.h>

struct test
{
int *a;
double *b;
};

void main()
{
struct test *c;
printf("Size of Structure member pointer variable is:%d\n",sizeof(*c->a));
}

Compile Your Program

[root@localhost ~]# gcc -o size size.c

Output

[root@localhost ~]# ./size
Size of int pointer: 4

Also Read: 30+ AWK Examples for Beginners

Leave a Comment