Hello All,
I am unable to understand this piece of code

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

I got this question from a C problem set. I run this code and I have to give the i/p 3 times. After this the code crashes. My questions are
1. What is the relevance of the + sign
2. Why do I have to give the i/p 3 times. Why not 4 ?
3. Why does the program crash ?

Thank You

scanf returns int , a number of processed fields. The inner scanf would return 2. The argument of the outer scanf becomes "%d %d" + 2 , that is " %d" - that's why it needs just one input. It crashes because the outer scanf does not have an argument to scan the value into. The

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

will not crash.

My questions are:
Is this a bad cut & paste?
If not:
Who wants you to deal with this piece of trash code?
When will you ever do something like this (and keep your job)?
Why do people insist on teaching this crap?

Comments
Great unanswered questions of our time....

> I got this question from a C problem set.
Which one, I feel like complaining to them about posting crap.

@Salem
This is one of those problem sets which prepares you for the job interviews.

If this came from a job-interview, you do not want to be working for this company. When they give you this just put on a disgusted face and walk away :)

Check out this link...This is posted as a job interview question

http://www.geekinterview.com/question_details/20527

This is the question I am trying to solve..... Still cannot understand it

If this came from a job-interview, you do not want to be working for this company. When they give you this just put on a disgusted face and walk away :)

I wish I could afford such luxuries..... Till then nose to the grindstone....Have to keep solving such stupid questions

To help put this to rest, try running this code

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i, j;
	fprintf(stdout, "----  %s  ----\n", "%d %d"+scanf("%d %d", &i, &j));
	return 0;
}

You should get output similar to this:

---- %d ----

Which is what is passed to the second scanf..

Edited 6 Years Ago by gerard4143: n/a

> This is one of those problem sets which prepares you for the job interviews.
After half a dozen clicks on that site (or should I say, "what a sight!"), it's disgusting what rubbish people spout as being good answers. There's no end of "Well my compiler does this, therefore it's the right answer".

Prepare - what a laugh.
Anyone armed with that information will either fail the interview or get confused on the first programming assignment.

Comments
That site is certainly a laugh!!!

Why does scanf("%d %d"+2) become scanf("%d") ?

Would the following be of any help?

#include <stdio.h>
int main(void)
{
    char const * p = "invisible text" + 10;
    printf("%s", p);

    return 0;
}

When they give you this just put on a disgusted face and walk away

Don't you think that they will feel that aspirant has chickened out after seeing the code

This article has been dead for over six months. Start a new discussion instead.