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

Recommended Answers

All 13 Replies

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?

commented: Great unanswered questions of our time.... +19

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

uh-ooh you aroused the 'C decency committee'

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

@nezachem

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

@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..

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

commented: That site is certainly a laugh!!! +9

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.