Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Find Size of fundamental data types

C++ Basic: Exercise-3 with Solution

Write a program in C++ to find Size of fundamental data types.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;
 
int main() 
{
    cout << "\n\n Find Size of fundamental data types :\n";
	cout << "------------------------------------------\n";
   cout << " The sizeof(char) is :          " << sizeof(char) << " bytes \n" ;
   cout << " The sizeof(short) is :         " << sizeof(short) << " bytes \n" ;
   cout << " The sizeof(int) is :           " << sizeof(int) << " bytes \n" ;
   cout << " The sizeof(long) is :          " << sizeof(long) << " bytes \n" ;
   cout << " The sizeof(long long) is :     " << sizeof(long long) << " bytes \n";
   cout << " The sizeof(float) is :         " << sizeof(float) << " bytes \n" ;
   cout << " The sizeof(double) is :        " << sizeof(double) << " bytes \n";
   cout << " The sizeof(long double) is :   " << sizeof(long double) << " bytes \n";
   cout << " The sizeof(bool) is :          " << sizeof(bool) << " bytes \n\n";
   return 0;
}

Sample Output:

 Find Size of fundamental data types :                                 
------------------------------------------                             
 The sizeof(char) is :          1 bytes                                
 The sizeof(short) is :         2 bytes                                
 The sizeof(int) is :           4 bytes                                
 The sizeof(long) is :          8 bytes                                
 The sizeof(long long) is :     8 bytes                                
 The sizeof(float) is :         4 bytes                                
 The sizeof(double) is :        8 bytes                                
 The sizeof(long double) is :   16 bytes                               
 The sizeof(bool) is :          1 bytes

Flowchart:

Flowchart: Find Size of fundamental data types

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to print the sum of two numbers.
Next: Write a program in C++ to print the sum of two numbers using variables.

What is the difficulty level of this exercise?