Monday, April 28, 2025

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

 This C code is a simple program that demonstrates the use of the sizeof operator in C to determine the size of different data types.

#include<stdio.h>

int main()
{

    int int_type;
    float float_type;
    double double_type;
    char char_type;

    printf("Int size: %zu bytes \n", sizeof(int_type));
    printf("Float size: %zu bytes \n", sizeof(float_type));
    printf("Double size: %zu bytes \n", sizeof(double_type));
    printf("Char size: %zu byte \n", sizeof(char_type));

    return 0;
}

Result:

Int size: 4 bytes
Float size: 4 bytes
Double size: 8 bytes
Char size: 1 byte

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.

The code starts by including the standard input-output library stdio.h. The program then defines four variables of different data types: int_type, float_type, double_type, and char_type.

 
int int_type;
float float_type;
double double_type;
char char_type;

 

These variables are not assigned any initial value, so they contain undefined values.

Next, the printf function is used to print the size of each data type using the sizeof operator. The sizeof operator returns the size of a given data type in bytes. The %zu format specifier is used to print the size_t value returned by the sizeof operator.

 
printf("Int size: %zu bytes \n", sizeof(int_type));
printf("Float size: %zu bytes \n", sizeof(float_type));
printf("Double size: %zu bytes \n", sizeof(double_type));
printf("Char size: %zu byte \n", sizeof(char_type));

 

The output of the program will show the size of each data type in bytes.

Finally, the main function returns 0, which indicates successful execution of the program.

Overall, this program is a simple demonstration of the use of the sizeof operator in C to determine the size of different data types.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...