954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unable to understand this code

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

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

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.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

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?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

uh-ooh you aroused the 'C decency committee'

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

@nezachem

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

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 
@Salem This is one of those problem sets which prepares you for the job interviews.

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
@Salem This is one of those problem sets which prepares you for the job interviews.

Check out this link...This is posted as a job interview question http://www.geekinterview.com/question_details/20527

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

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

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
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;
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 
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

xavier666
Junior Poster
173 posts since Sep 2009
Reputation Points: 71
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You