In this article, we are going to learn how to take input from the user and print it. Here we will learn, How to take integer input from users in the C program. How to take character input from users in the C program and the concepts to do these in C language. Also, we will learn how to print integers entered by the user in the C program and how to print character input entered by the user in the C program.
1. C Program to take character input from users and print
Here we are making use of the scanf() function to take the user inputs. By using %d for an integer and %c for a character we are specifying the format of the input that we are expecting from the user.
#include <stdio.h>
int main() {
int numInput;
char charInput;
printf("\n\nPlease enter an Character: ");
// User Input the Character
scanf("%c", &charInput);
// Output the Character on Console
printf("\nThanks for entering Character: %c", charInput);
return 0;
}
Output
Please enter an Character: a
Thanks for entering Character: a
2. How to take integer input from user in C
Here we are making use of the scanf() function to take the user inputs. By using %c for a character we are specifying the format of the input that we are expecting from the user.
#include <stdio.h>
int main() {
int numInput;
printf("\n\nPlease enter an integer: ");
// User Input the integer
scanf("%d", &numInput);
// Output the integer on Console
printf("\nThanks for entering Number: %d", numInput);
return 0;
}
Output
Please enter an integer: 12
Thanks for entering Number: 12
3. How to take string input from User in C
In this example, we are taking a string input from a user in the C language. The gets() method takes string input. We are printing the input string using printf() method.
#include <stdio.h>
int main()
{
char mystr[20];
gets(mystr);
printf("We have entered : %s", mystr);
return 0;
}
Output
Welcome to devenum
We have entered : Welcome to devenum