1

I'm currently studying beginner C programming using Eclipse IDE.

Below is a very basic program that suppose to print out a line first, take in some input, and then print out a second line. But my Eclipse always ask me for the input first before printing out the two lines together afterwards.

This is extremely frustrating because it is something so simple and doesn't work. What am I doing wrong?

#include <stdio.h>

int main(void){

    int aNumber;

    printf("first line\n");
    scanf("%d", &aNumber);
    printf("second line with %d", aNumber);

    return 0;
}

enter image description here

halfer
  • 18,701
  • 13
  • 79
  • 158
Thor
  • 8,608
  • 10
  • 43
  • 113
  • 1
    Used the wrong dup, try this one: https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – John3136 Jun 14 '17 at 02:08
  • 1
    Is this running in Windows? Perhaps the console is expecting `\r\n` line breaks instead of just `\n`? Your code works fine in OS X. – r3mainer Jun 14 '17 at 02:08
  • @John3136 thanks for the suggestion! but after reading the suggested duplicate, I had this feeling of being thrown into the middle of the ocean without learning how to swim first lol. I think the suggested dup is a bit out my league right now, for a beginner like me – Thor Jun 14 '17 at 02:18
  • @squeamishossifrage hi, thanks for the suggestion! I am using windows and I have tried using \r\n, but it doesn't work :( – Thor Jun 14 '17 at 02:19
  • @CaptainAmerica Very strange. Try adding `fflush(stdout);` after the first `printf()` statement and see if that makes any difference. – r3mainer Jun 14 '17 at 02:21
  • @squeamishossifrage yes! it worked! thank you so so much! but why is this happening? do you have any ideas? – Thor Jun 14 '17 at 02:23
  • 1
    @CaptainAmerica In the comments after [this question](https://stackoverflow.com/q/35358924/1679849), someone mentioned that *"Eclipse's console doesn't count as a terminal so you don't get the default line-buffering"*. So I guess you just have to flush stdout manually :-( – r3mainer Jun 14 '17 at 02:30
  • Which platform are you on? – Erik W Jun 14 '17 at 02:38
  • Probably an Eclipse thing. – MD XF Jun 14 '17 at 02:39
  • @ErikW I'm on windows 10, using eclipse the latest version (download it two weeks ago) – Thor Jun 14 '17 at 12:50

1 Answers1

1

Had the same problem once. Solved it by flushing stdout.

#include <stdio.h>

int main(void){
  int aNumber;
  printf("first line\n"); 
  fflush(stdout); // Prints stdout content

  scanf("%d", &aNumber);
  printf("second line with %d", aNumber);

  return 0;
}
  • That's probably not it. `stdout` is line-buffered, meaning it's automatically flushed upon `\n`. – MD XF Jun 14 '17 at 02:39
  • It should be automatically flushed, but it is not. I had this problem using mingw on windows. It may be worth a shoot. – maycon kruger Jun 14 '17 at 02:48
  • @MDXF It may be an Eclipse-specific bug where the standard library thinks it's writing to a file, and therefore doesn't need to line-buffer. – user253751 Jun 14 '17 at 03:20