0

Here, if I don't use getchar() the output is misbehaving:

Check here to see the output console

What exactly is the getchar() doing here ?

And what is it holding until another input gets invoked ?

And how will the next input be invoked here in this case ?

My code:

//To take 3 students data using stucture which is user defined.
#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
struct student
{
    char reg_number[200];
    char name[200];
    int marks_obt;
};
int main()
{
    struct student stud1,stud2,stud3;
    printf("Enter registration number of student 1 : ");
    gets(stud1.reg_number);
    printf("Enter name of student 1 : ");
    gets(stud1.name);
    printf("Enter marks of student 1 : ");
    scanf("%d",&stud1.marks_obt);
    system("cls");
        //getchar();
    printf("Enter registration number of student 2 : ");
    gets(stud2.reg_number);
    printf("Enter name of student 2 : ");
    gets(stud2.name);
    printf("Enter marks of student 2 : ");
    scanf("%d",&stud2.marks_obt);
    system("cls");
        //getchar();
    printf("Enter registration number of student 3 : ");
    gets(stud3.reg_number);
    printf("Enter name of student 3 : ");
    gets(stud3.name);
    printf("Enter marks of student 3 : ");
    scanf("%d",&stud3.marks_obt);
    system("cls");
    
    printf("ID of student 1 is %s \n Name of student 1 is %s \n Marks obtained by stdent 1 is %d",stud1.reg_number,stud1.name,stud1.marks_obt);
    printf("\n\n");
    printf("ID of student 2 is %s \n Name of student 2 is %s \n Marks obtained by stdent 2 is %d",stud2.reg_number,stud2.name,stud2.marks_obt);
    printf("\n\n");
    printf("ID of student 3 is %s \n Name of student 3 is %s \n Marks obtained by stdent 3 is %d",stud3.reg_number,stud3.name,stud3.marks_obt);
    //getch();
} 
anastaciu
  • 23,467
  • 7
  • 28
  • 53
Srijoy_paul
  • 77
  • 1
  • 6

2 Answers2

3

First of all, never use the gets(). It is deprecated from the C and C++ standards. The safe way would be to apply fgets():

char input[128];
if (fgets(input, sizeof input, stdin) == NULL) {
  // Oops... input was incorrectly given, handle the error
  return EXIT_FAILURE; // To quit
}

// Removing the trailing newline character
input[strcspn(input, "\n")] = 0;

You're facing an overlap when asking for another input from the user in the next gets() call because you hit the Return key as well when you finish typing with scanf(). A newline key is added at the end.

The getchar() takes the newline, so it is discarded and does the job you expect.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
2

There are already comments and a correct answer that should be enough, but judging by your last comment it seems that you didn't yet understand what's going on.

This line:

scanf("%d",&stud1.marks_obt);

Parses an integer, for that you press Enter, when you do that a \n is added to the stdin buffer, and remains there, it is not parsed by scanf.

After that:

gets(stud2.reg_number);

Will parse that \n character and store it in stud2.reg_number, and the program moves on.

When you use getchar(), it parses the \n and leaves the stdin buffer empty again so the gets has nothing to parse and waits for your input, correcting the problem, it's still a flimsy solution, but it works in this particular case.

Moral of the story, don't mix scanf with gets, fgets at least, since, as stated, gets was removed from the standard in C11. The reason why some compilers still support it is beyond me.

anastaciu
  • 23,467
  • 7
  • 28
  • 53