Re: sizeof(type)

Martin Husemann <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
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;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.