Scanf Behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sajjanharudit@gmail.com

    #1

    Scanf Behaviour

    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??

  • usenet@zevv.nl

    #2
    Re: Scanf Behaviour

    > Can anyone explain me what is happening in the following code:[color=blue]
    >
    > #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??[/color]

    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

    Comment

    • sajjanharudit@gmail.com

      #3
      Re: Scanf Behaviour

      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???

      Comment

      • usenet@zevv.nl

        #4
        Re: Scanf Behaviour

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

        You tried *what* ?
        [color=blue]
        > 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???[/color]

        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.
        [color=blue][color=green]
        >> scanf("%d %d" + scanf("%d %d",&i,&j));[/color][/color]

        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.
        [color=blue][color=green]
        >> printf("%d %d\n");[/color][/color]

        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.
        [color=blue][color=green]
        >> }[/color][/color]

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


        Ico


        --
        :wq
        ^X^Cy^K^X^C^C^C ^C

        Comment

        • sajjanharudit@gmail.com

          #5
          Re: Scanf Behaviour

          >>From this point on,[color=blue][color=green]
          >>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.[/color][/color]
          [color=blue][color=green][color=darkred]
          >>>> printf("%d %d\n");[/color][/color][/color]
          [color=blue][color=green]
          >>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. "[/color][/color]

          ok got this point..
          [color=blue][color=green]
          >> "The expression[/color][/color]
          [color=blue][color=green]
          >> "%d %d" + 2[/color][/color]
          [color=blue][color=green]
          >>results in " %d" "[/color][/color]

          but didn't get how this happened??

          Comment

          • usenet@zevv.nl

            #6
            Re: Scanf Behaviour

            Hello again,
            [color=blue][color=green][color=darkred]
            >>>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.[/color][/color]
            >[color=green][color=darkred]
            >>>>> printf("%d %d\n");[/color][/color]
            >[color=green][color=darkred]
            >>>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. "[/color][/color]
            >
            > ok got this point..
            >[color=green][color=darkred]
            >>> "The expression[/color][/color]
            >[color=green][color=darkred]
            > >> "%d %d" + 2[/color][/color]
            >[color=green][color=darkred]
            >>>results in " %d" "[/color][/color]
            >
            > but didn't get how this happened??[/color]

            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

            Comment

            • sajjanharudit@gmail.com

              #7
              Re: Scanf Behaviour

              Thank you... its clear now.

              Comment

              • Keyser Soze

                #8
                Re: Scanf Behaviour

                <sajjanharudit@ gmail.com> wrote in message
                news:1135937858 .882262.205580@ g49g2000cwa.goo glegroups.com.. .[color=blue]
                > 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??
                >[/color]
                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:1135939219 .629775.141890@ g14g2000cwa.goo glegroups.com


                Comment

                Working...