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.
/*
* C++ Program to Print Size of Primitive Datatypes
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char c;
int i;
long l;
float f;
double d;
cout << "Type : " << " Size" << endl;
cout << "char " << setw(5) << setfill(' ')
<< sizeof(c) << endl;
cout << "integer " << setw(2) << setfill(' ')
<< sizeof(i) << endl;
cout << "long " << setw(5) << setfill(' ')
<< sizeof(l) << endl;
cout << "float " << setw(4) << setfill(' ')
<< sizeof(f) << endl;
cout << "double " << setw(3) << setfill(' ')
<< sizeof(d) << endl;
}
$ 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Computer Science Internship
- Apply for Information Technology Internship
- Buy Programming Books
- Buy C++ Books
- Practice Computer Science MCQs