Determine the size of data type using sizeof keyword
When we compile the c++ program, the compiler translates that program into language of that machine in which you compile. For example, the size of the data type may vary on 16-bit and on 32-bit processor. In this case you might have to modify your source code to run your program properly. This is not the case in java. As java does not translates the program to machine language when it is compiled, rather it translates the program to bytecode. The java interpreter i.e. the java virtual machine (JVM) or the java run-time environment interprets the bytecode. Hence no modification is required. However in c++ we can find the amount memory allocated by each data type using the sizeof keyword.The below is the program by which you can know how much memory, a particular data type will require on the machine in which it is executed. For example with windows xp operating system which runs on pentium 4 processor the size of int and long is same, it is 4 bytes.
A C++ Program example to find the size of the data type using sizeof keyword..
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
cout << "The size of bool is" << setw (9) << ": "
<< sizeof (bool) << " byte" << endl;
cout << "The size of char is" << setw (9) << ": "
<< sizeof (char) << " byte" << endl;
cout << "The size of short is" << setw (8) << ": "
<< sizeof (short) << " byte" << endl;
cout << "The size of int is" << setw (10) << ": "
<< sizeof (int) << " byte" << endl;
cout << "The size of long is" << setw (9) << ": "
<< sizeof (long) << " byte" << endl;
cout << "The size of float is" << setw (8) << ": "
<< sizeof (float) << " byte" << endl;
cout << "The size of double is" << setw (7) << ": "
<< sizeof (double) << " byte" << endl;
cout << "The size of long double is" << setw (2) << ": "
<< sizeof (long double) << " byte" << endl;
return 0;
}
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters
I have spent a lot of the time in different blogs but this is really a unique blog for me.
ReplyDeletec++ programming
This comment has been removed by the author.
ReplyDelete
ReplyDeletenice information for beginners.thank you.
learn c++ tutorial