#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
scanf("%d%d"+scanf("%d%d",&i,&j));
printf("%d %d",i,j);
getch();
return 0;
}
Please put your question in the body of your post, even if it just
duplicates your subject.
The program works only if you are very careful with your inputs. In
other cases, it invokes undefined behavior which, among other things,
means there is no explanation.
The inner call to scanf accepts at most two integers. If you do not
enter two integers, at least one of the two integers printed has an
indeterminate value and evaluating it causes undefined behavior.
If you do enter two integers, they must be of the form
int_1 int_2 ??? <ENTER>
where the quantity and choice of white space characters are irrelevant
as long as there is one between the two integers and one after the
second integer. The ??? represent additional optional data which will
not be processed by this call to scanf. The use of the enter key
assumes you are using a keyboard but your system may have other
methods of determining that the input is complete.
scanf will stop processing the input at the first character that is
not part of int_2 and this character will be made available to the
next attempt to read from stdin. At this point, both i and j have
been set and scanf will return 2, indicating that two values were
successfully converted.
The argument of the outer call to scanf is a pointer value formed by
the pointer arithmetic expression. The left operand of the addition
operator is the address of the string literal which is stored in your
program as an unnamed static array of char. The array consists of the
five characters '%', 'd', '%', 'd', and '\0'. The address of this
array is the address of the first '%'. The right operand is the int
returned by scanf, which we are assuming is 2. The result of this
addition is the address of the second '%'. Therefore, the effective
argument passed to scanf is "%d".
Thus, the outer call to scanf will accept at most one integer. One of
the features of the %d conversion specifier is that it will skip over
white space looking for the start of the integer. Assuming you did
not enter any ??? data, it will skip over all the white space
following int_2, including the '\n' generated by the enter key. It
will then wait for you to enter more data. Presumably, you hit enter
again. But this is just more white space to ignore. At this point,
you must force end of file or enter a non-numeric character followed
by the enter key. Either of these actions will cause the conversion
to fail and scanf to return. You should have no trouble with the
remaining code.
If you enter any numeric data when scanf is waiting, or if you entered
a characters sequence starting with a number after int_2, then this
outer call to scanf will attempt to convert that data into an int
value and store it somewhere. That somewhere would be specified by
the next argument to scanf but there is no other argument. This also
results in undefined behavior.
In summary, the program may have worked but if it did so it is only
because you cleverly, or luckily, avoided all the pitfalls.
It is also possible that the program did not work properly but only
appeared to. Improper input during either call to scanf results in
undefined behavior. One of the more undesirable manifestations of
undefined behavior is to appear to do what is expected, this time.