0

I am using GCC to compile my C code. My second scanf is not stopping to get the input. It only reads in the first scanf and prints the two statements, one with what I entered in string and the other is just blank.

int main (void) {
    setvbuf(stdout, NULL, _IONBF, 0);

    char string[25] = {'\0'};
    char c;

    scanf(" %s", string);  

    scanf(" o%c", &out);

    printf("Input is : %s \n\n", string);
    printf("Out is: %c", out);

    return 0;
}

Instead of getting

Input is whatever I typed and a prompt to enter a char for out

I got output as shown below

Input is : whatever i typed
Out is:

The program terminates. Can someone help. I've done some research and tried to put a space before %c for out and for string and still nothing happened.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Please clarify *exaclty* what input you gave. Best to just run the program and paste in the *exact* log. Your second `scanf` expects the input to contain the letter `o` followed by another character. Is that really what you intended? – kaylum Apr 07 '16 at 05:12
  • Is the `o` really in the format string `" o%c"`? Note that `%s` stops reading at the first white space after a non-space character. – Jonathan Leffler Apr 07 '16 at 05:31

1 Answers1

3

You haven't defined out. And c is unused here. Having said that

Change

scanf(" o%c", &out);  //What is that o in here? Is it a typo?

to

scanf(" %c", &out);

If your terminal uses line-buffered input

scanf(" %s", string);  

can read the input till the first white-space. So the input buffer from the white-space is unused which is available for the next scanf which automatically starts reading from the buffer. So if you enter a string with spaces, the white-space will be assigned to the character out in your case

Change first scanf like below to clear the buffer:

if( scanf(" %s", string) == 1)
{
while(getchar()!='\n')
  continue;
}

Also you might wish to replace

scanf(" %s", string); 

with

fgets(string,25,stdin);
/* Use 26 if you actually wish to have max 25 characters
 * ie char string[26]={'\0'}
 */

fgets has the advantage that it can read the white spaces and the newline charcter '\n' and it will automatically trim the output in case of an overflow.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • @diabai : Please have a look at the addition that I have just made. – sjsam Apr 07 '16 at 05:30
  • I just saw it, thanks alot again. I forgot to say that the o in scanf ("o%c", &out) for out is just the first letter to be read to determine the type of output method i want to call . –  Apr 07 '16 at 06:02
  • `is just the first letter to be read` ! To my knowledge, this is illegal and will lead to undefined behaviour. Have a look at this [wiki article](https://en.wikipedia.org/wiki/Scanf_format_string#Format_string_specifications) to see valid specifications. – sjsam Apr 07 '16 at 06:19
  • You must qualify your `while (getchar()..)` loop with `if (scanf(" %s", string) == 1)` or it will hang on user `EOF`. (the return should be checked regardless) – David C. Rankin Apr 07 '16 at 06:25
  • You'd be surprised just how *not* obvious that is to a lot of people. Better to be on the safe side. Thanks. – David C. Rankin Apr 07 '16 at 18:58