strange scanf

Discussion in 'C Programming' started by Shivanand Kadwadkar, Jan 20, 2011.

  1. I was reading a forum there i found following program.

    main()
    {
    int i, j;
    scanf("%d %d"+scanf("%d %d", &i, &j));
    printf("%d %d", i, j);
    }

    the above program is working fine.
    Input:
    4
    5
    6
    output
    6 5

    this meas i=6 and j=5.

    any idea why i=6 and j=5 ( i was thinking i should be i=5 and j=6)and
    what the "+" is doing in scanf?

    Thanks for sharing comments.
     
    Shivanand Kadwadkar, Jan 20, 2011
    #1
    1. Advertising

  2. Shivanand Kadwadkar <> writes:

    > I was reading a forum there i found following program.
    >
    > main()
    > {
    > int i, j;
    > scanf("%d %d"+scanf("%d %d", &i, &j));
    > printf("%d %d", i, j);
    > }
    >
    > the above program is working fine.


    The program's meaning is undefined for at least two reasons. That means
    that it is bound to work fine: since the program has no defined meaning,
    anything it might do is as good as anything else.

    > Input:
    > 4
    > 5
    > 6
    > output
    > 6 5


    I get something else (a segmentation fault) but that's also "working
    fine".

    A web search suggests this is an interview question, though where
    answers are offered, none are correct.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Jan 20, 2011
    #2
    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. Snubis

    Re: safe scanf( ) or gets

    Snubis, Jan 2, 2004, in forum: C++
    Replies:
    0
    Views:
    393
    Snubis
    Jan 2, 2004
  2. JustSomeGuy

    string.scanf?

    JustSomeGuy, Jun 4, 2004, in forum: C++
    Replies:
    5
    Views:
    8,178
    Jorge Rivera
    Jun 6, 2004
  3. Harvey Twyman
    Replies:
    8
    Views:
    574
    August Derleth
    Oct 25, 2003
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,409
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    684
    Richard Bos
    May 2, 2006
Loading...

Share This Page