473,513 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof main;

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
9 6260
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
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
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
Thank You. It solves my problem.
Nov 13 '05 #5
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
"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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
13556
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!"; int size1 = sizeof(std::string); int size2 = sizeof(someString); and printed out the values of size1 and size2. size1 and size2 always
15
6225
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float *xPrev, float *yPrev);
3
1645
by: kimi | last post by:
Hello, yes i try to learn c-programming.So i have a question on my code, but if i am wrong here please tell me a group where i belong to. I wrote a function which fgets a filename from the commandline. It worked great when i put the code in main(). Then why not, copy this piece in a function. But when i type in the text, pressed enter i...
8
2519
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
7
5652
by: rahul8143 | last post by:
hello, why following 2 codes does not give same result that is 10? 1) #include <stdio.h> #define DIM(array) sizeof(array)/sizeof(int) void main() { int arr; printf("The dimension of the array is %d", DIM(arr)); }
90
8293
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
2541
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
17
4079
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
3
2966
by: Andreas Eibach | last post by:
Hi, when I had in mind to turn this code (which worked fine in main()) to a separate function, I stumbled upon this...(of course, it's way bigger than that, but I hope this short snippet can demonstrate the issue anyway) -begin code (Ì) - int main(void) { unsigned char rawdata =
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.