0

i am a beginner in C programming. I was learning about the user inputs. The following piece of code:

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

    int main()
    {
        char grade;
        printf("Enter you grade: ");
        scanf("%c", &grade);
        printf("Your grade is %c.", grade);

        return 0;
     }

does what I intend it to do i.e.

  • ask for the grade
  • display the grade

But when I modify the code to the following :

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

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

           printf("-----------------------------\n");
           double gpa;
           printf("Enter your GPA: ");
           scanf("%lf", &gpa);
           printf("Your GPA is %f. \n", gpa);

           printf("-----------------------------\n");
           char grade;
           printf("Enter you grade: ");
           scanf("%c", &grade);
           printf("Your grade is %c.", grade);

           return 0;
     }

it does the following:

  • asks for the age
  • displays the age
  • asks for the gpa
  • displays gpa
  • asks for grade
  • it doesnt display grade.

The output looks like:

Enter you age: 45
You are 45 years old.
-----------------------------
Enter your GPA: 4
Your GPA is 4.000000. 
-----------------------------
Enter you grade: Your grade is 
.

please suggest me what I am doing wrong.

physu
  • 169
  • 9

1 Answers1

0

When you enter your age, this is what is in the input buffer just before the scanf(%d) starts retrieving characters:

45<newline>

The scan(%d) skips any white space in the buffer (there isn't any), reads the 45, but leaves the newline. Then when you enter your GPA, this is what is in the input buffer just before the scanf(%lf) starts retrieving characters:

<newline>4<newline>

The scan(%lf) skips any white space in the buffer (the newline), reads the 4, but again leaves the newline. In other words, the leading newline doesn't matter if your next next scanf also skips whitespace such as moving from the first entry to the second.

But scanf(%c) does not skip white space. It simply reads the next character, which is a newline:

<newline>A<newline>

That's why tour period appears on the following line, it has read a character, but that character was the newline rather than the grade.

Section 7.21.6.2 The fscanf function, in the ISO C11 standard, has this to say on the first step of processing a conversion specifier (my emphasis):

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

A quick fix would be to manually skip whitespace yourself, something like:

printf("Enter your grade: ");
scanf(" %c", &grade);
printf("Your grade is %c.", grade);

as per that same section of the standard:

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953