what's this

Discussion in 'C Programming' started by vicky, Feb 24, 2007.

  1. vicky

    vicky Guest

    why is the output of the following command is 4 4 5.

    main()
    {
    int a=2;
    printf("%d",printf("\n%d %d",a,a,a);
    getch();
    }

    i did not understand what these two printf in a command signifies.
    please help me
     
    vicky, Feb 24, 2007
    #1
    1. Advertisements


  2. Some mistakes there. This is what you mean:

    #include <stdio.h>

    int main(){
    int a = 2;
    printf("%d\n", printf("%d %d ", a, a));
    return 0;
    }


    ...and that will print 2 2 4. Now for the reason for that see `man 3
    printf' ( http://www.die.net/doc/linux/man/man3/printf.3.html ) which
    states:

    int printf(const char *format, ...);


    ...which means printf is a function call that returns a value of type int.
    It further states:

    Return value
    Upon successful return, these functions return the number of characters
    printed (not including the trailing ’\0’ used to end output to
    strings). ... If an output error is encountered, a negative value is
    returned.


    Now take a look:

    #include <stdio.h>

    int a(int x){
    return x * x;
    }

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


    ...that will print 256 just as this will print 256:

    #include <stdio.h>

    int a(int x){
    return x * x;
    }

    int main(){
    int tmp1 = a(2);
    int tmp2 = a(tmp1);
    int tmp3 = a(tmp2);
    printf("%d\n", tmp3);
    return 0;
    }


    ...get it?
     
    =?iso-8859-1?q?Lars_Rune_N=F8stdal?=, Feb 24, 2007
    #2
    1. Advertisements

  3. vicky

    Bill Pursell Guest


    Perhaps it will be clearer if you see the similarity
    between your code and this:

    #include <stdio.h>
    #include <stdlib.h>

    int
    main(void)
    {
    int a=2;
    int b;

    b = printf( "\n%d %d", a, a );
    printf("%d", b);

    return EXIT_SUCCESS;
    }
     
    Bill Pursell, Feb 24, 2007
    #3
  4. vicky

    Flash Gordon Guest

    vicky wrote, On 24/02/07 14:53:
    It isn't on my system. On my system the output is a compilation error.
    Perhaps if you posted your actual code (copy and past, do not retype)
    then a better suggestion could be given. Although I suspect even then
    the answer will be "because it is since you invoked undefined behaviour".

    A few general comment though, main returns an int and it is better to be
    explicit about that (it is required in the latest C standard) and it is
    also better to be explicit about not taking any parameters. Having
    realised main returns an int you should then return one! Calling printf
    requires a prototype, so you should include <stdio.h>. Standard C does
    not have a getch function, it has a getchar function (prototype in
    stdio.h) which probably does what you want, although learning to drive
    your IDE would be better so you do not have to make the program pause.
    So a slightly improved form would be:

    #include <stdio.h>

    int main(void)
    {
    int a=2;
    /* Some printf calls, but I'm not sure what you intended */
    getchar(); /* better to remove this, but I'll leave it in for you */
    return 0;
    }
     
    Flash Gordon, Feb 24, 2007
    #4
  5. This is essentially FAQs 3.1 & 3.2 & 3.3


    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Feb 24, 2007
    #5
  6. vicky

    CBFalconer Guest

    Perfectly legitimate. Any behaviour whatsoever is allowed when you
    invoke undefined behavior. On another system you might invoke
    WWIII, or find demons in your snot.

    Lack of #include <stdio.h>
    Failure to include a '\n' in the output, or a fflush(stdout).
    getch undefined
    main returns int. Say so and do so.

    In addition, blanks are no longer on allocation, so you can use
    them without fouling the supply lines. They are quite cheap too.
     
    CBFalconer, Feb 24, 2007
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.