Scanf Behaviour

Discussion in 'C Programming' started by sajjanharudit@gmail.com, Dec 30, 2005.

  1. Guest

    Can anyone explain me what is happening in the following code:

    #include<stdio.h>

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

    It takes 3 inputs and outputs the first two... how come??
     
    , Dec 30, 2005
    #1
    1. Advertising

  2. Guest

    > Can anyone explain me what is happening in the following code:
    >
    > #include<stdio.h>
    >
    > int main()
    > {
    > int i,j;
    > scanf("%d %d" + scanf("%d %d",&i,&j));
    > printf("%d %d\n");
    > }
    >
    > It takes 3 inputs and outputs the first two... how come??


    Yes, truely amazing that it even outputs two numbers. It just made my system
    crash and burn.

    What *did* you expect from this piece of code ?

    Ico

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    , Dec 30, 2005
    #2
    1. Advertising

  3. Guest

    Hey I tried it on the VC++ 6.0 and it ran fine without any crashes...
    and secondly I really didn't expect anything from the code... the only
    thing I expect is an answer that why is it doing so???
     
    , Dec 30, 2005
    #3
  4. Guest

    > Hey I tried it on the VC++ 6.0 and it ran fine without any crashes...

    You tried *what* ?

    > and secondly I really didn't expect anything from the code... the only
    > thing I expect is an answer that why is it doing so???


    Why is *what* doing so ?

    You probably heard it before : please, please, please! quote context when
    replying to other posts.

    *I* will now quote parts of your previous message, since you didn't.

    To answer your question : Your program is jost mostly wrong.

    >> scanf("%d %d" + scanf("%d %d",&i,&j));


    The inner-most scanf is evaluated first; this reads to integers into i and
    j, and returns the value '2'. That's just fine.

    The outer scanf gets the format string "%d %d" *plus* the return value of
    the inner scanf. The expression

    "%d %d" + 2

    results in

    " %d"

    so the second scanf will try to read one integer. But you didn't tell it
    where to store that integer, which is very very wrong. From this point on,
    there is no telling what your program will do: it will probably try to store
    a few bytes somewhere in memory, which may, or may not, set your computer on
    fire.

    >> printf("%d %d\n");


    Here more funny things will happen. You are telling printf to output two
    integers, but you do not tell it *what* to print. Also undefined behaviour.

    >> }


    And one more : main() is supposed to return an integer. Do just that.


    Ico


    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    , Dec 30, 2005
    #4
  5. Guest

    >>From this point on,
    >>there is no telling what your program will do: it will probably try to store
    >>a few bytes somewhere in memory, which may, or may not, set your computer on
    >>fire.


    >>>> printf("%d %d\n");


    >>Here more funny things will happen. You are telling printf to output two
    >>integers, but you do not tell it *what* to print. Also undefined behaviour. "


    ok got this point..

    >> "The expression


    >> "%d %d" + 2


    >>results in " %d" "


    but didn't get how this happened??
     
    , Dec 30, 2005
    #5
  6. Guest

    Hello again,

    >>>From this point on, there is no telling what your program will do: it
    >>>will probably try to store a few bytes somewhere in memory, which may, or
    >>>may not, set your computer on fire.

    >
    >>>>> printf("%d %d\n");

    >
    >>>Here more funny things will happen. You are telling printf to output two
    >>>integers, but you do not tell it *what* to print. Also undefined behaviour. "

    >
    > ok got this point..
    >
    >>> "The expression

    >
    > >> "%d %d" + 2

    >
    >>>results in " %d" "

    >
    > but didn't get how this happened??


    The character constant "%d %d" is actually just a pointer. Your compiler and
    linker pick up the string between the quotes and put it in the executable.

    When you run the program, this string is loaded somewehere in memory, and
    the *location* of this stringin memory (the pointer!) is passed to scanf.

    When you add 2 to a character pointer, the pointer will just point two
    bytes ahead of the original location. So in memory, the string will still be
    "%d %d", but the *pointer* points somewhere halfway this string, to the
    " %d" part. Which is a valid format string for scanf(), which will just try
    to do wat you are asking : read one integer.

    Hope this is clear.

    Ico


    By the way: thank you for quoting this time
    (you see Kenny, some people *do* learn :) )

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    , Dec 30, 2005
    #6
  7. Guest

    Thank you... its clear now.
     
    , Dec 30, 2005
    #7
  8. Keyser Soze Guest

    <> wrote in message
    news:...
    > Can anyone explain me what is happening in the following code:
    >
    > #include<stdio.h>
    >
    > int main()
    > {
    > int i,j;
    > scanf("%d %d" + scanf("%d %d",&i,&j));
    > printf("%d %d\n");
    > }
    >
    > It takes 3 inputs and outputs the first two... how come??
    >

    The program look like intentional obfuscation.

    The 'printf' call appears to be exploiting a benign but undefined behavior
    in the VC6 method of creating local variable on the stack.

    When the 'printf' function is called the 'i' and 'j' variable are in the
    "right" place on the stack to be accepted as the parameters that should have
    been part of the 'printf' statement.

    If you add more local variables the behavior of this program may change.

    -----
    Note to reader:

    The reference to VC6 is from one of the OPs other posts in this thread.
    news:
     
    Keyser Soze, Dec 30, 2005
    #8
    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. Rob Thorpe

    Correct behaviour of scanf and sscanf

    Rob Thorpe, Mar 14, 2005, in forum: C Programming
    Replies:
    6
    Views:
    467
    Dan Pop
    Mar 15, 2005
  2. Lalatendu Das

    confused about behaviour of scanf

    Lalatendu Das, Dec 21, 2005, in forum: C Programming
    Replies:
    33
    Views:
    1,041
    Dave Thompson
    Jan 4, 2006
  3. =?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,520
    those who know me have no need of my name
    Apr 3, 2006
  4. Argento

    scanf(), ungetc() behaviour.

    Argento, Mar 3, 2006, in forum: C Programming
    Replies:
    62
    Views:
    1,592
  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:
    706
    Richard Bos
    May 2, 2006
Loading...

Share This Page