C Program To Find the Size of int, float, double and char

In this post, you will get to know how to find the Size of different data types like int, float, double and char.

C Program To Find the Size of int, float, double and char

sizeof() operator is used to find the size of different data types. When the sizeof() operator is used with different data types such as int, float, char etc., it returns the amount of memory allocated to them.

C Program To Find the Size of int, float, double and char

// C Program To Find the Size of int, float, double and char
#include <stdio.h>
int main(){
    int a;
    float b;
    char c;
    double d;
    
    // Size of Variables
    printf("Size of int: %ld bytes \n", sizeof(a));
    printf("Size of float: %ld bytes \n", sizeof(b));
    printf("Size of char: %ld bytes \n", sizeof(c));
    printf("Size of double: %ld bytes", sizeof(d));
    
    return 0;
}

Output

Size of int: 4 bytes 
Size of float: 4 bytes 
Size of char: 1 bytes 
Size of double: 8 bytes

How Does This Program Work ?

    int a;
    float b;
    char c;
    double d;

In this program , we have declared 4 variables named as a, b, c and d respectively.

    // Size of Variables
    printf("Size of int: %ld bytes \n", sizeof(a));
    printf("Size of float: %ld bytes \n", sizeof(b));
    printf("Size of char: %ld bytes \n", sizeof(c));
    printf("Size of double: %ld bytes", sizeof(d));

Size of the variable is calculated using the sizeof() operator. Then, the size of each of the variables is displayed using printf() function. 

Here, we have used %ld format specifiers because %ld format specifiers are implemented for representing long integer values.

Some used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

printf() – printf() function is used to display and print the string under the quotation to the screen.

sizeof() – sizeof() operator is used to generate the storage size of an expression or a data type.

// – Used for Commenting in C

Conclusion

I hope after going through this tutorial, you clearly understood how to find the size of different data types.

If you still have any doubt regarding this, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *