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

by | Jan 4, 2023 | C, C Programs

Introduction

In this program, we shall learn about the size of data types we use in C programming. Using the sizeof operator, we will calculate the size of all data types. Variables are declared with a particular data type because the size of the data type is used to allocate space in the memory for that variable.

To understand this program, you should know the following C concepts:

1. C data types

2. Variables, literals, and constants in C

3. Sizeof operator

Program

#include<stdio.h>
int main()
{
int A;
char B;
float C;
double D;
printf("Size of int: %d bytes\n",sizeof(A)); // sizeof operator will compute the size
of the parameter passed in it.
printf("Size of char: %d bytes\n",sizeof(B));
printf("Size of float: %d bytes\n",sizeof(C));
printf("Size of double: %d bytes\n",sizeof(D));
return 0;
}

 

Output

Explanation

Four variables that are A, B, C, Dare declared of type int, char, float, double.

printf(“Size of int: %d bytes\n”,sizeof(A));

In the above statement, the sizeof() operator is used to calculate the size of ‘A’. Since ‘A’ is an int type variable thus size of the int data type will be printed on the screen.

Similarly, other variables are passed in the same operator to calculate the size of other data types.

The size of anything is always an integer value therefore, we use %d in the ‘printf’ function.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.