So I'm stuck on this one. When I put an fgets after scanf, the fgets couldn't store space inputs. A quick googling taught me that This happens because every scanf() leaves a newline character in a buffer that is read by the next scanf. and putting a space or newline at the end of the control string fixes it... Yep it did but the problem I am facing is that if there's a printf between scanf and fgets, the fgets work before the printf.
So, I need an explanation for this behavior + any solutions/workarounds if any.
Compiler/Platform used: gcc 11/Linux
Here is the code (LOOK AT LINE 24-27 for example)
/*Write a program to create a structure of employees containing the following data members: Employee ID, Name, Age, Address, Department and Salary.
 Input data for 10 employees and display the details of the employee from the employee ID given by the user.*/
#include <stdio.h>
#include <string.h>
int main()
{
   int i=0;
   typedef struct employee
   {
      long int id;
      char name[30];
      int age;
      char address[100];
      char dept[30];
      long int salary;
   }emp;
   emp list[5];
   for (i = 0; i<10;i++)
   {
      printf("Enter id of employee %d : ", i+1);
      scanf("%ld ", &(list[i].id));
      printf("Enter name of employee %d : ", i+1);
      fgets(list[i].name, 30, stdin);
      printf("Enter age of employee %d : ", i+1);
      scanf("%d ", &(list[i].age));
      printf("Enter address of employee %d : ", i+1);
      fgets(list[i].address, 100, stdin);
      printf("Enter dept of employee %d : ", i+1);
      fgets(list[i].dept, 30, stdin);
      printf("Enter salary of employee %d : ", i+1);
      scanf("%ld ", &list[i].salary);
   }
   long int emp_id;
   int flag=0;
   printf("Enter employee id to be searched: ");
   scanf("%ld", &emp_id);
   // Linear search
   for (i=0; i<10; i++)
   {
      if (list[i].id==emp_id)
      {
         printf("Employee found!!");
         flag=1;
         printf("Id : %ld \n", list[i].id);
         printf("Name : %s \n", list[i].name);
         printf("Age : %d\n", list[i].age);
         printf("Address : %s \n", list[i].address);
         printf("Dept : %s \n", list[i].dept);
         printf("Salary : %ld \n", list[i].salary);
      }
   }
   if (flag==0)
   {
      printf("Employee not found!!");
   }
}
Output

 
     
    