×

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

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable.

Program to Determine Variable Size

Write a C or C++ program to determine the sizes of the four different types of variables that are given: int, char, float, and double.

Through the sizeof(variable) operation, a variable's size is obtained. Additionally, we can use the %ld format specifier to print the result produced by sizeof.

Examples:

  • Input: char
Output: Size of char = 1
  • Input: float
Output: Size of float = 4

To determine the Four Variables Sizes:

Following are the four types of variables that are defined:

  • integer_Type
  • float_Type
  • double_Type
  • char_Type

With the help of the sizeof() operator, the variables' sizes are determined.

The following is an example of the C and C++ program to evaluate the sizes of the int, char, float, and double types of data:

C Program:

// Using the C programming language, compute the sizes of the int, char, float, and double types of data.
#include <stdio.h>


int main ()
{
	int integer_Type;
	char char_Type;
	float float_Type;
	double double_Type;


// Obtain and display the size of the integer type.
	printf ( "The size of int type of data is: %ld\n",
		sizeof ( integer_Type ) );


// Obtain and display the size of the char type.
	printf ( "The size of char type of data is: %ld\n",
		sizeof ( char_Type ) );


// Obtain and display the size of the float type.
	printf ( "The size of float type of data is: %ld\n",
		sizeof ( float_Type ) );


// Obtain and display the size of the double type.
	printf ( "The size of double type of data is: %ld\n",
		sizeof ( double_Type ) );


	return 0;
}

C++ Program:

 // Using the C++ programming language, compute the sizes of the int, char, float, and double types of data.
#include <iostream>
using namespace std;


int main ()
{
	int integer_Type;
	char char_Type;
	float float_Type;
	double double_Type;


// Obtain and display the size of the integer type.
	cout << "The size of int type of data is: " << sizeof ( integer_Type ) << endl;


// Obtain and display the size of the char type.
	cout << "The size of char type of data is: " << sizeof ( char_Type ) << endl;


// Obtain and display the size of the float type.
	cout << "The size of float type of data is: " << sizeof ( float_Type ) << endl;


// Obtain and display the size of the double type.
	cout << "The size of double type of data is: " << sizeof ( double_Type ) ;


	return 0;
}

Output:

 The size of int type of data is: 4
The size of char type of data is: 1
The size of float type of data is: 4
The size of double type of data is: 8

Related Topics

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.