Scanf Behaviour

S

sajjanharudit

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

usenet

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
 
S

sajjanharudit

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

usenet

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.

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.

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
 
U

usenet

Hello again,
ok got this point..


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 :) )
 
K

Keyser Soze

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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top