0

I was trying to search for an answer for that but couldn't, hope someone can help. I have the following snippet of code:

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

int main()
{
    char c = '\0';
    int error = scanf(" %c", &c);

    // The user types now the following: A54fG6

    while (error != EOF) {
        printf("%c", c);
        error = scanf(" %c", &c);
    }
    return 0;
}

Where the first comment is, the input buffer points at A. Then it goes into the while loop, prints the character 'A' and the second scanf advances the input buffer to point at 5. After the last iteration, when the printf printed '6' - the second scanf points at what character?

Or in different words, how can I know when the program finished reading the current input buffer and then do something before the scanf prompts the user for more characters?

  • "Where the first comment is, the input buffer points at A", What comment? What A? – chux - Reinstate Monica Dec 05 '14 at 15:49
  • The 'scanf' will next input the next available char from stdin. In this case, the next char is probably a '\n' (newline). the next execution of scanf will (if it has that leading space in the format string, like yours do) consume the newline then return with the next available char (probably from the next user keystroke) However, the format string in the scanf's will skip over spaces, tabs, newlines. so if your code wants to receive any of those characters, then it would be better to use getch than to use scanf – user3629249 Dec 05 '14 at 15:50
  • Sorry for the "comment" and "A" terms, it was in the old edit and I edited it now into the code – Gal Fleissig Dec 05 '14 at 17:16

1 Answers1

1

First of all, scanf is a function. It points to some code, that runs every time you call it. Not to a character.

The input buffer of scanf is stdin (at least in your case), and it is open while the program is running. Whenever you call scanf, it tries to read from stdin. If it has nothing to read, it asks the user to input something. That's the way it works.

Now, the user may introduce an EOF character, but I don't think this is what you want.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • 1
    It doesn't exactly "ask" the user to input something, rather it blocks waiting for input to become available. – unwind Dec 05 '14 at 15:51
  • The problem that I'm having is that rather than arriving the "return 0" term in the end, what the above program does is printing the exact sequence of characters the user typed and then just stops. – Gal Fleissig Dec 05 '14 at 17:18
  • It not 'stops'. It asks for more characters, like you instructed it to do. You can use an "ending" character, or insert manually an EOF. – Paul92 Dec 05 '14 at 18:36
  • Thank you Paul92, it helped me a lot and I used an "ending" character. By the way something you mentioned and I didn't know (and it's probably worth for more people to know) is that the constant EOF will not appear when you type in manually, so you have to either insert it manually or read from a file. – Gal Fleissig Dec 05 '14 at 18:41
  • I'm glad I helped. Note that the files don't actually have an EOF character (http://stackoverflow.com/questions/9907416/common-misconception-files-have-an-eof-char-at-their-end), it is just a constant nowdays. Also, please consider upvoting/accepting my answer :). – Paul92 Dec 05 '14 at 18:49