Skip to content
Related Articles

Related Articles

Improve Article
C/C++ program to find the size of int, float, double and char
  • Difficulty Level : Basic
  • Last Updated : 05 Dec, 2019

Given four types of variables, namely int, char, float and double, the task is to write a program in C or 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 TypeMemory (bytes)RangeFormat Specifier
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int4-2,147,483,648 to 2,147,483,647%ld
unsigned long int40 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long long int80 to 18,446,744,073,709,551,615%llu
signed char1-128 to 127%c
unsigned char10 to 255%c
float4%f
double8%lf
long double12%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.

Below is the C and C++ program to find the size of int, char, float and double data types:

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\n",
           sizeof(integerType));
  
    // Calculate and Print
    // the size of charType
    printf("Size of char is: %ld\n",
           sizeof(charType));
  
    // Calculate and Print
    // the size of floatType
    printf("Size of float is: %ld\n",
           sizeof(floatType));
  
    // Calculate and Print
    // the size of doubleType
    printf("Size of double is: %ld\n",
           sizeof(doubleType));
  
    return 0;
}

C++




// C++ program to find the size of int, char,
// float and double data types
#include <iostream>
using namespace std;
  
int main() 
    int integerType; 
    char charType; 
    float floatType; 
    double doubleType; 
  
    // Calculate and Print 
    // the size of integer type 
    cout << "Size of int is: " << 
    sizeof(integerType) <<"\n"
  
    // Calculate and Print 
    // the size of doubleType 
    cout << "Size of char is: " << 
    sizeof(charType) <<"\n"
      
    // Calculate and Print 
    // the size of charType 
    cout << "Size of float is: " << 
    sizeof(floatType) <<"\n";
  
    // Calculate and Print 
    // the size of floatType 
    cout << "Size of double is: " << 
    sizeof(doubleType) <<"\n"
  
    return 0; 
Output:
Size of int is: 4
Size of char is: 1
Size of float is: 4
Size of double is: 8
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :