Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:26:15+00:00 2026-06-15T21:26:15+00:00

I have written a code for Linked List: #include <stdio.h> #include <stdlib.h> #include <string.h>

  • 0

I have written a code for Linked List:

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

struct student{
       char name[256];
       int id;  
       struct student *next;
};
  struct student *start;
  struct student *last;     

void enter(void);
void add( struct student *i,struct student **last);
void display(struct student *first);       

int main(int argc, char *argv[])
{
  start=last=NULL;

   enter();
   enter();
   enter();
   display(start);

  system("PAUSE");  
  return 0;
}

void add( struct student *i,struct student **last)
{
     if (!*last){ *last=i; start=i;}
     else {(*last)->next=i;*last=i;}
     i->next=NULL;

     }
void enter(void)
{
   struct student *info;

   info=(struct student *)malloc(sizeof(struct student));



    printf("Enter the name of the student:\n");
    fgets(info->name,255,stdin);
    fflush(stdin);
    // gets(info->name);
     printf("Enter the student id:\n");
     scanf("%d%",&info->id); 
     add(info,&last);

 }

 void display(struct student *first)
 {
      while(first)
      {
                  printf("\n\n\n\nName: %s\n",first->name);
                  printf("Id: %d\n", first->id);
                  first=first->next;
                  }
  }

It does create a linked list but when i try to input the values: it shows

OUTPUT

For first element it takes the name correctly for id i need to manually enter \n after the id digit (otherwise it does not come out of the scanf mode) and 2nd element onwards it does not prompt to take the name and it skips “Enter name” and asks for id where again I need to manually enter \n. While showing the linked list elements , it does show first name but 2nd name onwards shows \n. I have already used fflush(stdin). Could you please let me know why such behavior? I have attached the o/p pic.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:26:16+00:00Added an answer on June 15, 2026 at 9:26 pm

    If I had a nickel…

    Anyway, there are a number of problems here:

    1) Don’t fflush(stdin), that’s undefined behavior

    2) It’s never a good idea to mix scanfs and fgetss, it just makes things confusing.

    scanf(" %s", info->name); // will do instead of your fgets statement
    

    3) You have an extra % in your scanf:

    scanf("%d%",&info->id);  // should be scanf("%d",&info->id);
    

    For completeness…

    Why you’re having problems right now:

    fgets() takes a string and includes the newline, so when you’re asking for a name you’re getting “name\n”

    scanf() takes the number and leaves the newline. So when you ask for an ID, you’ll get the digit typed, but the newline character '\n' is still sitting in stdin.

    Next time that your fgets() runs it will automatically take that as the input for the second name. Seeming to “skip over” the request for input.


    EDIT

    If the reason you were using fgets() was to read a string with spaces in it (like "first_name last_name") that can be acomplished with scanf() as well using the Negated scanset option:

    scanf(" %[^\n]",info->name); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written this code in JavaScript and works perfectly fine when I include
I have written some code for displaying a drop down list, but the code
I have written my own linked list implementation in C, and it works fine
Have written all the code in a silverlight class library (dll) and linked this
I have written this piece of code here and I have linked it alright
I have a linked list of Nodes, each Node is defined as: struct Node
I have written a code to add a new member to a list .
I have written a function that merges two linked list. (Note that the function
I have written code that opens 16 figures at once. Currently, they all open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.