0
scanf("%lf",&b);
fgets(str, 100, stdin);

In the above code, fgets is not read until I add a space after %lf (eg. scanf("%lf ",&b);), or press ctrl-d instead of enter in the terminal. Why is this so?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
kulokromi
  • 3
  • 1
  • Possible duplicate of [fgets doesn't work after scanf](http://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) – n. m. could be an AI Jan 14 '17 at 11:27
  • You got latte answers because your question was not tagged `c` so was missing attention , in future please tag your question appropriately . – Suraj Jain Jan 14 '17 at 11:55

2 Answers2

0

Don't mix fgets() and scanf().

scanf() leaves a newline (when you press ENTER key) which terminates the input reading of fgets() as fgets() would as soon as it sees a newline character, effectively your fgets() not reading anything at all.

fgets():

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

When you have a whitespace in the format string (scanf("%lf ",&b);), the scanf call would only return after you input a non-whitespace which will then be read by the fgets() call. This approach is error-prone. Because you will be forced to input something if you don't read anything further.

Please see: Why does everyone say not to use scanf? What should I use instead?

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Well, or mix them only after reading and understanding the respective manual pages, as well as your terminal's, and perhaps shell's, and [stdin's](http://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html); unfortunately, manual terminal I/O is a thorny issue which for some reason tends to occur in beginner's exercises much more often than in real life. In real life you either program for a GUI (with entirely different, though usually no less thorny issues); or you are likely to not have to bother with human or console issues. – Peter - Reinstate Monica Jan 14 '17 at 11:28
  • I don't like the title of that link. Have been using scanf() since I was about 15. If someone is supposed to understand complicated programming concepts, they should also be capable of grasping how scanf() works. – szpanczyk Jan 14 '17 at 11:36
  • Whilst one could use *any* C function (or language for that matter) correctly *if* they understand it fully, `scanf()` is considered as something that can [easily cause many troubles and often error-prone](http://stackoverflow.com/search?q=scanf+bad). This is a long held opinion by many others for a long time with good reasons. Steve Summit probably wrote it originally in the early 90's but it's relevant and is still applicable. – P.P Jan 14 '17 at 22:04
0

The scanf("%lf",...) will stop and wait for more input when it encounters whitespace in input. Adding a space to the format string causes the whitespace character (or characters) to be removed from stdin, and the function to return.

Another way to cause scanf() to return is to make it recognise an error of some form. One way is to make it seem like it has reached end of file. You haven't mentioned it, but your host system is some flavour of unix - typing CTRL-D (depending on terminal settings you may need to hit CTRL-D after a newline, or possibly enter it twice) makes it seem like end of file has been encountered. If you check the return value from scanf() it will return EOF in this case too. Different systems require different actions to trigger end of file though.

Either way, fgets(..., stdin) cannot be called until scanf() returns. Note that triggering end of file may (depending on terminal settings) also cause fgets(..., stdin) to return EOF (i.e. it won't necessarily read input).

It is a really bad idea to mix use of scanf() (or related functions) and fgets() on the same stream (stdin in this case), because they do interact in strange ways (each relies on behaviour in interacting with the stream that the other doesn't provide)

Peter
  • 35,646
  • 4
  • 32
  • 74