The Wayback Machine - https://web.archive.org/web/20240118170213/https://www.geeksforgeeks.org/c-program-to-find-the-size-of-int-float-double-and-char/
Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given four types of variables, namely int, char, float and double, the task is to write a program in C to find the size of these four types of variables. sizeof Examples:

Input: int
Output: Size of int = 4
Input: double
Output: Size of double = 8

Here is a list of all the data types with its size, range and the access specifiers:

Data Type Memory (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4   %f
double 8   %lf
long double 12   %Lf

To find the size of the four variables:

  1. The four types of variables are defined in integerType, floatType, doubleType and charType.
  2. The size of the variables is calculated using the sizeof() operator.

C




// C program to find the size of int, char,
// float and double data types
 
#include <stdio.h>
 
int main()
{
    int integerType;
    char charType;
    float floatType;
    double doubleType;
 
    // Calculate and Print
    // the size of integer type
    printf("Size of int is: %ld", sizeof(integerType));
 
    // Calculate and Print
    // the size of charType
    printf("\nSize of char is: %ld", sizeof(charType));
 
    // Calculate and Print
    // the size of floatType
    printf("\nSize of float is: %ld", sizeof(floatType));
 
    // Calculate and Print
    // the size of doubleType
    printf("\nSize of double is: %ld", sizeof(doubleType));
 
    return 0;
}


Output

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

The time complexity and auxiliary space are both constant time, O(1).

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 15 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads