Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   strange scanf (http://www.velocityreviews.com/forums/t742324-strange-scanf.html)

Shivanand Kadwadkar 01-20-2011 04:25 PM

strange scanf
 
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.

Ben Bacarisse 01-20-2011 06:23 PM

Re: strange scanf
 
Shivanand Kadwadkar <shivanand.kadwadkar@gmail.com> 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.


All times are GMT. The time now is 04:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.