0

When learning C in simple programs it often happens that the computer skips the execution of some lines of code, and I can't understand why this is so.

Now as an example, I sketched a program which simply stores 3 numbers entered by the user:

#include <stdio.h>

int main(void)
{
int a, b, c;

printf("Please type in number a: \n");
scanf("%i", &a);
printf("Please type in number c: \n");
scanf("%i", &c);
printf("Please type in number b: \n");
scanf("%i", &b);    
return 0;
}

I want to enter 1, 2, and 3. This is what I get in the console (in Ubuntu):

Please type in number a:
1
Please type in number b:
Please type in number c:
2

The input of number b was ignored.

Not only the second input is skipped, so even the input is in the wrong order. Initially, I wrote everything in alphabetic order in the code - a, b and c, then the input of b was skipped, and then I changed the order in the code and still, as you can see, the execution remained unchanged.

And such cases I have had earlier.

Why is this happening?

Abraham Lincoln
  • 57
  • 2
  • 10

1 Answers1

0

On 2nd thought, I think OP has another issue

Leaving below as community wiki as a reference


To insure output occurs before scanf(), flush the output.

printf("Please type in number a: \n");
fflush(stdout);
scanf("%i", &a);

printf("Please type in number c: \n");
fflush(stdout);
scanf("%i", &c);

See What are the rules of automatic flushing stdout buffer in C?

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256