1

I was just trying out a simple program in C, inserting into an array. I used a scanf function to accept characters but it seems the compiler just skipped that and just went to end of the program. This was the code that I used :-

#include <stdio.h>

void main()
{
    int a[50], i, j, m, n, x;
    char ch;
    printf("Enter the no. elements of the array :- ");
    scanf("%d", &m);
    printf("Enter the elements below :- ");
    for (i = 0; i < m; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("The array is :- \n");
    for (i = 0; i < m; i++)
    {
        printf("%d", a[i]);
    }
    printf("\nDo you want to enter an element ? (Y/N)\n");
    scanf("%c", &ch);       // The compiler just skips this along with the      
    while (ch == 'y' || ch == 'Y') // while loop and goes straight to the printf 
    {                   // statement
        printf("The index of the element :- ");
        scanf("%d", &n);
        printf("\nEnter a number :- ");
        scanf("%d", &x);
        for (i = m; i > n; i--)
        {
            a[i] = a[i - 1];
        }
        a[n] = x;
        printf("\nInsert more numbers ? (Y/N)");
        scanf("%c", &ch);
        m = m + 1;
    }
    printf("\nThe array is :- ");
    for (i = 0; i < m; i++)
    {
        printf("%d", a[i]);
    }
}

I used the variable ch in order to allow the user to have a choice, whether or not to insert elements i.e. Y or N.

But the compiler basically skips the third scanf function, the one that accepts the char, along with the while loop.

I just want to know why the scanf function was skipped ?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
steve
  • 125
  • 12
  • 2
    Please change `scanf("%c",&ch);` to `scanf(" %c",&ch);` so at to consume the newline left in the buffer. Unlike other formats, `%c` does not automatically skip whitespace in the input buffer. – Weather Vane Feb 21 '17 at 15:41
  • 3
    Possible duplicate of [Scanf Skip scanning character](http://stackoverflow.com/questions/36281871/scanf-skip-scanning-character) – EOF Feb 21 '17 at 15:43
  • ....and please post well indented code... – LPs Feb 21 '17 at 15:43
  • The compiler doesn't care about the keys you press. It only produces an executable program from your source code. This executable program (and the source code you compiled to generate it) is responsible for correct handling of the input. Your code that reads the numbers doesn't consume the newlines produced when you press ``, the newline is then read by the next `scanf("%c")` and this is the reason the code doesn't work as expected. – axiac Feb 21 '17 at 15:44
  • Possible Duplicate Of http://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c – Suraj Jain Feb 22 '17 at 04:01

1 Answers1

7

Back to the previous scanf which is the last array member.

scanf("%d",&a[i])

In the input file if you entered:

32\n 
^^

input will wait just before the newline after reading the decimal number.

In the scanf which causes problem:

scanf("%c", &ch); 

It will read the newline character as it is available in input that's why it will skip that line after being executed implicitly.

To ignore the whitespace you have only to add space before the specifier %c as stated in comment by @xing and @WeatherVane.

scanf(" %c",&ch);

C99 7.19.6.2

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.250)

  • 1
    Good answer +1. But consider to use tons of previous answers as duplicate – LPs Feb 21 '17 at 15:52
  • This question is also duplicate but it seems that there is no good understanding of `scanf` from OP – Meninx - メネンックス Feb 21 '17 at 15:53
  • "It will read the newline character as it is available in input that's why it will skip it. To ignore the whitespaces" I didn't quite get that. Could you please elaborate as to why the scanf function skipps since there is a \n present before it. – steve Feb 21 '17 at 16:00
  • `scanf` read from input file which is `stdin`. As long as it find the required characters in the file and the correct format it will read those characters **directly** without your intervention. When input file is **empty** it will wait for user to fill the input file with required characters. In your case, after reading the decimal number you entered newline which will stay in input file so for the next `scanf` you require a character and as newline is a character it will read it from input that's why it will be skipped. – Meninx - メネンックス Feb 21 '17 at 16:34