fgetc(stdin) in a loop is producing strange behaviour

Terminals tend to be line-buffered, meaning that stream contents are accessible on a line-by-line basis. So, when fgetc starts reading from STDIN, it’s reading a full line, which includes the newline character that ended that line. That’s the second character you’re reading. As for fflush, that’s for flushing output buffers, not input buffers. So, what … Read more

getc() vs fgetc() – What are the major differences?

From the Advanced Programming in Unix Environment: … The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro. This means three things: The argument to getc should not be an expression with side effects. Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of fgetc as … Read more

Reading string by char till end of line C/C++

You want to use single quotes: Double quotes (“) are for strings, which are sequences of characters. Single quotes (‘) are for individual characters. However, the end-of-line is represented by the newline character, which is ‘\n’. Note that in both cases, the backslash is not part of the character, but just a way you represent … Read more