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

Discussion in 'C Programming' started by vicks, Dec 10, 2007.

  1. vicks

    vicks Guest

    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
     
    vicks, Dec 10, 2007
    #1
    1. Advertising

  2. vicks

    Chris Torek Guest

    In article <>
    vicks <> wrote:
    >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.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Dec 10, 2007
    #2
    1. Advertising

  3. vicks

    Ian Collins Guest

    vicks wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Dec 10, 2007
    #3
  4. vicks

    vicks Guest

    On Dec 9, 9:30 pm, Chris Torek <> wrote:
    > In article <>


    > 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!!
     
    vicks, Dec 10, 2007
    #4
  5. vicks

    Chris Torek Guest

    >On Dec 9, 9:30 pm, Chris Torek <> wrote:
    >> 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].


    In article <>
    vicks <> wrote:
    >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".)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Dec 10, 2007
    #5
  6. vicks

    Sarath Guest

    On Dec 10, 11:38 am, vicks <> wrote:
    > 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
     
    Sarath, Dec 10, 2007
    #6
  7. vicks

    Sarath Guest

    On Dec 10, 11:38 am, vicks <> wrote:
    > 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.
     
    Sarath, Dec 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ashish
    Replies:
    3
    Views:
    4,002
    Naveen K Kohli
    Sep 7, 2004
  2. code break

    Wht you mean this declaration..

    code break, Dec 7, 2006, in forum: C Programming
    Replies:
    3
    Views:
    436
  3. code break

    Wht you mean by this declaration..

    code break, Dec 7, 2006, in forum: C Programming
    Replies:
    1
    Views:
    294
    matevzb
    Dec 7, 2006
  4. wht's

    , Apr 21, 2007, in forum: C Programming
    Replies:
    5
    Views:
    433
    Malcolm McLean
    Apr 21, 2007
  5. heenagoyal11

    Wht is is the output of following code and why?

    heenagoyal11, May 1, 2012, in forum: C Programming
    Replies:
    0
    Views:
    330
    heenagoyal11
    May 1, 2012
Loading...

Share This Page