By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

sizeof main;

P: n/a
Hi,

Why the following program gives two different
answers? -

#include <stdio.h>

int
main ( void )
{
printf ( "%u\n", sizeof ( main ) );
printf ( "%u\n", sizeof ( main () ) );
return 0;
}

GCC 3.3.2 (Windows XP) gives the following o/p:

1
4

Thanks.

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Vijay Kumar R Zanvar wrote:
Why the following program gives two different
answers? -

printf ( "%u\n", sizeof ( main ) );
I don't know what this is supposed to mean. The size of the code of the
main function? Apparently not, given the returned value. Allowing this
seems to be a GCC extension, and not a very useful one. Maybe the gcc
manual explains it.
printf ( "%u\n", sizeof ( main () ) );


This is the size of the expression main(), i.e. the size of main's
return type, int.

--
Hallvard
Nov 13 '05 #2

P: n/a
Vijay Kumar R Zanvar wrote:
Hi,

Why the following program gives two different
answers? -


#include <stdio.h>

int
main ( void )
{
printf ( "%u\n", sizeof ( main ) );

This is not really allowed. In ISO-C section 6.5.4.3 it is stated:
Constraints
1 The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of such a
type, or to an lvalue that designates a bit-field object.

If you turn on all options in gcc to obtain good warnings etc., you will
capture the error:

gcc -ansi -pedantic -Wall sizeof.c
sizeof.c: In function `main':
sizeof.c:6: warning: sizeof applied to a function type

The function main is of 'int (*f)(X)' type, where I am not entirely sure
how to specify X (variable argument list that does not follow ISO-C, may
be '...'). That is, main is of a function type.

I am uncertain that the return value of sizeof has an actual meaning in gcc.
printf ( "%u\n", sizeof ( main () ) );
This is the size of 'int' type.
return 0;
}

GCC 3.3.2 (Windows XP) gives the following o/p:

1
4

Thanks.

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/


Nov 13 '05 #3

P: n/a
Vijay Kumar R Zanvar wrote:
#include <stdio.h>

int
main ( void )
{
printf ( "%u\n", sizeof ( main ) );
printf ( "%u\n", sizeof ( main () ) );
return 0;
}

GCC 3.3.2 (Windows XP) gives the following o/p:

1
4


Probably some weird gcc extension since:

$ gcc -pedantic toto.c
toto.c: In function `main':
toto.c:5: warning: sizeof applied to a function type

Therefore, you might want to ask on the gcc mailing list.

Nov 13 '05 #4

P: n/a
Thank You. It solves my problem.
Nov 13 '05 #5

P: n/a
Vijay Kumar R Zanvar wrote:
Hi,

Why the following program gives two different
answers? -


The *real* question is why you ignored or suppressed the gcc diagnostics
which I have embedded as comments into your code:

#include <stdio.h>

int main(void)
{
printf("%u\n", sizeof(main));

/* gcc diagnostics:
warning: invalid application of `sizeof' to a function type
warning: unsigned int format, long unsigned int arg (arg 2)
*/
printf("%u\n", sizeof(main()));

/* gcc diagnostic:
warning: unsigned int format, long unsigned int arg (arg 2)
*/
return 0;
}

--
Martin Ambuhl

Nov 13 '05 #6

P: n/a
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote:
Why the following program gives two different
answers? -
Short answer: because C is not Pascal.

Long answer:
printf ( "%u\n", sizeof ( main ) );
This asks for the size of the main function, which is actually illegal:

# 6.5.3.4 The sizeof operator
# Constraints
# 1 The sizeof operator shall not be applied to an expression that has
# function type

Since your compiler apparently decided to compile thr program anyway,
this constraint violation invokes undefined behaviour, and may give any
answer whatsoever. In principle, anything which happens after this point
could be completely random.
printf ( "%u\n", sizeof ( main () ) );
This asks for the size of the value returned by evaluating main(). Since
main() returns int (and you've correctly defined it as doing so), that
is equal to sizeof (int). Note, btw, that main() isn't evaluated, since
its size can be determined without doing so (unlike its value).

Note also that you are invoking undefined behaviour in both printf()
calls, because you don't know that size_t is equivalent to unsigned int.
Either cast both sizeofs to (unsigned int), so it corresponds to the
printf() specifier %u, or, if you use C99, change the %u to %zu.
GCC 3.3.2 (Windows XP) gives the following o/p:

1
4


Apparently, your compiler has decided to give all functions a size of 1,
and int is four bytes large on your implementation.

Richard
Nov 13 '05 #7

P: n/a

The *real* question is why you ignored or suppressed the gcc diagnostics
which I have embedded as comments into your code:


May be there is no meaning to the program what I have written.
But, look! What a response I am getting and how it is helping
me to learn more! Do you not agree?

I did notice the warnings, but that was not my answer.

Thanks.

--
Vijay Kumar R Zanvar
Nov 13 '05 #8

P: n/a
In <bq*************@ID-203837.news.uni-berlin.de> "Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:
I did notice the warnings, but that was not my answer.


If you did notice them, yet you couldn't figure out the answer, you have
a problem. A big one.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9

P: n/a
In <bq**********@news-rocq.inria.fr> Grumble <in*****@kma.eu.org> writes:
Vijay Kumar R Zanvar wrote:
#include <stdio.h>

int
main ( void )
{
printf ( "%u\n", sizeof ( main ) );
printf ( "%u\n", sizeof ( main () ) );
return 0;
}

GCC 3.3.2 (Windows XP) gives the following o/p:

1
4


Probably some weird gcc extension since:


Indeed: in GNU C, sizeof(something) yields 1 when something is not a
complete object type. The purpose of this extension is to allow
performing pointer arithmetic on pointers to functions or to objects
of incomplete types, like void pointers by treating them as pointers to
char. It saves some casts or temporary pointer variables in certain
cases, which is a good thing (if you can afford it).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.