What arguments does the sizeof operator take in C?
[The original name refers to the sizeof function.]
I tried these and they all worked:
char * p; printf ("Size of * p is% d \ n", sizeof (* p)); // result = 1 printf ("Size of p is% d \ n", sizeof (p)); // result = 4 printf ("Size of p is% d \ n", sizeof (& p)); // result = 4
I wonder why the first printf is 1, the 2nd and 3rd are 4? So what arguments can sizeof have?
Type required.
sizeof(char)
always alone. The variable p
itself is a pointer, and on your platform is of size 4. Then you execute &p
or a pointer to a pointer, which is also of size 4.
On most modern desktop systems, a 32-bit architecture will contain 4 byte pointers, and a 64-bit architecture will contain 8 byte pointers.
sizeof
itself is a compile-time allowed keyword, not a function. In C99, arrays can be of variable length, and sizeof will wait at runtime to allow that size.
sizeof
is not a function, it is a keyword. You can remove the parentheses and you should be fine. Since it is not a function, it works with whatever type or object you give it - it is much more flexible than a function.
sizeof
is not a function; this is the operator. It can be used in two ways: how sizeof(typename)
and how sizeof expression
. When used with a type name, parentheses are required. The parentheses are not needed when the operand is an expression, although for clarity, many programmers will express the expression in parentheses independently. Note that unlike most programming language operators, it sizeof expression
does not evaluate its argument under normal circumstances, that is, when its operand is not a C99 variable length array.
You are of course working on a 32-bit machine.
*p is a character == 1 byte.
p is a pointer == 4 bytes.
&p is an address of a pointer == 4 bytes. Same as char**
Also note:
sizeof 'a' == 1 // C++
but
sizeof 'a' == 4 /* C */
(To be precise, sizeof 'a' == sizeof (int) in C, as posted by pmg).
In C, 'a' is promoted to int before being evacuated. One of the few incompatibilities between the two languages.
sizeof (char) = 1
sizeof (void *) = 4
The first one is 1 because you are passing char. (p points to char) 2nd and 3rd calls get a char * / void * object, so they give 4.
The first answer says char is 1 byte. (derefing your pointer to the actual char data)
The second says that the pointer value is a 4-byte value, that is, the memory location is represented by a 4-byte value.
The third says that the pointer address is 4 bytes.
Size can take all of these arguments, and it provides the exact answer for all values.
Greetings.
In addition to other answers, beware of possible terminological confusion.
In the C world, "byte" is the same as "char". That is, each 'char' is 1 'byte' by definition. This byte value is not necessarily related to the machine byte in the underlying hardware. C 'byte' (i.e. 'char') can be multiple bytes of the machine. However, in a C program, you don't have access to machine bytes. Everything is measured in C bytes (ie "characters"), and "sizeof" gives the size, measured in "characters".
For these reasons, 'sizeof (char)' is always 1, no matter how many machine bytes each 'char' consists of.
The operand of the operator sizeof
is the type.
If you use sizeof with an object or value, its type is used instead.
sizeof 65; /* same as `sizeof (int);` */
sizeof 'X'; /* same as `sizeof (int);` */
sizeof 42.0; /* same as `sizeof (double);` */
float f;
sizeof f; /* same as `sizeof (float);` */
/* etc ... */
You can specify the type directly for the sizeof operator (as in the same as above). The parentheses are needed "to translate the operand into the desired type" (as in (int)2.5
), and not as the syntax itselfsizeof
#include <math.h>
sizeof (12 * log(100)); /* same as `sizeof (double);` */
/* without the parenthesis, this would be */
/* (sizeof 12) * log(100); */
Size of operator show the size of which data type we used
#include<stdio.h>
int main()
{
int a;
float b;
char c;
double d;
printf("%d",sizeof(a));
printf("%d",sizeof(a));
printf("%d",sizeof(a));
printf("%d",sizeof(a));
}
Answer a=4;
b=4;
c=1;
d=8
If you are using 8 bit compiler.