×

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

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

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.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.