tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sizeof(type)



On Mon, Nov 25, 2024 at 05:26:22PM +0100, Edgar Fuß wrote:
> May I use the wisdom of this list for a question that is not NetBSD-related?
> 
> Why does C's sizeof operator need parentheses when applied to a type?

Because the version with paranthesis gets a "type-id" instead of an
expression, and the rules are different.

In the code below try to write the second sizeof w/o ().

Martin


/* compile with cc -Wall -ansi */

#include <stddef.h>
#include <stdio.h>

struct foo { int a; float b; };

int main(int argc, char **argv)
{
	int foo = 42;

	size_t blah = sizeof foo;
	size_t blah2 = sizeof(struct foo);

	printf("blah: %zu\nblah2: %zu\n", blah, blah2);
	return 0;
}


Home | Main Index | Thread Index | Old Index