The sizeof Operator
Sizeof एक keyword के साथ एक operator भी है । इसका उपयोग operand का memory size पता करने के लिए करते है। यह operand का memory size, bytes में बताता है। यहाँ operand, variable, data type, expression और user defined data type हो सकता है।
The sizeof is a keyword plus an operator. It is used to determine the memory size of its operand. It returns the memory size of its operand in number of bytes. The operand can be a variable, constant, expression or data type (including user defined data type). When you operate sizeof on data type or expression it must be parenthesized. And when you are operating sizeof on variable or constant, there must be at least one space between the operator sizeof and its operand. The size returned by the sizeof operator is printed using %u specifier. Here is an example to completely demonstrate the use of sizeof operator.
The sizeof is a keyword plus an operator. It is used to determine the memory size of its operand. It returns the memory size of its operand in number of bytes. The operand can be a variable, constant, expression or data type (including user defined data type). When you operate sizeof on data type or expression it must be parenthesized. And when you are operating sizeof on variable or constant, there must be at least one space between the operator sizeof and its operand. The size returned by the sizeof operator is printed using %u specifier. Here is an example to completely demonstrate the use of sizeof operator.
/* The sizeof operator */
#include<stdio.h>
#include<conio.h>
int main(void)
{
int x;
char ch;
float y;
double z;
clrscr();
printf("Size of x = %u\n",sizeof x);
printf("Size of ch = %u\n",sizeof ch);
printf("Size of y = %u\n",sizeof y);
printf("Size of z = %u\n",sizeof z);
printf("Size of character constant = %u\n",sizeof 'V');
printf("Size of integer constant = %u\n",sizeof 1234);
printf("Size of float constant = %u\n",sizeof 123.45f);
printf("Size of double constant = %u\n",sizeof 67.25);
printf("Size of character type = %u\n",sizeof (char));
printf("Size of integer type = %u\n",sizeof (int));
printf("Size of float type = %u\n",sizeof (float));
printf("Size of double type = %u\n",sizeof (double));
printf("Size of integer expression = %u\n",sizeof(20+30));
printf("Size of float expression = %u\n",sizeof(20.5f+30.5f));
printf("Size of double expression = %u\n",sizeof(2.5*2.5));
getch();
return 0;
}
Output:
Size of x = 2
Size of ch = 1
Size of y = 4
Size of z = 8
Size of character constant = 2
Size of integer constant = 2
Size of float constant = 4
Size of double constant = 8
Size of character type = 1
Size of integer type = 2
Size of float type = 4
Size of double type = 8
Size of integer expression = 2
Size of float expression = 4
Size of double expression = 8
See, the fifth line of output. The size of character constant is given 2 bytes. Isn’t it strange? Actually, character constants represent ASCII codes and they are internally stored as numbers. When you use them with sizeof operator they are automatically promoted to integer. Mean to say, writing sizeof(’A’) is same as to write sizeof(65), where 65 is the ASCII value of A. And thus the size obtained is 2, which is the size of integer.
NEXT : Bitwise Operators
PREVIOUS : Increment and Decrement Operators
c भाषा सीखने के लिए नमूना कार्यक्रम कोड
ReplyDeletec प्रोग्राम कोड टर्मिनल पासवर्ड
It's very helpful , thanks
ReplyDelete