C++ Program to Find Size of Primitive Data Types

This C++ Program which prints size of primitive datatypes. The program uses a library function sizeof( ) to print the size of the respective datatypes. The output streams are manipulated using the library functions setw( ) and setfill( ) defined in the header ‘iomanip’.

Here is source code of the C++ program which prints size of primitive datatypes. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Print Size of Primitive Datatypes
  3.  */
  4.  
  5. #include<iostream>
  6. #include<iomanip>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     char c;
  12.     int i;
  13.     long l;
  14.     float f;
  15.     double d;
  16.  
  17.     cout << "Type : " << " Size" << endl;
  18.     cout << "char " << setw(5) << setfill(' ')
  19.          << sizeof(c) << endl;
  20.     cout << "integer " << setw(2) << setfill(' ')
  21.          << sizeof(i) << endl;
  22.     cout << "long " << setw(5) << setfill(' ')
  23.          << sizeof(l) << endl;
  24.     cout << "float " << setw(4) << setfill(' ')
  25.          << sizeof(f) << endl;
  26.     cout << "double " << setw(3) << setfill(' ')
  27.          << sizeof(d) << endl;
  28. }

$ g++ main.cpp
$ ./a.out
Type :  Size
char     1
integer  4
long     8
float    4
double   8

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.