Using getchar () in C; will it move to the next char every time i use it? Including assignment operations?

I use getchar () when writing a program in C (scanf is not yet allowed at this point in the course.) I was wondering if I would call it every time, if it would go to the next; including during assignment operations. For instance; I am trying to read in double from the console; and decide if he has a negative sign at the front. If that happens; I want to set the neg variable to 1 (so that I can determine if the final result should be returned negative), and then I want to move on to the next character to do my actual double calculations and what not. ex)

int x = getchar(); int neg = 0; if(x == '-') { neg = 1; x = getchar(); // will this make it so the next time I use the x } // variable it will be past the negative sign and to the //first actual digit? 
+5
source share
1 answer

Yes, every time you call getchar() , it will read the next character (assuming the next character is read).

Quote C11 , chapter ยง7.21.7.6

The getchar() function returns the next character from the input stream pointed to by stdin .

If there is nothing valid to read,

If the stream is at the end of the file, the end-of-file indicator for the stream is set and getchar returns EOF . If a read error occurs, an error indicator for the stream is displayed and getchar returns EOF .

+5
source

All Articles