473,802 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2175
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.programmin g) 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*****@earthl ink.net> wrote in message
news:le******** ***********@new sread1.news.atl .earthlink.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

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

Similar topics

9
2251
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 the first int and &arr is a pointer to the whole array of 10 ints. Then why does sizeof(arr) gives 40 while sizeof(&arr) gives 4. Shouldn't is be the other way around.
42
2418
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
90
8501
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
6759
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 in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
6
2641
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 this one. Below is the code fragment. Granted it's pretty platform specific. typedef struct hwata_info_tag
42
7046
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
4113
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
3052
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
27
5612
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
1970
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?) inside sort function on b and get size of b i.e by sizeof(b)/sizeof (b) but i am not getting correct result.....
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9559
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10529
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10301
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10280
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10058
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9107
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4268
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

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.