Exercise:
Write a C Program to find the size of int, float, double and char data type.
Click Here to View the Solution!
#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float data type: %zu bytes\n", sizeof(floatType));
printf("Size of double data type: %zu bytes\n", sizeof(doubleType));
printf("Size of char data type: %zu byte\n", sizeof(charType));
return 0;
}
Click Here to View the Output!
Size of int: 4 bytes
Size of float data type: 4 bytes
Size of double data type: 8 bytes
Size of char data type: 1 byte
Click Here to View the Explanation!
- This program is used to determine the size of the various data types.
- In the
main method
, 4 variables, each of a different data type are initialized. - The variables of the type,
int, float, double and char
are used for the initialization of the variables. - In the
printf()
functions, the size of each data type variable is used inside thesizeof() operator
which prints the size of each data type using the%zu
specifier that prints the length. - The
return 0 statement
exits the program.