10

I am new to C language and I am trying read a character and a string (a sentence; max-length 25) from a user.

Not sure what I am doing wrong in the following lines of code, its giving me an error "Segment Fault".

#include <stdio.h>

int main(){
    char * str[25];
    char car;

    printf("Enter a character: ");
    car = getchar();

    printf("Enter a sentence: ");
    scanf("%[^\n]s", &str);

    printf("\nThe sentence is %s, and the character is %s\n", str, car);

    return 0;
}

Thanks!

0

3 Answers 3

13

You have to make four changes:

  1. Change

    char * str[25];
    

    to

    char str[25];
    

    as you want an array of 25 chars, not an array of 25 pointers to char.

  2. Change

    char car;
    

    to

    int car;
    

    as getchar() returns an int, not a char.

  3. Change

    scanf("%[^\n]s", &str);
    

    to

    scanf( "%24[^\n]", str);
    

    which tells scanf to

    1. Ignore all whitespace characters, if any.
    2. Scan a maximum of 24 characters (+1 for the Nul-terminator '\0') or until a \n and store it in str.
  4. Change

    printf("\nThe sentence is %s, and the character is %s\n", str, car);
    

    to

    printf("\nThe sentence is %s, and the character is %c\n", str, car);
    

    as the correct format specifier for a char is %c, not %s.

Sign up to request clarification or add additional context in comments.

Comments

11

str is an array of 25 pointers to char, not an array of char. So change its declaration to

char str[25];

And you cannot use scanf to read sentences--it stops reading at the first whitespace, so use fgets to read the sentence instead.

And in your last printf, you need the %c specifier to print characters, not %s. You also need to flush the standard input, because there is a '\n' remaining in stdin, so you need to throw those characters out.

The revised program is now

#include <stdio.h>    
void flush();
int main()
{
    char str[25], car;

    printf("Enter a character\n");
    car = getchar();

    flush();

    printf("Enter a sentence\n");
    fgets(str, 25, stdin);

    printf("\nThe sentence is %s, and the character is %c\n", str, car);

    return 0;
}
void flush()
{
    int c;
    while ((c = getchar()) != '\n' && c != EOF)
        ;
}

4 Comments

Thanks for your answer. I copy and pasted your code, but its terminating the program without letting me enter a sentence. So, right after I enter a character and press enter, before asking for a sentence input it gets terminated.
You would need to flush the standard input (don't use fflush(stdin)).
"you cannot use scanf to read sentences--it stops reading at the first whitespace" -- Actually, it is possible (by uaing the %[ format specifier). %s in scanf stops reading on the first whitespace. BTW, fgets will read the \n left out by the getchar() in your program.
I am not using fflush(stdin), in fact I don't even know what that is lol, as I am new to c language. I think this my 3rd or 4th program I ever wrote in c.
1

// This is minimal change to your code to work

#include <stdio.h>

int main(){

    char car,str[25];

    printf("Enter a character: ");
    car = getchar();

    printf("Enter a sentence: ");
    scanf("%s", str);

    printf("\nThe sentence is %s, and the character is %c\n", str, car);

    return 0;
}

1 Comment

Why do you answer old questions? Is there anything different from the pre-existing answers?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.