sizeof(<function name>); return wht??

V

vicks

Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));
}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta
 
C

Chris Torek

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

[sample code snipped]

The problem is that you have not told your compiler to operate as
a C compiler. Most things labeled "C compilers" are not, in fact,
C compilers by default -- you have to give them some sort of
option(s) to make them obey their part of the "Standard C contract",
as it were.

In this case, my crystal ball says that you are using gcc, which
is not a C compiler unless you also use the "-ansi -pedantic".
Doing so will cause the compiler to (correctly) "emit a diagnostic"
telling you that sizeof should not be applied to function names.

(After producing the diagnostic, the compiler may -- or may not --
go on to produce an executable. If it does produce one and you
run it, Standard C says absolutely nothing about what will happen.
The documentation for that *particular* compiler might say what
will happen, but of course, if you ever use some *other* compiler,
something else may happen, or you might not get an executable
program.)
 
I

Ian Collins

vicks said:
Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));
}
Try turning your warning level up and see what your compiler tells you.
 
V

vicks

The problem is that you have not told your compiler to operate as
a C compiler. Most things labeled "C compilers" are not, in fact,
C compilers by default -- you have to give them some sort of
option(s) to make them obey their part of the "Standard C contract",
as it were.

In this case, my crystal ball says that you are using gcc, which
is not a C compiler unless you also use the "-ansi -pedantic".
Doing so will cause the compiler to (correctly) "emit a diagnostic"
telling you that sizeof should not be applied to function names.
....

Well yeap after using these switched for compilation I got the
warnings but my compiler still goes to give putput.. its ok.. With
this one more thing..

If i using this switches(-ansi ) then it uses C89/c99 std why am
asking this is because It gives an error when I uses "//" comment!!
 
C

Chris Torek

In this case, my crystal ball says that you are using gcc, which
is not a C compiler unless you also use the "-ansi -pedantic" [flags].

If i using this switches(-ansi ) then it uses C89/c99 std why am
asking this is because It gives an error when I uses "//" comment!!

This is getting off-topic (there are gcc-specific groups for
gcc-specific questions), but ... there are a bunch of different
switches for various versions of gcc. Depending on which version
of gcc you have, it will support one or more of:

-ansi
-std=c89
-std=gnu89
-std=gnu99
-std=c99

The first two mean the same thing, selecting "C89" (also sometimes
called "C90" or "ANSI C", although technically "ANSI C" should now
refer to C99, as ANSI adopted the C99 standard shortly after that
standard was available). The "-pedantic" flag is required for
actual conformance, but the -std (or -ansi) flag is required to
select the mode before turning on conformance.

C89 does not have "//" comments, hence selecting C89 conformance
makes them become syntax errors, as they should be.

Gcc (any version) does not fully support C99 despite the -std=c99
option. Selecting -std=c99, if you have it at all -- it is not
present in older versions -- gets you "as close as possible" in
that particular version of gcc. Exactly how close depends on
which version of gcc you use.

The various "gnu" standards implement the GNUC languages, which
resemble C fairly closely, but are different in a number of important
ways, including things like allowing "sizeof function" (with the
result always being (size_t)1 in at least some variants of GNUC).
Note that different variants of GNUC support *different* features,
so that using any one particular feature may prevent you from using
another version of gcc on your code. (To avoid being trapped into
particular versions of particular compilers, you can simply avoid
using the extensions. Of course, if the extension does something
you really *want*, you have to choose between "portability" and
"convenience and perhaps even accomplishment".)
 
S

Sarath

Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));

}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta

I dont know how this get compiled. those compilers which follows the
ISO C++ standard will generate error on compilation.

Regards,
Sarath
 
S

Sarath

Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));

}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta

Ignore my previous post. I forgot that you are asking about a C
environment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top