Write a C program to find the size of int, float, long, double and char

Description: Print the size (in bytes) of the basic data types of C.

Hint:

  • Use the sizeof operator and the %zu format specifier.

C program to find the size of data types

#include <stdio.h>

int main(void) {
    printf("int    : %zu bytes\n", sizeof(int));
    printf("float  : %zu bytes\n", sizeof(float));
    printf("long   : %zu bytes\n", sizeof(long));
    printf("double : %zu bytes\n", sizeof(double));
    printf("char   : %zu byte\n", sizeof(char));
    return 0;
}

Output:

Output of the above program is

int : 4 bytes
float : 4 bytes
long : 8 bytes
double : 8 bytes
char : 1 byte

How it works

  • sizeof gives the size of a type in bytes. It returns the type size_t, which is printed with %zu.
  • The sizes can change on a different computer. For example, long is 4 bytes on 64 bit Windows. This output is from a 64 bit Mac.

Read C Data Types.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.