Most of the time we have to take input from the user in our programs. In the C language, a user can give input using the console and we can assign that value in some variables.
Here is a syntax for taking input, the output from the user.
Standard File |
File Pointer |
|
Take input |
stdin |
|
Take output |
stdout |
The getchar() & putchar() functions
The getchar() function reads the variable assigned by the user and returns it as an integer. This function only reads only a single character at a time and can be used in the loop.
The putchar() function is used to assign the passed character on the screen and return the same character back. Also, you can use this function inside the loop to display more than one character.
Here is a C program to take input from a user.
Program:
#include <stdio.h> int main( ) {
int c;
printf( "Enter your name: "); c = getchar( );
printf( "\nYou entered: "); putchar( c );
return 0; } |
Output:
Enter your name: alex
You entered: a
Program:
#include <stdio.h> int main( ) {
char str[100]; int i;
printf( "Enter your name :"); scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0; } |
Output:
Enter your name: alex
You entered: alex
Now here we are completed with our basic knowledge of C language programming. To improve your coding skills in this language make sure to do practise daily. Try to perform program exercises that will help you build your logical and coding skills. If you are having any problem during this tutorial then please let us know about this in the comments. We are here to help you anytime