473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sizeof argument

Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

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

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Thanks in advance for any explanations.
Mar 25 '06 #1
11 2152
Ben C wrote:
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

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

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.


Good one. ;-)

'int ()' is actually a function type. (A function taking no argument
and returning an int.)

sizeof on a function type is undefined behavior. Whether it returns
1 or 10000, doesn't matter. It makes no sense.

With a appropriate compiler options, you should get a warning:
for instance, with GCC:

gcc -Wall -pedantic

Test1.c: In function `main':
Test1.c:5: warning: invalid application of `sizeof' to a function type

Mar 25 '06 #2
Ben C wrote:
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

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

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.


Maybe your warning level is not high enough? For me gcc with -Wall
-ansi and -pedantic options reports:
2.c: In function `main':
2.c:5: warning: invalid application of `sizeof' to a function type

But it compiles and prints 1 as output when run.

Mar 25 '06 #3
Ben C wrote:
Why does this compile with no warnings,
Because you have not got your diagnositics turned on.
gcc, even with the source language specified as gnu99,
warns

In function 'main':
5: warning: invalid application of 'sizeof' to a function type
5: warning: format '%d' expects type 'int', but argument 2 has type
'long unsigned int'

what, if anything, does it mean,
nothing, except as a non-portable extension provided by your implementation
and why does the program print out 1?
because that's what you implementation decided to interpret this
non-portable construct as meaning.

#include <stdio.h>

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

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Thanks in advance for any explanations.


Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.

Mar 25 '06 #4
On 2006-03-25, Guillaume <> wrote:
Ben C wrote:
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

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

[...]
'int ()' is actually a function type. (A function taking no argument
and returning an int.)

sizeof on a function type is undefined behavior. Whether it returns
1 or 10000, doesn't matter. It makes no sense.


Of course, a function type! I was using -Wall but not -pedantic. Thanks.

The original example (which is on comp.programming) was this:

typedef void (*voidf)();
... sizeof (voidf()) ...

Well, not sizeof (voidf()) actually, but va_arg(ap, voidf()).

Here voidf() is also a function type-- a function that returns a pointer
to a function that returns void and takes no arguments that takes no
arguments. Surely not what was intended.
Mar 25 '06 #5
Ben C wrote:
Of course, a function type! I was using -Wall but not -pedantic. Thanks.


Yet another one of these "GCCisms". I don't think this should be allowed
at all. A warning doesn't seem enough to me here: and you only get one
if you go "-pedantic" with GCC. Weird.

Some other C compilers report this construct as an error, and I think
they should as well.
Mar 25 '06 #6
Martin Ambuhl wrote:
Ben C wrote:
Why does this compile with no warnings,


Because you have not got your diagnositics turned on.
gcc, even with the source language specified as gnu99,
warns

In function 'main':
5: warning: invalid application of 'sizeof' to a function type
5: warning: format '%d' expects type 'int', but argument 2 has type
'long unsigned int'

what, if anything, does it mean,


nothing, except as a non-portable extension provided by your
implementation
and why does the program print out 1?


because that's what you implementation decided to interpret this
non-portable construct as meaning.

#include <stdio.h>

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

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Thanks in advance for any explanations.


Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human
beings do not use it.


Who's 'Bill Collectors' - any relation to 'Bill Posters?

--
==============
*Not a pedant*
==============
Mar 26 '06 #7

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:le*******************@newsread1.news.atl.eart hlink.net...

Thanks in advance for any explanations.


Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.


Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
you?"

I may not be the correct person from your perspective to defend BenC, but, I
haven't seen a harsh word yet. I'd say it's a polite statement and nothing
more.

Mar 26 '06 #8
Martin Ambuhl wrote:

[snip]
Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.


Really?

I guess that people like myself and probably the OP who have never
talked to or heard from a bill collector should be expected to know
better. Where did we go wrong?

--
jay
Mar 26 '06 #9
Martin Ambuhl wrote:

Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human
beings do not use it.


I'm going to have to call BS that. I don't believe it's in any way
impolite.

Brian
--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Mar 26 '06 #10
On 2006-03-26, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:le*******************@newsread1.news.atl.eart hlink.net...
>
> Thanks in advance for any explanations.


Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.


Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
you?"

I may not be the correct person from your perspective to defend BenC, but, I
haven't seen a harsh word yet. I'd say it's a polite statement and nothing
more.


Thank you! Certainly no implication of dominance or compliance was
intended, I am sorry if anyone took it that way.
Mar 26 '06 #11
Ben C <sp******@spam.eggs> writes:
On 2006-03-26, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:le*******************@newsread1.news.atl.eart hlink.net...
>
> Thanks in advance for any explanations.

Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.


Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
you?"

I may not be the correct person from your perspective to defend BenC, but, I
haven't seen a harsh word yet. I'd say it's a polite statement and nothing
more.


Thank you! Certainly no implication of dominance or compliance was
intended, I am sorry if anyone took it that way.


Some people are offended by the phrase "thanks in advance", thinking
it implies some sort of obligation. Others (myself included) don't
think it's at all a big deal. (I occasionally use "AtDhVaAnNkCsE"
myself.) I suggest that those who use the term should probably avoid
it, and those who are offended by it should consider that it's
probably not meant to be offensive.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 26 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
90
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
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
6
by: Daniel Rudy | last post by:
Hello Everyone, I've ran into a little snag with the sizeof operator. Can you get the size of a member inside a struct using sizeof? I looked through the FAQ and I couldn't find the answer to...
42
by: Jess | last post by:
Hello, if I have the following code that has an array of int*: #include<iostream> #include<string> #include<cstring> #include<cstddef> using namespace std; int main(){
17
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,
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
8
by: c.lang.myself | last post by:
whenever we pass an array to function, we have to also pass its size.e.g if i am making a soritng function then void sort(int b,int size); Now I thought about using sizeof operator(macro?)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.