What is the size of an int variable and a char variable. What is the size of a variable which points to an int and a variable that points to a char. Now write a piece of code that displays the sizes of the data types mentioned above.
Size of an int variable is 4 bytes. Size of a char variable is 1 byte. Size of variable which points to an int and a variable that points to a char are the same and it is 8 bytes.
#include <iostream>
using namespace std;
int main()
{
int a = 3;
char b = 's';
int* p = &a;
char* p1 = &b;
cout << "Size of an int variable: " <<sizeof(a) << " bytes.\n";
cout << "Size of a char variable: " << sizeof(b) << " bytes.\n";
cout << "Size of a variable that points to int: "<< sizeof(p) << " bytes.\n";
cout <<"Size of a variable that points to char: "<< sizeof(p1) << " bytes.\n";
}
Comments
Leave a comment