-1

Fair warning, I am quite new to C, and am still very much a beginner with the language. This problem has stuck with me for a while and I thought someone here might be able to help.

Here is the code:

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

int main()
{

    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You are %d years old \n", age);

    double gpa;
    printf("Enter your GPA: ");
    scanf("%lf", &gpa);
    printf("You're GPA is %lf \n", gpa);    

    char name[20];
    printf("Please enter your name: ");
    scanf("%s", &name);
    printf("Your name is %s \n", name);

    char grade;
    printf("Enter your grade: ");
    scanf("%s", &grade);
    printf("Your grade it %s \n ", grade);

return 0;
}

The problem lies in the 'grade' section, everything else works perfectly. The output of the program with user input is

Enter your age: 3
You are 3 years old 
Enter your GPA: 4.5
You're GPA is 4.500000 
Please enter your name: Test
Your name is Test 
Enter your grade: B
Segmentation fault (core dumped)

Hopefully someone here can help. All the best!

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

2 Answers2

3

Input for getting the input of grade would be

scanf(" %c",&grade);

The issue arises because the grade is a char variable, not a char array.

char -> %c

char array(aka String in C) -> %s

Mohit Sharma
  • 338
  • 2
  • 13
  • Thanks , I forgot about the \n being left by %s before, I did mean `scanf(" %c",&grade);`. Will edit and fix it . – Mohit Sharma Sep 08 '20 at 05:21
  • 1
    Now that's what will keep the input for grade from being skipped! To make it a truly helpful answer, you could explain what is happening with the `"%s" `and leaving the `'\n'` in `stdin` unread and without the `" %c"` the `'\n'` will be taken as the input for `grade`. – David C. Rankin Sep 08 '20 at 05:22
1

The reason you get segmentation fault is that you are scanning the input B as a string instead of a character (it is equivalent to {'B','\0'}, so it is not just B) and you are trying to assign this string into a char .

If you want to get a character as input (seems like you want it this way) you must do :

scanf ("%c",&grade);

The %c format specifier is for scanning chars, and &grade is basically the address of grade.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// However, if you want to get a string as input (i.e. character array in C) , you must define grade like this :

char grade [n];

(n is a number telling the size of array)

and the scanf line must be :

scanf ("%s",grade);

The %s format specifier is for getting strings (i.e. character arrays in C). The reason why we used grade without an address-of (&) operator is related with pointers. Because grade is already a pointer; we need its value (i.e. the address of the first character of array), not address. Check this link for better understanding. Also check this post after ensuring you've understood the logic of pointers.

Note : This is a self study question. Avoid getting downvoted as possible.

  • hmm, now i seem to behaving a problem where it exits the program before i can even answer it. like this Enter your grade: Your grade is – RancidRocket Sep 10 '20 at 04:57