+ 1
Purpose of the sizeof operator and how to use it?
3 Réponses
+ 2
we use sizeof operator to know the size of any variable for example if we want to know the size of character a we type
int x;
char a;
x=sizeof(a);
cout<<x;
0
The sizeof operator is used to calculate the space used by a variable (in bytes) in memory. For int, it is 4 bytes, for char it is 1 byte etc. It also varies from compiler to compiler.
void main()
{
int x=5,a[4];
char c;
int size=sizeof(x);
int size1=sizeof(a);
int size2=sizeof(c);
cout<<"Size of x is "<<size<<" bytes";
cout<<"\nSize of Array is "<<size1<<" bytes";
cout<<"\nSize of c is "<<size2<<" bytes";
}
0
sizeof is used to find bit size of variable
to find size of datatype use sizeof(datatype)
to find size of variable use sizeof variable
also it is ued to find size of array