AkShay Mahajan
AkShay Mahajan

Reputation: 45

Regarding structures in C

If I try to run this code then it doesn't ask me the value of s2.name. Why is it so?

#include<stdio.h>

int main()
{
    struct student
    {
         char name;
         int roll;
         int age;
    };

    struct student s1;
    struct student s2;

    printf("Enter name of the student: ");
    scanf("%c", &s1.name);
    printf("%c", s1.name);
    printf("\n");

    printf("Enter name of the student: ");
    scanf("%c", &s2.name);
    printf("%c", s2.name);

    return 0;
}

Upvotes: 1

Views: 57

Answers (2)

hm1912
hm1912

Reputation: 314

You are basically inputting two characters: - the one that you type - the newline character `\n` because you hit `enter` A solution to this problem is clearing stdin after reading in the first "name":

#include<stdio.h>

int main()
{
    struct student
    {
         char name;
         int roll;
         int age;
    };

    struct student s1;
    struct student s2;

    printf("Enter name of the student: ");
    scanf("%c", &s1.name);
    printf("%c", s1.name);
    printf("\n");
    fflush(stdin); //only works on windows, clears the input buffer
    printf("Enter name of the student: ");
    scanf("%c", &s2.name);
    printf("%c", s2.name);

    return 0;
}

Another way to clear the input buffer is:

while (getchar() != '\n');

This reads in all characters from the input buffer, because as soon as input is read by functions like getchar() or scanf(), the input is removed from stdin.
EDIT:
When you input values using getchar(), scanf(), etc., then the symbols that you type into stdin (most of the times via a keyboard) are stored in the input buffer at first (stdin). getchar() or any similar function then takes the values that it should read in, out of the input buffer. For example:

scanf("%d", &var);
scanf("%d", &var2);

If I input 5x, the characters in the input buffer are '5', 'x' and \n (because you hit the enter key). The first scanf() then takes out the '5', as it fits the format string %d. After that, the characters in the input buffer are 'x' and \n. In this case scanf returns 1, because it read in one value correctly.
When it then continues to the second one, scanf() won't even let you type anything, as there is already something in the input buffer stdin. It won't store any data however, because the first "item" in stdin ist 'x'. That doesn't fit the format string %d. The compiler then doesn't continue to read. In this case scanf would return 0, as no value was read in correctly.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

When you input a single character and press the Enter key, you are actually inputting two characters: The character in your input and a newline from the Enter key.

The second scanf reads this newline.

Or if you give multiple characters as input to the first name, then the second character will be read by the second scanf.


The way to solve the first problem is easy: Tell scanf to read and discard leading white-space (which newline is) by adding a single space in front of the format, like

scanf(" %c", &s2.name);
//     ^
// Note space here

The way to solve the second problem is to read strings instead, which means you have to turn your name members into arrays and then use the "%s" format (preferably with a specified width so you don't read to many characters).

Upvotes: 2

Related Questions