hi i have a simple program i cant exit the program
Code:#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int x; printf("press 3 to exit"): scanf("%i",x): while(x!=3) // i want to exit from the loop if its 3 isthis right { }
hi i have a simple program i cant exit the program
Code:#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int x; printf("press 3 to exit"): scanf("%i",x): while(x!=3) // i want to exit from the loop if its 3 isthis right { }
Code:scanf("%i",&x):
Mainframe assembler programmer by trade. C coder when I can.
This program will work . But if you give any non-numeric characters then the loop will run unterminatly.For that you have to check the input before processing that .
Learn the basic first . C Programming Tutorial .Code:#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int x; printf("press 3 to exit"); scanf("%i",&x); while(x!=3) // i want to exit from the loop if its 3 isthis right { scanf("%i",&x); } }
Both fail in terms of error checking.
You should be checking the return value of scanf(). As it stands if scanf() fails the default uninitialised value of x will be used for the loop.