0

So the first input is a character and the second input is a string, and the last input is a whole sentence. It says `Segmentation Fault', I can't find where I did the mistake.

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

int main() 
{
    char ch,s,sen[20];
    scanf("%c", &ch);
    scanf("%s", &s);
    scanf("\n");
    scanf("%[^\n]%*c", &sen);
    printf("%c", ch);
    printf("%s", s);
    printf("%s", sen);
    return 0;
}
  • 1
    What did you intend with `scanf("%s", &s);`? – Scott Hunter Jul 12 '22 at 13:48
  • I'm taking a word as an input there – Nunna Swaroop Gunadham Jul 12 '22 at 14:02
  • `s` is a _single_ character. How does a word (or a single character string with null-terminator for that matter) fit into that? – user17732522 Jul 12 '22 at 14:04
  • See [Common string handling pitfalls in C programming](https://software.codidact.com/posts/284849). Your bug is FAQ #1. And because of it you write out of bounds of the character where no memory exists as shown in the linked duplicate. I'm unaware of a better duplicate for "how do strings work" on SO. – Lundin Jul 12 '22 at 14:07

0 Answers0