-3
#include <stdio.h>

int main(){
    int l,m,q,j;
    char option;
    scanf("%d",&q);
    printf("%d\n",q);
    for(j=0;j<q;j++){
        scanf("%c %d %d",&option,&l,&m);
        printf("%c %d %d",option,l,m);
    }
    return 0;
}

Output:

3(Input)
3
C 1 4(Input)

 0 -374066224C 1 4

What is wrong with the above code? It is not giving the expected output.

3
  • What is your expected output? Commented Aug 10, 2012 at 13:17
  • What is the expected output? Did you try to debug it? Seems lie a homework-related problem... Commented Aug 10, 2012 at 13:17
  • @Levon Its not a homework, I tried debugging using gdb but no use. The expected output should be the same as input Commented Aug 10, 2012 at 13:19

2 Answers 2

9

There is still a newline character in the input stream from the initial scanf() (and subsequent scanf()s): this will be assigned to option and the subsequent int assignments will fail as C is not an int, meaning l and m are uninitialised int variables.

To skip it add a leading space character to the format specifier of the scanf() within the for loop:

scanf(" %c %d %d",&option,&l,&m);
    /* ^ */

The return value of scanf() is the number of successful assignments made: check it to ensure 3 assignments are made.

if (3 == scanf(" %c %d %d",&option,&l,&m))
{
    /* Valid input. */
}
Sign up to request clarification or add additional context in comments.

4 Comments

That solved the problem but could you explain why there is a newline character please
@sachinirukula, when the user enters the number they type 3 and hit return. The return inserts a new line character into the input stream. The new line character is not consumed by the scanf(), leaving it in the input stream.
@sachinirukula The reason why goes back to the ancient things called typewriters. When you pressed their equivalent to enter, the machine did a carriage return (back to the left-most side of the paper) and then it changed to a new line. Windows works the same. Windows text files even end each row with the ASCII symbols '\r' (ASCII 13) '\n' (ASCII 10), they are called carriage return and line feed respectively.
For the record, the reason why the initial space solves the problem is this section of the standard: 7.21.6.2/5 (scanf functions) "A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails."
2

Every time you prompt the user for an input, they will write it and then press enter. Enter leaves a line feed character in the input buffer. You must discard it before asking for a character input, or it will end up in your character variable.

The easy but blunt solution is this:

scanf("%d",&q); getchar();
printf("%d\n",q);
for(j=0;j<q;j++){
    scanf("%c %d %d",&option,&l,&m); getchar();
    printf("%c %d %d",option,l,m);
}

(I'm pretty sure there is a C FAQ for this somewhere but I can't find the link.)

1 Comment

hmjd's solution is far prettier, so I would go with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.