FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


scanf and printf in c





davidv
Code:
int main(void) {
    char cmd;
    int row, col;

    scanf("%c %d %d", &cmd, &row, &col);
    printf("%c %d %d\n", cmd, row, col);

    scanf("%c %d %d", &cmd, &row, &col);
    printf("%c %d %d", cmd, row, col);

    return 0;
}                                                                                         


The result is:

Code:
g 100 100
g 100 100
b 50 50

 100 100


I'm wondering why the second printf doesn't b 50 50.
loremar
Try adding a space in the second scanf like this:
Code:
    char cmd;
    int row, col;
    scanf("%c %d %d", &cmd, &row, &col);
    printf("%c %d %d\n", cmd, row, col);
    scanf(" %c %d %d", &cmd, &row,&col);
    printf("%c %d %d\n", cmd, row, col);

or insert another scanf:
Code:
    char cmd;
    int row, col;
    scanf("%c %d %d", &cmd, &row, &col);
    printf("%c %d %d\n", cmd, row, col);
    scanf("%d", &col);
    scanf("%c %d %d", &cmd, &row,&col);
    printf("%c %d %d\n", cmd, row, col);

It works! Wink

Why? I'm not sure but my guess is that %c counts a whitespace as a character.
Like for example, if you write the code this way:
Code:
    char cmd;
    char cmd2;
    int row, col;
    scanf("%c%c%d %d", &cmd, &cmd2, &row, &col);
    printf("%c%c%d %d\n", cmd, cmd2, row, col);
    scanf(" %c %d %d", &cmd, &row,&col);
    printf("%c %d %d\n", cmd, row, col);

Try writing a space for the second %c and you'll get the same output:
Code:
g 100 100
g 100 100
b 50 50
b 50 50

So in your original code, the second scanf reads a whitespace for %c and the letter b for %d. Since the input for integer is an error, the last printf prints the last correct input for row and col.
davidv
Thanks for the response. Sadly, you're close but no cookie.

What happened was, scanf() stopped scanning the IOStream before the new line character. This meant that when I used scanf() again, it would replace cmd with the new line character from the previous scan instead of the new character I've entered since the stream wasn't cleared (or flushed... I think that's the correct terminology here).

So what I did was make scanf() look at one more character (the new line character) and store that somewhere:

Code:
scanf("%c %d %d%c", &cmd, &row, &col, &nl);


It's pretty interesting. I've never had to deal little things like this before.
loremar
Quote:
What happened was, scanf() stopped scanning the IOStream before the new line character. This meant that when I used scanf() again, it would replace cmd with the new line character from the previous scan instead of the new character I've entered since the stream wasn't cleared (or flushed... I think that's the correct terminology here).

Yes, like I said the second scanf reads a whitespace for %c instead of b. But I did foolishly thought that it read the \n of the previous print(of course that's silly since scanf only reads the stdin not stdout). What mislead me is that I hit enter before printf and so it must have read the next line character right after printf(That's silly logic right there). I guess no cookie for me. Razz

I think the cleanest solution is to use getchar() after scanf or before the next scanf instead of creating/initializing another variable. Or avoid a little pitfall like this and use %s instead unless you like to read character by character.

Quote:
It's pretty interesting. I've never had to deal little things like this before.

Me too. That's maybe because I haven't used stdin and stdout for years. Smile
Arrogant
YOu could flush the stdin if its makin much of a problem
Related topics
c++ ile programlamaya devam....
[help]Ejercicio en lenguaje C
iCode
This C code is little unclear to me.
What's wrong with this C code???
Simple menu written in C - Problem with loop
Basic C Programming Help?
C++ program yazmaya başlayalım artık :=)
Languages in c++
C Program to propose a girl
C > Chr() in C++??
A C program in Linux
C program with 3D array, in Linux
[C] Splitting names program
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.