0
#include <stdio.h>

int main(void) {

   char str[50];
   printf("Enter a string : ");
   gets(str);
   printf("You entered: %s", str);
   return(0);
}

I am running the code on Eclipse, I expected to get the following result:

Enter a string : abcde
You entered: abcde

but when I run the program, the first print statement does not show up in the console, and after I type in any string, the program fails. Can anyone tell me why this happened? Also, can you tell me how to fix my code to get the expected result?

Jens
  • 61,963
  • 14
  • 104
  • 160
  • 7
    [DO NOT use `gets()`, it is dangerous](http://stackoverflow.com/q/1694036/2173917). use [`fgets()`](https://linux.die.net/man/3/fgets) instead. – Sourav Ghosh Jan 16 '17 at 14:45
  • 6
    What do you mean that it "fails"? *How* does it fail? Does it crash? You get unexpected results? Please elaborate. – Some programmer dude Jan 16 '17 at 14:46
  • 4
    *"the first print statement does not show up in the console"* Try `fflush(stdout);` after the `printf`. – Weather Vane Jan 16 '17 at 14:47
  • I changed it to main(void), and it works, I am not sure why. – user6952870 Jan 16 '17 at 14:58
  • The change to `main(void)` shouldn't affect anything. – Jonathan Leffler Jan 16 '17 at 14:59
  • In C a function that doesn't take any arguments must be declared as taking `void`. `void func()` is *not* the same as `void func(void)`: The first one says that the function takes an unknown number of unknown arguments, the second says that the function takes no arguments. But as @JonathanLeffler says, it should not really affect anything. – Some programmer dude Jan 16 '17 at 14:59
  • *gets* has been [removed from `stdio.h`](http://stackoverflow.com/questions/34031514/implicit-declaration-of-gets) so its declaration is implicit - not sure this is the cause of your problem (buffering is, at least) but try to use *fgets* instead (while it could be not the case with your compiler...). – Breaking not so bad Jan 16 '17 at 15:00
  • Also, I am still not getting the expected result. It seems that I have to type in something before the first print statement shows up. – user6952870 Jan 16 '17 at 15:01
  • when I run the program, there is nothing shows up in the console. After I type in any string such as 'abc', it prints 'Enter a string : You entered: abc'. I thought the first print statement will shows up before I enter any string. can someone explain why? – user6952870 Jan 16 '17 at 15:05
  • 2
    Then you are definitely missing a `fflush` after the first output. See the comment by @WeatherVane. – Some programmer dude Jan 16 '17 at 15:07
  • 1
    As for the reason why the printf text is not automatically showing, read http://stackoverflow.com/a/1716621/5265292 – grek40 Jan 16 '17 at 15:11
  • 1
    @ringø: If `gets()` was fully removed, he would get a linker error and he wouldn't be able to run the program. – Bill Lynch Jan 16 '17 at 16:04
  • A major part of the problem is that the enviroment that Eclipse runs your program in means that the program's input and/or output doesn't appear to be a terminal to the standard I/O library, so the synchronization between standard output and standard input that many libraries provide when the standard I/O channels are connected to a terminal is in abeyance. That means that data printed to standard output is not necessarily flushed (hence the advice to use `fflush(stdout)`). Also, the non-prompt output should end with a newline; it may not appear in a timely fashion if it doesn't. – Jonathan Leffler Jan 16 '17 at 16:43
  • @BillLynch It's been removed from `stdio.h` ... not from the libraries! Tried it myself, the declaration is "implicit". But the linkage is ok. – Breaking not so bad Jan 17 '17 at 05:38

2 Answers2

5

Output is probably line buffered and your printf string does not end with a newline. Force output with fflush(NULL); after any printf() that does not end in \n.

Jens
  • 61,963
  • 14
  • 104
  • 160
  • You may find [rules of automatic flushing stdout](http://stackoverflow.com/q/39536212/2410359) informative. `'\n'` is commonly sufficient, though not specified to flush. `fflush(NULL)` affects all output streams. Suggest `fflush(stdout);`. – chux - Reinstate Monica Jan 16 '17 at 17:59
-3

Use fgets(char *str, int n, FILE *stream); instead of gets.

Govind Parmar
  • 18,500
  • 6
  • 49
  • 78
Marius
  • 27
  • 1
  • 5
  • 4
    While it is a good idea to use `fgets` instead of `gets`, that is not the issue that the OP is having - the problem is that the `printf` output is not even showing up (which is before the call to `gets`). – Govind Parmar Jan 16 '17 at 16:05
  • 2
    That will not fix the missing output issue. – Jens Jan 16 '17 at 16:05