0

My code:

#include <stdio.h>

#define SPITZE          1
#define GUT             2
#define BEFRIEDIGEND    3
#define AUSREICHEND     4
#define DURCHGEFALLEN   5
#define JA              1
#define NEIN            0

int main (void)
{

    unsigned int note, bestanden;

    printf ("\nGeben Sie bitte eine Note (1-5) ein: ");
    scanf ("%u", &note);

    if (note == SPITZE)
        bestanden = JA;
    else if (note == GUT)
        bestanden = JA;
    else if (note == BEFRIEDIGEND)
        bestanden = JA;
    else if (note == AUSREICHEND)
        bestanden = JA;
    else
        bestanden = NEIN;

    bestanden ? printf ("\nOK!,") : printf( "\nNICHT bestanden");
    return 0;
}

Console output

So the console doesn't even print out the text first printf ("\nGeben Sie bitte eine Note (1-5) ein: "); . The text doesnt show up, until I enter a Number and then it prints out the "printf & the result" directly.

How do I get it to printf the line to see it ?

NST7R
  • 21
  • 1
  • 6

3 Answers3

4

You can use fflush() after printf(): Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).

antzshrek
  • 9,276
  • 5
  • 26
  • 43
lihudi
  • 860
  • 1
  • 12
  • 21
  • May I have an example, since I tried it and it still doesn't work. I wanna see if i got it correct – NST7R Jun 16 '20 at 10:48
2

When using functions that print to stdout like printf, on some systems you might need to "flush" the buffer in order to make the items appear on screen.

This is typically done by adding a \n at the end of each printf, but you can also do it explicitly with a call to fflush(stdout).

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

printf is buffered so you should flush it before the scanf

antzshrek
  • 9,276
  • 5
  • 26
  • 43
0___________
  • 60,014
  • 4
  • 34
  • 74