-3

I know it is a simple question but I am stuck.The code is:

#include <stdio.h>
#define MAX_SIZE 1025
#define NUM 64
int main(){
    int mem_size;
    char types[NUM];
    char values[MAX_SIZE];

    fgets(types,NUM,stdin);
    printf("%s",types);
    fgets(values,MAX_SIZE,stdin);
    printf("%s",values);
    scanf("%d",&mem_size);
    printf("%d",mem_size);

    return 0;
}

Although I want the results after I type and hit enter, the flow is: I need to enter all the fgets and scanf stuff and it correctly prints the desired results. What is the problem? Please help.

mualloc
  • 495
  • 9
  • 24

1 Answers1

1

OP: "Problem is it shows the results after I enter mem_size altogether simultaneously, not one by one"

Some systems to not "flush" the stdout output promptly even with a \n. The output that was seen came out just before the program ended, which forced the buffered stdout to the console.

Either add fflush(stdout) after each printf() or change your system's settings (varies with environment) to promptly send stdout to the console.

Ref:

printf not printing on console

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • @Murat Mataracı it was the "Problem is it shows the results ..." comment that made your question sensible. The post contained the 1) what was done 2) statement about what was wanted but it was _missing_ 3) what happened - the comment fixed that. Suggest future posts contain 1) the question 2) code 3) data in 4) data out 5) expected result. Also, after some time - an hour, a day, if an answer works well for you, accept the best one. – chux - Reinstate Monica Jan 05 '14 at 18:10