sizeof() O/P

Discussion in 'C Programming' started by raghu, Dec 24, 2005.

  1. raghu

    raghu Guest

    i'm surprised at the output of the following code. compiled in turbo C

    void main()
    {
    printf("%d",sizeof(printf());
    }
    the output was : 2
    how come the output is 2? actually what is the property of sizeof()
    and printf()?are there any return types for the two functions? hoping
    for the positive responses.. thanks a lot in advance.
    raghu, Dec 24, 2005
    #1
    1. Advertising

  2. raghu

    Chris Hills Guest

    In article <>,
    raghu <> writes
    >i'm surprised at the output of the following code. compiled in turbo C
    >
    >void main()
    >{
    >printf("%d",sizeof(printf());
    >}
    >the output was : 2
    >how come the output is 2? actually what is the property of sizeof()
    >and printf()?are there any return types for the two functions? hoping
    >for the positive responses.. thanks a lot in advance.


    Ignoring that it should have been

    #include <stdio.h>
    int main(void)
    {
    printf("%d",sizeof(printf());
    return 0;
    }

    Who set this home work?


    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Dec 24, 2005
    #2
    1. Advertising

  3. raghu said:

    > i'm surprised at the output of the following code. compiled in turbo C
    >
    > void main()


    Don't be. Any program which gives the wrong return type for main() exhibits
    undefined behaviour, so any behaviour, surprising or not, is permissible as
    far as the C language is concerned.

    > {
    > printf("%d",sizeof(printf());


    Calling printf without a correct prototype in scope invokes undefined
    behaviour. Calling printf without any arguments invokes undefined
    behaviour.

    Start your program like this:

    #include <stdio.h>

    int main(void)

    ....and then try your program again, first fixing any diagnostics issued by
    your compiler.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 24, 2005
    #3
  4. Chris Hills said:

    > In article <>,
    > raghu <> writes
    >>i'm surprised at the output of the following code. compiled in turbo C
    >>
    >>void main()
    >>{
    >>printf("%d",sizeof(printf());
    >>}
    >>the output was : 2
    >>how come the output is 2? actually what is the property of sizeof()
    >>and printf()?are there any return types for the two functions? hoping
    >>for the positive responses.. thanks a lot in advance.

    >
    > Ignoring that it should have been
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > printf("%d",sizeof(printf());


    No, %d doesn't match size_t.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 24, 2005
    #4
  5. Richard Heathfield said:

    > raghu said:
    >
    >> printf("%d",sizeof(printf());

    >
    > Calling printf without a correct prototype in scope invokes undefined
    > behaviour. Calling printf without any arguments invokes undefined
    > behaviour.


    Except, of course, that the code doesn't actually call printf without any
    arguments! Sorry about that. But the rest of my reply stands.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 24, 2005
    #5
  6. raghu

    Eric Sosman Guest

    raghu wrote:
    > i'm surprised at the output of the following code. compiled in turbo C
    >
    > void main()
    > {
    > printf("%d",sizeof(printf());
    > }
    > the output was : 2
    > how come the output is 2? actually what is the property of sizeof()
    > and printf()?are there any return types for the two functions? hoping
    > for the positive responses.. thanks a lot in advance.


    There should be no cause for surprise, no matter what
    output is or is not produced. You should not be surprised
    if the code makes demons fly out of your nose, because it
    invokes undefined behavior not once, not twice, but thrice:

    - The main() function is not `void'

    - It is U.B. to call a variadic function like printf()
    without a prototype in scope

    - It is U.B. to pass a non-`int' to the "%d" specifier
    (On some "exotic" systems where `size_t' is narrower
    than `int' this might be all right, so I really should
    say that whether the behavior is defined or undefined
    is implementation-defined.)

    .... and on top of all that, the fate of a newline-less "line"
    at the end of a stream of output is implementation-defined.

    (Three instances of U.B. and one of I.D.B., or possibly
    two U.B. and two I.D.B. -- all in just four lines, two of
    which consist only of braces. Is this density of errors --
    a density of densness, one might say -- a candidate for the
    Guinness Book? Sadly, I fear not.)

    Here's a cleaned-up version:

    #include <stdio.h> /* for printf() prototype */
    int main(void) {
    printf ("%d\n", /* note newline */
    (int)sizeof printf()); /* note type coercion */
    return 0; /* required in C90, optional in C99 */
    }

    Now, you may still be puzzled about the output of the
    cleaned-up version of your bletcherous code. Much will become
    clearer if you ponder two questions:

    - What does the `sizeof' operator do with its argument?

    - What type does the printf() function return?

    Answer these, Grasshopper, and you will be on the path
    to enlightenment -- but if you keep on writing code like the
    sample you provided here, it means you're on the right path
    but walking in the wrong direction.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 24, 2005
    #6
  7. raghu a écrit :
    > i'm surprised at the output of the following code. compiled in turbo C
    >
    > void main()
    > {
    > printf("%d",sizeof(printf());
    > }


    This code invokes an undefined behaviour

    - The type returned by main() is int.
    - An explicit valid value must be returned.
    "%d" expects an int and the sizeof operator returns a size_t.

    > the output was : 2


    Supposing the code was fixed, this 2 is the size of the type returned by
    printf (actually int) on your machine.

    > how come the output is 2? actually what is the property of sizeof()


    Like your C-book said, the sizeof operator return the size of an object
    or of a type (with parens) in number of bytes. The type of the returned
    value is size_t.

    > and printf()?


    printf() is a function returning int. Details belong to your C-book.

    > are there any return types for the two functions?


    sizeof is not a function but a C-unary-operator
    --
    A+

    Emmanuel Delahaye
    Emmanuel Delahaye, Dec 24, 2005
    #7
  8. raghu

    Chris Hills Guest

    In article <dojjl4$qeo$-infra.bt.com>, Richard
    Heathfield <> writes
    >raghu said:
    >
    >> i'm surprised at the output of the following code. compiled in turbo C
    >>
    >> void main()

    >
    >Don't be. Any program which gives the wrong return type for main() exhibits
    >undefined behaviour, so any behaviour, surprising or not, is permissible as
    >far as the C language is concerned.


    void main (void) *may* be permissible with turbo C as I think it could
    turn out code that would run with out an OS. Somewhere I have a ROM
    kit for Turbo-C.

    In general (ie unless explicitly specified as self hosted) it MUST be
    int main ([args])


    >
    >> {
    >> printf("%d",sizeof(printf());

    >
    >Calling printf without a correct prototype in scope invokes undefined
    >behaviour. Calling printf without any arguments invokes undefined
    >behaviour.
    >
    >Start your program like this:
    >
    >#include <stdio.h>
    >
    >int main(void)
    >
    >...and then try your program again, first fixing any diagnostics issued by
    >your compiler.
    >
    >


    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Dec 24, 2005
    #8
  9. Eric Sosman wrote:
    > raghu wrote:
    >
    >> i'm surprised at the output of the following code. compiled in turbo C
    >>
    >> void main()
    >> {
    >> printf("%d",sizeof(printf());
    >> }
    >> the output was : 2
    >> how come the output is 2? actually what is the property of sizeof()
    >> and printf()?are there any return types for the two functions? hoping
    >> for the positive responses.. thanks a lot in advance.

    >
    > ...
    > ...
    > Here's a cleaned-up version:
    >
    > #include <stdio.h> /* for printf() prototype */
    > int main(void) {
    > printf ("%d\n", /* note newline */
    > (int)sizeof printf()); /* note type coercion */
    > return 0; /* required in C90, optional in C99 */
    > }


    Well, this does not compile on gcc given -ansi -pedantic.
    Perhaps printf("") instead of printf() would be better?
    Giannis Papadopoulos, Dec 24, 2005
    #9
  10. raghu wrote:
    > i'm surprised at the output of the following code. compiled in turbo C
    >
    > void main()
    > {
    > printf("%d",sizeof(printf());
    > }
    > the output was : 2
    > how come the output is 2?


    Since you define main to have an invalid return type (main returns an
    int) and leave out the declaration of printf (corrected by including
    <stdio.h>), and fail to terminate the last line of output with an
    end-of-line character, any action this program performs is completely
    random.
    Martin Ambuhl, Dec 24, 2005
    #10
  11. raghu

    Eric Sosman Guest

    Giannis Papadopoulos wrote:
    > Eric Sosman wrote:
    >> Here's a cleaned-up version:
    >>
    >> #include <stdio.h> /* for printf() prototype */
    >> int main(void) {
    >> printf ("%d\n", /* note newline */
    >> (int)sizeof printf()); /* note type coercion */
    >> return 0; /* required in C90, optional in C99 */
    >> }

    >
    > Well, this does not compile on gcc given -ansi -pedantic.
    > Perhaps printf("") instead of printf() would be better?


    Interesting. The constraint of 6.5.2.2/2 requires that
    the number and type of the provided function arguments agree
    with those of the function prototype, so it seems a diagnostic
    is required. On the other hand, the erroneous call produces
    no executable code and cannot possibly cause harm (and the
    compiler knows this). The question comes down to whether
    6.5.2.2/2 applies to function calls that appear in the code
    (even if only in a formal sense) or should be taken as applying
    to function calls that are executed at run-time.

    Full employment for language lawyers, I guess. In any
    case, good catch. By my count, the O.P.'s code contains five
    known (now) errors in just four lines, only two non-trivial.
    Maybe the Guinness people should be alerted after all.

    --
    Eric Sosman, Dec 24, 2005
    #11
  12. raghu

    Randy Howard Guest

    Richard Heathfield wrote
    (in article
    <dojjvj$qeo$-infra.bt.com>):

    > Chris Hills said:
    >
    >> In article <>,
    >> raghu <> writes
    >>> i'm surprised at the output of the following code. compiled in turbo C
    >>>
    >>> void main()
    >>> {
    >>> printf("%d",sizeof(printf());
    >>> }
    >>> the output was : 2
    >>> how come the output is 2? actually what is the property of sizeof()
    >>> and printf()?are there any return types for the two functions? hoping
    >>> for the positive responses.. thanks a lot in advance.

    >>
    >> Ignoring that it should have been
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> printf("%d",sizeof(printf());

    >
    > No, %d doesn't match size_t.


    MISRA loophole? :)


    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Dec 24, 2005
    #12
  13. raghu

    pemo Guest

    "raghu" <> wrote in message
    news:...
    > i'm surprised at the output of the following code. compiled in turbo C
    >
    > void main()
    > {
    > printf("%d",sizeof(printf());
    > }
    > the output was : 2
    > how come the output is 2? actually what is the property of sizeof()
    > and printf()?are there any return types for the two functions? hoping
    > for the positive responses.. thanks a lot in advance.


    This reminds me of an 'incident' [a very long time ago now!] when a fellow
    lecturer of mine [who's now a *very* highly respected prof. of electrical
    engineering] said to me ... "you know what, there's a major bug in the
    compiler! A student of mine had the following [see below] and the damn
    compiler said it was ok!!!!

    Boy, it took me ages to find out what was *wrong*.

    I should report it as a major bug!!!"

    ===

    Here's the line of code

    void someFunc(void)
    {
    }

    int main(void)
    {
    ...
    ...
    /* error - damn s**t compiler [his implicit annotation] */
    someFunc;
    ...
    ...
    return you_know_what;
    }
    pemo, Dec 24, 2005
    #13
  14. raghu

    Jordan Abel Guest

    On 2005-12-24, pemo <> wrote:
    >
    > "raghu" <> wrote in message
    > news:...
    >> i'm surprised at the output of the following code. compiled in turbo C
    >>
    >> void main()
    >> {
    >> printf("%d",sizeof(printf());
    >> }
    >> the output was : 2
    >> how come the output is 2? actually what is the property of sizeof()
    >> and printf()?are there any return types for the two functions? hoping
    >> for the positive responses.. thanks a lot in advance.

    >
    > This reminds me of an 'incident' [a very long time ago now!] when a fellow
    > lecturer of mine [who's now a *very* highly respected prof. of electrical
    > engineering] said to me ... "you know what, there's a major bug in the
    > compiler! A student of mine had the following [see below] and the damn
    > compiler said it was ok!!!!
    >
    > Boy, it took me ages to find out what was *wrong*.
    >
    > I should report it as a major bug!!!"
    >
    >===
    >
    > Here's the line of code
    >
    > void someFunc(void)
    > {
    >}
    >
    > int main(void)
    > {
    > ...
    > ...
    > /* error - damn s**t compiler [his implicit annotation] */
    > someFunc;
    > ...
    > ...
    > return you_know_what;
    >}


    What's wrong with that? It evaluates the address of the function and
    then throws it away. [of course, if the compiler makes it _call_ the
    function, that's a problem]
    Jordan Abel, Dec 24, 2005
    #14
  15. Eric Sosman <> writes:
    > Giannis Papadopoulos wrote:
    >> Eric Sosman wrote:
    >>> Here's a cleaned-up version:
    >>>
    >>> #include <stdio.h> /* for printf() prototype */
    >>> int main(void) {
    >>> printf ("%d\n", /* note newline */
    >>> (int)sizeof printf()); /* note type coercion */
    >>> return 0; /* required in C90, optional in C99 */
    >>> }

    >> Well, this does not compile on gcc given -ansi -pedantic.
    >> Perhaps printf("") instead of printf() would be better?

    >
    > Interesting. The constraint of 6.5.2.2/2 requires that
    > the number and type of the provided function arguments agree
    > with those of the function prototype, so it seems a diagnostic
    > is required. On the other hand, the erroneous call produces
    > no executable code and cannot possibly cause harm (and the
    > compiler knows this). The question comes down to whether
    > 6.5.2.2/2 applies to function calls that appear in the code
    > (even if only in a formal sense) or should be taken as applying
    > to function calls that are executed at run-time.


    It's a constraint, requiring a compile-time diagnostic. Of course it
    applies at compilation time.

    All three printf calls in the following violate the same constraint:

    #include <stdio.h>
    #include <time.h>
    int main(void)
    {
    sizeof printf();
    if (0) {
    printf();
    }
    if (time(NULL) < 1135455446) {
    printf();
    }
    }

    In all three cases, the compiler can potentially determine that the
    call is never executed (for the last, it would have to take advantage
    of its knowledge about the representation of time_t and assume that
    the system clock is correct). This doesn't relieve the compiler of
    its obligation to generate a diagnostic, any more than it could omit a
    diagnostic for a conditional syntax error:

    if (0) {
    printf("hello, world\n) /* missing semicolon */
    }

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Dec 24, 2005
    #15
  16. Emmanuel Delahaye <> writes:
    > raghu a écrit :
    >> i'm surprised at the output of the following code. compiled in turbo C
    >> void main()
    >> {
    >> printf("%d",sizeof(printf());
    >> }

    >
    > This code invokes an undefined behaviour
    >
    > - The type returned by main() is int.
    > - An explicit valid value must be returned.
    > "%d" expects an int and the sizeof operator returns a size_t.


    But printf is never actually called, and without a visible prototype
    the compiler (at least for C90) assumes that printf() is a function
    taking unknown arguments and returning int.

    In fact, the following returns sizeof(int) to the calling environment:

    int main(void)
    {
    return sizeof unknown_function();
    }

    On the system where I just tried it, since unknown_function() isn't
    actually called, the implementation doesn't attempt to link it into
    the executable. I'm not sure whether this is allowed (and it's too
    bizarre for me to care very much).

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Dec 24, 2005
    #16
  17. raghu

    Skarmander Guest

    pemo wrote:
    > "raghu" <> wrote in message
    > news:...
    >
    >>i'm surprised at the output of the following code. compiled in turbo C
    >>
    >>void main()
    >>{
    >>printf("%d",sizeof(printf());
    >>}
    >>the output was : 2
    >>how come the output is 2? actually what is the property of sizeof()
    >>and printf()?are there any return types for the two functions? hoping
    >>for the positive responses.. thanks a lot in advance.

    >
    >
    > This reminds me of an 'incident' [a very long time ago now!] when a fellow
    > lecturer of mine [who's now a *very* highly respected prof. of electrical
    > engineering] said to me ... "you know what, there's a major bug in the
    > compiler! A student of mine had the following [see below] and the damn
    > compiler said it was ok!!!!
    >
    > Boy, it took me ages to find out what was *wrong*.
    >
    > I should report it as a major bug!!!"
    >
    > ===
    >
    > Here's the line of code
    >
    > void someFunc(void)
    > {
    > }
    >
    > int main(void)
    > {
    > ...
    > ...
    > /* error - damn s**t compiler [his implicit annotation] */
    > someFunc;


    Almost all compilers I've seen have an option for issuing a warning when a
    statement has no effect, typically enabled with a host of other useful
    warnings you wouldn't want to do without. Aside from a better grasp of the
    language, a better understanding of the compiler wouldn't hurt either.

    S.
    Skarmander, Dec 24, 2005
    #17
  18. On Sat, 24 Dec 2005 20:26:26 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >On the system where I just tried it, since unknown_function() isn't
    >actually called, the implementation doesn't attempt to link it into
    >the executable. I'm not sure whether this is allowed


    I suspect the 'as if' rule would apply. Since the code behaves as if
    the function didn't do anything other than return an unspecified int,
    the compiler is allowed to completely optimise it out and replace it
    by an unspecified int. OTOH I'd expect some sort of complaint.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 24, 2005
    #18
  19. raghu

    pemo Guest

    "Jordan Abel" <> wrote in message
    news:...
    > On 2005-12-24, pemo <> wrote:
    >>
    >> "raghu" <> wrote in message
    >> news:...
    >>> i'm surprised at the output of the following code. compiled in turbo C
    >>>
    >>> void main()
    >>> {
    >>> printf("%d",sizeof(printf());
    >>> }
    >>> the output was : 2
    >>> how come the output is 2? actually what is the property of sizeof()
    >>> and printf()?are there any return types for the two functions? hoping
    >>> for the positive responses.. thanks a lot in advance.

    >>
    >> This reminds me of an 'incident' [a very long time ago now!] when a
    >> fellow
    >> lecturer of mine [who's now a *very* highly respected prof. of electrical
    >> engineering] said to me ... "you know what, there's a major bug in the
    >> compiler! A student of mine had the following [see below] and the damn
    >> compiler said it was ok!!!!
    >>
    >> Boy, it took me ages to find out what was *wrong*.
    >>
    >> I should report it as a major bug!!!"
    >>
    >>===
    >>
    >> Here's the line of code
    >>
    >> void someFunc(void)
    >> {
    >>}
    >>
    >> int main(void)
    >> {
    >> ...
    >> ...
    >> /* error - damn s**t compiler [his implicit annotation] */
    >> someFunc;
    >> ...
    >> ...
    >> return you_know_what;
    >>}

    >
    > What's wrong with that? It evaluates the address of the function and
    > then throws it away. [of course, if the compiler makes it _call_ the
    > function, that's a problem]


    Um, yes, I know!
    pemo, Dec 25, 2005
    #19
  20. >i'm surprised at the output of the following code. compiled in turbo C
    >
    >void main()
    >{
    >printf("%d",sizeof(printf());
    >}
    >the output was : 2


    Given that <stdio.h> was included, the return type of printf is int,
    and an int in turbo C is a 16-bit entity.

    >how come the output is 2? actually what is the property of sizeof()


    Try again on a modern compiler and you should get 4.



    Jan Engelhardt
    --
    Jan Engelhardt, Dec 25, 2005
    #20
    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. Derek
    Replies:
    7
    Views:
    24,305
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,391
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    545
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    489
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page