-2

I'm new at coding and I'm trying to run this code on Eclipse, and instead of going line by line, it is asking me for the input in the beginning and printing the outputs together.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int a;
printf("type in the number");
scanf("%d",&a);
printf("you have typed in %d",a);


return EXIT_SUCCESS;
}

This is the output

enter image description here

let me know what I've done wrong, thanks.

The code works on other IDEs but its on eclipse where it does not work.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Vimal
  • 1
  • 1
  • Sometimes `stdout` (where `printf` goes) is line buffered. So it won't actually flush to the terminal until it sees a line feed (`'\n'` in C). Try adding `fflush(stdout)` after the first `printf`. – Jason Aug 23 '23 at 17:54
  • Even though I didn't understand what that meant, it works now! Thanks! – Vimal Aug 23 '23 at 17:58
  • You need whitespace too. Notice that everyting has been run together. `printf` only separates each output, if you include it in the format. – Weather Vane Aug 23 '23 at 18:00
  • I used white spaces without the `fflush(stfout)` but i get the same result, they ask for the input before printing the string "type in the number". – Vimal Aug 23 '23 at 18:09
  • Printf asks to write to stdout, but since your strings don't contain any '\n', the lines are considered "not ready to be printed", so they stay in memory. When your program ends, the memory (output buffer) is flushed and its content sent for display. That's not 100% accurate, it's a bit more complex than that, but you get the global idea. – AR7CORE Aug 23 '23 at 19:29
  • "I used white spaces without the `fflush(stfout)`". So use the `fflush`. I said you ***also*** need to add whitespace. – Weather Vane Aug 23 '23 at 21:26

0 Answers0