3


I'm prompting the user to enter the length of an array, initializing a char[] array with this input, and then prompting the user to type a message to enter into the char[] array.

I'm reading the first character of the user's message with getchar().

However, getchar() is reading the new-line escape '\n' before it is reading any user input. It seems to be getting '\n' from the previous printf statement that prompts the user...


Here is the relevant code:

#include <stdio.h>

int main(void) {

    int len = 0,
        originalLen = 0;

    printf("\n\nWhat is the length of the array? ");
    scanf("%d", &originalLen);
    char str[originalLen]; // intitializing the array

    printf("Enter a message to enter into the array: ");
    char target = getchar();
    str[len] = target;

    // why is getchar() reading '\n'?
    if (target == '\n') {
        printf("\n...what happened?\n");
    }
    return 0;
} // end of main


Ian Campbell
  • 2,678
  • 10
  • 56
  • 104

3 Answers3

4

It's because the previous scanf does not read the newline after the number.

This can be solved two ways:

  1. Use e.g. getchar to read it
  2. Add a space after the scanf format (e.g. scanf("%d ", ...))
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks @Joachim, so it seems that you have to use the extra space hack for `scanf`... is there a better alternative, or is this professionally accepted? – Ian Campbell Feb 08 '13 at 03:51
  • 1
    @IanCampbell I wouldn't call it a "hack", it simply tells `scanf` to read (and discard) whitespace. As for "professionally accepted", I've used it myself a lot and seen it in a lot of other code as well, so it's not uncommon. – Some programmer dude Feb 08 '13 at 03:52
3

You can use getchar in a loop to flush out stdin before reading the next character.

while((target = getchar()) != '\n' && target != EOF)
Simon Broadhead
  • 3,483
  • 21
  • 19
2

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Ah thanks @Alok, that makes sense -- it's hitting the ENTER key that adds the `'\n'` character. Is `scanf` frowned upon for this reason? – Ian Campbell Feb 08 '13 at 03:50
  • 1
    @IanCampbell: Yes, for this and many more. such as: *type safety, input validation and so on* . It should be avoided while reading characters and strings but it is okay to use it for simple numeric inputs. – Alok Save Feb 08 '13 at 04:03
  • Thanks @Alok, interesting... so what's a better alternative to `scanf` for characters and Strings? – Ian Campbell Feb 08 '13 at 04:34
  • 1
    @IanCampbell: [`fgets` always!](http://stackoverflow.com/questions/1252132/difference-between-scanf-and-fgets) – Alok Save Feb 08 '13 at 04:37