c - If i run this program even if i give the input as 0 it countinuosly getting the values in it? -


#include<stdio.h> #include<conio.h> #include<malloc.h>  void main() {     struct node     {         int data;         struct node *next;     };     struct node *head,*temp;     int x;     clrscr();     head=(struct node *) malloc (sizeof(struct node));     temp=head;     while(temp!=null)     {         scanf("%d",x);         temp->data=x;         if(x==0)         {temp->next=null;}         else         {temp->next=(struct node *) malloc (sizeof(struct node));}         temp=temp->next;     } } 

i writing code simple linke list program ... can run program when press 0 program not stopping..

firstly, scanf wrong. needs passed reference:

scanf("%d", &x); 

secondly, should set x other 0 before scanf'ing it, in case. way written, have no exit condition in loop.

you try using gdb , stepping line line if want right it.

hope helps


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -