Clarification Needed Regarding Getchar() and Newline

Clarification needed regarding getchar() and newline

Yes, you have to consume newlines after each input:

char1 = getchar();
getchar(); // To consume `\n`
char2 = getchar();
getchar(); // To consume `\n`

This is not compiler-dependent. This is true for all platforms as there'll be carriage return at the end of each input line (Although the actual line feed may vary across platforms).

Can't figure out why getchar() is picking up newline for first occurence in C

On the first prompt, you type something like aEnter, so your input stream contains the characters 'a', '\n'. The first getchar call reads the a and leaves the newline in the input stream.

In response to the second prompt, you type bcEnter, so your input stream now contains '\n', 'b', 'c', '\n'.

You can probably figure out what happens from here - the next getchar call reads that newline character from the input stream.

There are a couple of ways to deal with this. One is to test your input, and try again if it's a newline:

do
{
a = getchar();
} while ( a == '\n' ); // or while( isspace( a )), if you want to reject
// any whitespace character.

Another is to not use getchar; instead, use scanf with the %c conversion specifier and a blank space in the format string:

scanf( " %c", &c ); // you will need to change the types of your 
... // variables from int to char for this.
scanf( " %c", &a );
scanf( " %c", &b );
scanf( " %c", &c );

The leading space in the format string tells scanf to ignore any leading whitespace, so you won't pick up the newline character.

Why is getchar() reading '\n' after a printf statement?

When you enter the number and hit the ENTER key, a number and a character are placed in the input buffer, they are namely:

  • The entered number and
  • The newline character(\n).

The number gets consumed by the scanf but the newline remains in the input buffer, which is read by getchar().

You need to consume the \n before calling getchar() by using:

scanf("%d ", &originalLen);
^^^

This tells scanf to read the number and an additional character, which is \n.

getchar() function getting enter as input

When inserting a character in the command line you are really inserting two characters, it's the character itself and and the newline character(\n), so the first getchar() gets the character and the second getchar() gets the newline character, so it jumps to the third getchar().

If you press only enter you can see that it cycles only once since enter is only one newline charater, you can solve this by putting two getchar(), so the second catches the \n:

while(input != '!'){
puts("Enter a char");
input = getchar();
getchar();
///...

Or create a case in you switch for the newline character.

case '\n': break;

How getchar() works when it is used as condition in while loop

There are many layers between the user writing input into a terminal, and your program receiving that input.

Typically the terminal itself have a buffer, which is flushed and sent to the operating system when the user presses the Enter key (together with a newline from the Enter key itself).

The operating system will have some internal buffers where the input is stored until the application reads it.

Then in your program the getchar function itself reads from stdin which is usually also buffered, and the characters returned by getchar are taken one by one from that stdin buffer.


And as mentioned in a comment to your question, note that getchar returns an int, which is really important if you ever want to compare what it returns against EOF (which is an int constant).

And you really should compare against EOF, otherwise you won't detect if there's an error or the user presses the "end-of-file" key sequence (Ctrl-D on POSIX systems like Linux or macOS, or Ctrl-Z on Windows).

Why we need getchar() although using scanf()?

When scanf("%d%c", ... encounters non-numeric input, the "%d" causes the scanning to stop and the offending character to remain in stdin for the next input function. The "%c" does not get a chance to read that non-numeric character.

If code re-reads stdin with the same scanf("%d%c", ..., the same result. Some other way is needed to remove the non-numeric input. getchar(), getch(), etc. will read any 1 character.

Example code GetPositiveNumber()



Related Topics



Leave a reply



Submit