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

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

  scanf("%c",&a); // take character
  printf("%c",a); //print character
  scanf("%s",s); //take input as a word
  printf("\n%s",s); //print the word

  if((gets(sen))=='\n')
    gets(sen);// take input as a string

  puts(sen); //print that string
}

As gets() takes input from buffer so it will take '\n' as input after that another gets() command should work but that is not working. It doesn't take any input. Why?

pzaenger
  • 11,381
  • 3
  • 45
  • 46
Tushar Jain
  • 134
  • 1
  • 12
  • 2
    [Never use `gets`, period.](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) Rewrite this code to avoid the use of `gets`, then ask a question about your new code if it doesn't work. – Nate Eldredge Jun 06 '20 at 17:39
  • The return value of `gets` is the pointer you pass to it, i.e. `sen`, or `NULL` on error. It will never be `'\n'`. Your compiler should have warned you about the comparison. *Read the man page.* You shouldn't even be using `gets`. It only exists for legacy code. No newly written code uses it. – Tom Karzes Jun 06 '20 at 17:43
  • And `scanf("%s", s)` is just as bad as `gets`; fix that too. – Nate Eldredge Jun 06 '20 at 17:44
  • Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). If the `gets` and the (incorrect) comparison is an attempt to get rid of that, you'll end up in knots. Don't mix your input methods, you'll have the same problem after you change the code to `fgets`. – Weather Vane Jun 06 '20 at 17:46

1 Answers1

0

gets(sen) returns sen, which is the address of the char array.

Therefore, what you are trying to do should be

if(strcmp(gets(sen), "\n") == 0)
    gets(sen);// take input as a string

However, this is error-prone because sen can be a space with a newline, which is not "\n" or so.

Naetmul
  • 14,544
  • 8
  • 57
  • 81