Input is important as well as output. What if we are going to write a calculator program. It won't be a good thing if the calculator has been saying 3 + 5 = 8 all the time. The calculator has to show the result based on what users giving. That's input. To take input from users we all do have a keyboard. the best input device.
scanf in c
we have seen that we used printf() to show output. just like that we are going to use scanf() in order to get input from user.- method name: scanf
- first arg: format specifier in string
- from second arg: variable address
int a = 10; scanf("%d", &a);
above code will display blank screen and that screen will wait for users input. when you are pressing any key in keyboard. program will come to an end. so what happened? The key what you pressed will be stored as integer in variable "a". Then there is no line to execute the program. What if we want to display what we enteredTo display use printf
int a;
scanf("%d", &a);
printf("key you entered: %d", a);
Run this code.int a;
scanf("%d", &a);
printf("key you entered: %d", a);
Programs will be closed at the end of program (line). To avoid this or to hold the screen we can use the method getch() or scanf.
These methods will wait for users input. Only after a keypress screen will be closed.
These methods will wait for users input. Only after a keypress screen will be closed.