0

I have declared 3 variables. here ch for store one character and s for store a single word and sen for a sentence or multiple words! But, when i run the code it is not giving the opportunity to give an input for sen variable.

I have tried to figure out the problem. but I failed! What's wrong with my code. can anyone help me please...

Here is my code...

#include<stdio.h>
int main()
{
    char ch, s[20], sen[100];
    scanf("%c%s",&ch,&s);
    gets(sen);
    printf("%c\n%s\n%s", ch, s, sen);
    return 0;
}
Md Nasir Ahmed
  • 23
  • 1
  • 10
  • 7
    First of all, never ***ever*** use `gets`! [It's so dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it has even been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Aug 30 '21 at 16:53
  • 3
    Beyond that, the `Enter` key you have pressed for the first`scanf` input will be written to the input buffer as a newline `'\n'`. This will be the first character read by `fgets` (or `gets` in your code). The function will treat it as the end of the line, and not read anything else. – Some programmer dude Aug 30 '21 at 16:55
  • Md Nasir Ahmed, what was your exact input? – chux - Reinstate Monica Aug 30 '21 at 17:14
  • @Someprogrammerdude Thanks for your valuable information! I just overcome it by using \n in the scanf line. ` scanf("%c%s\n",&ch,&s);` – Md Nasir Ahmed Aug 30 '21 at 17:20
  • Md Nasir Ahmed, reading with `%s` will not save input with spaces in it like a typical _sentence_. – chux - Reinstate Monica Aug 30 '21 at 17:22
  • @chux-ReinstateMonica I just tried to input 3 different types of character or string. like first one for a single character, 2nd one for a single word and third one for a single line. – Md Nasir Ahmed Aug 30 '21 at 17:23
  • Md Nasir Ahmed, Rather than only [descibe the input](https://stackoverflow.com/questions/68987691/whats-wrong-with-my-code-why-gets-function-is-not-working?noredirect=1#comment121925572_68987691), what was your exact input? – chux - Reinstate Monica Aug 30 '21 at 17:25
  • Sorry, I think i couldn't explain my thoughts. @chux-ReinstateMonica I was trying to input these things.. `c cool its really cool` but i was able to give only 2 input. not 3. that was my problem. – Md Nasir Ahmed Aug 30 '21 at 17:33
  • Trailing white-space isn't the proper solution either. It happens to work for your specific case, but it's a bad habit which will lead to problem in other situations. One possible naive solution is to use a loop reading character by character until the newline (or end of file). A much better solution is to not use `scanf` at all, but use `fgets` to read whole lines, and then possibly use `sscanf` to parse the string. – Some programmer dude Aug 30 '21 at 18:05

1 Answers1

0

You should not use gets function for buffer overflow. Use fgets() instead.

fgets() takes 3 arguments. one is str >>> pointer to an initialized string in which characters are copied. it also returns str.

another is int n >>> number of characters to copy.

and finally, file stream or stdin >>> reading from standard input.

Use

#include<stdio.h>
int main()
{
    char ch;
    char* s[20], sen[100];
    scanf("%c%*c\n", &ch);
    scanf_s("%19s", s, 20);
    fgets(sen, 100, stdin);
    printf("%c\n%s\n%s", ch, s, sen);
    return 0;
}

You are missing &sen which is supposed to take the input.

Fatin Ishrak Rafi
  • 339
  • 1
  • 3
  • 11