I saw some code which used sizeof directly and wondered if it is standard C. To my surprise, it was working just fine. Here is an example:
#include <stdio.h>
#include <string.h>
int main()
{
char buff[255];
printf("size %d\n", sizeof buff);
return 0;
}
Output: size 255
As you can see, in the above example I used sizeof <variable>
instead of sizeof(<variable>)
Please throw more light on it.
No there is not alternative to sizeof() operator in standard ANSI C. Sizeof() is a compiletime operator can be applied to type too ,i.e. sizeof(int); runtime not call any function so sizeof() is very quick.
Sizeof is a much used operator in the C or C++. It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t.
It returns the size of a variable. It can be applied to any data type, float type, pointer type variables. When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data type.
"When sizeof's operand is a type, it has to be enclosed in parentheses. But when sizeof's operand is a variable, this is not required."--Expert C Programming: Deep C secrets. And from c11 standard:
sizeof unary-expression
sizeof ( type-name )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With