1

I just started learning on C, I was trying to do some exercises. There is an exercise that wants me to take input for a character,a word and a sentence then print it. I tried to take all three input with scanf but when I execute the code program wants the first two inputs but skips the third input and prints the first two. I tried fgets and gets functions too but I couldn't solve the problem.

#include <stdio.h>
#include <string.h>

int main() 
{
    char ch;
    char s[100];
    char sen[100];

    scanf("%c", &ch);
    scanf("%s", &s);
    scanf("%[^\n]%*c",sen);
    printf("%c", ch);
    printf("\n%s", s);
    printf("\n%s", sen);
    
    return 0;
}

I tried scanf, fgets and gets to enter all the inputs but I couldn't take and print all of them.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Welcome to SO. Please provide the exact input, output and expected output. You can use the `edit` button below your question to add the missing information. Best copy&paste as formatted text. – Gerhardh Mar 10 '23 at 10:32
  • You should not even think about using `gets`. It is considered dangerous and was removed from the standard long ago. You should also never use `scanf` to read strings without providing a length limit: `scanf("%s", &s);` => `scanf("%99s", s);` (Note the removed `&` as well) – Gerhardh Mar 10 '23 at 10:34
  • Instead of describing that you tried to use `fgets` you should show what you tried. – Gerhardh Mar 10 '23 at 10:37
  • With both `%c` and `%[]` you must expcitly filter leading whitespace. This is typically done by using a space in front of the specifier, e.g. `" %c"` or `" %[^\n]"`. This removes *any amount* of whitespace. Trying to deal with trailing whitespace is kludgy and awkward, and usually only removes a single character. – Weather Vane Mar 10 '23 at 10:43
  • Also this is informative: [What does `scanf("%*[^\n]%*c")` mean?](https://stackoverflow.com/questions/30065675/what-does-scanf-nc-mean) – mcagriaksoy Mar 10 '23 at 10:47

1 Answers1

2

The problem is that this call of scanf

scanf("%[^\n]%*c",sen);

encounters a new line character '\n' that is stored in the input buffer due to pressing the key Enter in the preceding call of scanf and thus reads an empty string.

Also in this case

scanf("%s", &s);

the second argument expression has invalid type char ( * )[100] instead of char *.

Write instead

scanf("%c", &ch);
scanf("%99s", s);
scanf(" %99[^\n]",sen);

Pay attention to the leading space in the format string in the third call of scanf. It allows to skip white space characters in the input buffer.

In the first call of scanf you also may place a leading space in the format string

scanf(" %c", &ch);

It will be useful for example when you read characters in a loop and need to skip the new line character '\n' of a preceding call of scanf.

As for this call with the conversion specifier s

scanf("%99s", s);

then the function automatically skips white space characters until a non white space character is encountered. Do placing a leading space in the format string is redundant.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335