I have seen a simple program for handling Link List operations. The following is the part of the code that contains the problem. The code is modified but contains all necessary information regarding the problem.
In this, the link list is created and initialized. There are certain printf() statements I've inserted for better understanding of the problem. The llist() function prompts the user to enter a number, creates a node, initializes it and attaches it to LL, and then asks user if he has any node to be inserted. The choice is read in a "char" variable "ch" which is initialized to 'y'.
1. If we use "scanf("%c",&ch );", it never accepted the choice "ch" and directly returned to main().
2. If we use string format specifier %s in place of %c (as used in above code snippet), it accepts choice "ch", but manipulates the addresses of LL. The following is a sample output. It will give an Idea what is happening.
3. When we use "int ch" in place of "char ch", and give option as "Enter 0 for exit" and do necessary modifications in code, the whole thing works perfectly and we get desired output.
So, I don't understand why it is not accepting "char" (%c) but accepts "string" (%s) but gives problem and why no problem with "int"(%d)? How is it related to keyboard buffer? Please explain this.
Thank you.
Code:
#include<stdio.h> #include<stdlib.h> #include<conio.h> struct llist { int val; struct llist *next; }; typedef llist item; void llist(item **head); void main() { item *head; llist(&head); } void llist(item **head) { int i=1, no; char ch='y'; item *curr, *temp; while(ch=='y') { printf("Enter no: "); scanf("%d",&no); if(i==1) { (*head)=(item*)malloc(sizeof(item)); (*head)->val=no; (*head)->next=NULL; temp=(*head); i++; printf("After First node allocation : *head = %u temp = %u\n\n",*head, temp); } else { printf("Before next node allocation : temp = %u\n", temp); curr=(item*)malloc(sizeof(item)); curr->val=no; curr->next=NULL; temp->next=curr; temp=curr; printf("After compliting next node allocation : curr=%u temp=%u\n\n",curr,temp); } printf("Do you want to add node?(y/n) : "); scanf("%s",&ch); /**** Use %c here ****/ } }
1. If we use "scanf("%c",&ch );", it never accepted the choice "ch" and directly returned to main().
2. If we use string format specifier %s in place of %c (as used in above code snippet), it accepts choice "ch", but manipulates the addresses of LL. The following is a sample output. It will give an Idea what is happening.
Code:
Enter no: 123 After First node allocation : *head = 3289416 temp = 3289416 Do you want to add node?(y/n) : y Enter no: 234 Before next node allocation : temp = 3289344 After completing next node allocation : curr=3289432 temp=3289432 Do you want to add node?(y/n) : y Enter no: 345 Before next node allocation : temp = 3289344 After completing next node allocation : curr=3289448 temp=3289448 Do you want to add node?(y/n) : y Enter no: 456 Before next node allocation : temp = 3289344 After completing next node allocation : curr=3289464 temp=3289464 Do you want to add node?(y/n) : n
So, I don't understand why it is not accepting "char" (%c) but accepts "string" (%s) but gives problem and why no problem with "int"(%d)? How is it related to keyboard buffer? Please explain this.
Thank you.
Comment