1

Hi consider the simple program below:

int main(void)
{
    //exercise 1

    float num2;
    printf("please enter a number \n");
    scanf_s("%f", &num2);
    printf("the number multiple by 3 is %3.3f\n", num2 * 3);

    //exercise 2
    char ch1, ch2, ch3, ch4;

    printf("enter a word with four char\n");
    ch1 = getchar();
    ch2 = getchar();
    ch3 = getchar();
    ch4 = getchar();

    printf("the chars in reverse order are\n");
    putchar(ch4);
    putchar(ch3);
    putchar(ch2);
    putchar(ch1);
    putchar('\n');
}

the output is:

please enter a number
2
the number multiple by 3 is 6.000
enter a word with four char
ffff
the chars in reverse order are
fff

3 chars printed to the console, if i move the code block of exercise 2 above 1:

int main(void)
{
    //exercise 2
    char ch1, ch2, ch3, ch4;

    printf("enter a word with four char\n");
    ch1 = getchar();
    ch2 = getchar();
    ch3 = getchar();
    ch4 = getchar();

    printf("the chars in reverse order are\n");
    putchar(ch4);
    putchar(ch3);
    putchar(ch2);
    putchar(ch1);
    putchar('\n');

    //exercise 1

    float num2;
    printf("please enter a number \n");
    scanf_s("%f", &num2);
    printf("the number multiple by 3 is %3.3f\n", num2 * 3);
}

the result as expected:

enter a word with four char
ffff
the chars in reverse order are
ffff
please enter a number
2
the number multiple by 3 is 6.000

i want to know why it's working when i change the order the code block and how can i solve it, thank you.

fluter
  • 13,238
  • 8
  • 62
  • 100
YaakovHatam
  • 2,314
  • 2
  • 22
  • 40
  • Extremely common FAQ. You have new line characters left in the input buffer. See for example the linked duplicate, section "When *scanf() does not work as expected". – Lundin Dec 13 '16 at 09:29

2 Answers2

4

want to know why it's working when i change the order the code block and how can i solve it,

That's because scanf_s("%f", &num2); leaves newline character in the input buffer. So your first getchar(); will interpret that newline as ch1.

For this case, a silent preceding getchar would do:

getchar();  // will consume the remaining newline from stdin
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
artm
  • 17,291
  • 6
  • 38
  • 54
  • 1
    Using fflush won't work, because it is not defined for input buffers. Please remove that part from the answer, since it is incorrect and leads to invoking undefined behavior. – Lundin Dec 13 '16 at 09:30
  • ah right, `fflush` isn't standard, tks. Agree and remove that part.. – artm Dec 13 '16 at 09:53
2

There is a newline character when you input the first floating number, and it is input as a char by the call to getchar. Another way to fix this is to get entire line as a string using fgets, then parse it with whatever format you want:

char line[512];
printf("please enter a number \n");
fgets(line, sizeof line, stdin);    // the newline is consumed here
sscanf(line, "%f", &num2);

ch1 = getchar(); // working as expected
fluter
  • 13,238
  • 8
  • 62
  • 100