tags:

views:

143

answers:

2

In the gcc compiler, sizeof(main), sizeof(printf) and sizeof(scanf) all are 1. I want to know how the size of all these are 1. What is the logic behind it?

A: 

I would have thought it is the size of a pointer to function. One actually surprises me...

Mario The Spoon
That should be a comment, it's not really an answer.
Joachim Sauer
instead of upvoting this comment, you people should downvote the "answer" instead.
codymanix
What's wrong with comments? I find the one of the most useful things ever is discussing, with other developers, what they thought. Eliminating that is, IMHO, gross folly.
Blank Xavier
+14  A: 
KennyTM
So applying sizeof to a function type is undefined/unspecified behaviour?
codymanix
thnx KennyTM...But I want to know how the gcc compiler returns 1 ??
Parixit
@Parixit: Because you did something that makes no sense, so you get a result that makes no sense. Garbage in, garbage out. It's undefined behavior, and the compiler could do anything it likes, including formatting your harddrive or setting your hair on fire. But it chose to do something a bit less dramatic, and just returned 1
jalf
I guess that function as is have no size (size=0), but sizeof is required to return something > 0. And yes, a compile error instead would have made more sense to me.
codymanix
@Parixit The gcc developers decided to hardcode that `sizeof function` is 1.
nos
As far as the standard is concerned, shouldn't this result in a diagnostic, rather than undefined behavior, since it's a breach of language constraints? Indeed, with `-pedantic`, gcc issues a warning. Obviously in non-standard modes (without `-pedantic`), gcc can do what it likes: if you're not following the standard, then *everything* is undefined behavior by definition.
Steve Jessop
@jaif: it should SO set his hair on fire.
Blank Xavier