What is the sizeof() function in C Programming ? - ProgrammerTech
Language:

Search

What is the sizeof() function in C Programming ?

  • Share this:
What is the sizeof() function in C Programming ?

sizeof() function in C Language

The function ( sizeof() ) is a function used to find the size of variables in memory in bytes of a variable or array and can be applied to any type of data including integers .... etc. We talked earlier about the data types that we learned earlier, which are int, double, float, char. In this lesson, we will learn about what is the size of each data type.

 

Using sizeof with an int data type

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 23;
    printf("size of x = %d\n", sizeof(x));
    return 0;
}

We create a variable of type int x and its value is 23 and then we print the word size of x is equal to. Then we put the function sizeof() to define the editor that we want the size of the variable x, then we ran the code and got the following result and its size is 4 bytes.

size of x = 4

 

Using sizeof with a data type double

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    double cs = 2.3;
    printf("size of cs = %d\n", sizeof(cs));
    return 0;
}

We created a variable of type double, cs, and its value is 2.3, and then we typed the word size of cs equal to . Then we set the function sizeof() to tell the editor that we want the size of the cs variable. Then we ran the code and got the following result and its size is 8 bytes.

size of cs = 8

 

Using sizeof with a float data type

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    float s = 3.2;
    printf("size of s = %d\n", sizeof(s));
    return 0;
}

We create a float variable of type s and its value is 3.2 and then we print the word size of s is equal to. Then we set the function sizeof() to tell the editor that we want the size of the variable s. Then we ran the code and got the following result and its size is 4 bytes.

size of s = 4

 

Using sizeof with a char data type

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    char c = 's';
    printf("size of c = %d\n", sizeof(c));
    return 0;
}

We have created a variable of type char which is c and its value is s because this type accepts only one character. Then we print the word size of c equal to and then set the function sizeof to tell the editor that we want the size of the variable c. Then we ran the code and got the following result and its size is 1 byte.

size of c = 1

 

Using sizeof with arrays

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int array[3] = {1, 2, 3};
    printf("size of array = %d\n", sizeof(array));
    return 0;
}

We have created an array of type int and it is array and its size is 3 and its values ​​are 1, 2, 3 and then we print the word size of array equals. Then we put the function sizeof to define the editor that we want the size of the array then we ran the code and got the following result and its size is 12 bytes.

size of array = 12

Example that collects all data types via the sizeof function

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 23;
    double cs = 2.3;
    float s = 3.2;
    char c = 's';
    int array[3] = {1, 2, 3};
    printf("size of x = %d\n", sizeof(x));
    printf("size of cs = %d\n", sizeof(cs));
    printf("size of s = %d\n", sizeof(s));
    printf("size of c = %d\n", sizeof(c));
    printf("size of array = %d\n", sizeof(array));
    return 0;
}

We have explained all this in the examples and collected them for you in one code and when you run this code, the result will be

size of x = 4
size of cs = 8
size of s = 4
size of c = 1
size of array = 12

We can see from the previous output window that the size of a variable of type int is 4 bytes in memory, i.e. it reserves 4 bytes. A variable of type float also reserves 4 bytes in memory. As for the double variable, it reserves 8 bytes of memory, which is double the float because it is a double. The literal variable can only hold one character, so only 1 byte is reserved in memory. And in the end we put a number array with three values ​​and when printing it gave us that it reserves 12 bytes of memory. That is, because its type is int and the type int reserves 4 bytes of memory and it has three values ​​of type int, i.e. 4+4+4 equals 12.


Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.