C – User Input

To get user input in the C language, use the scanf(). It is used to read the data input by the user on the console. Let us see some example C programs to get input from the user.

Before moving further, we’ve prepared a video tutorial to get user input in C:

Video: User Input in C

Video: User Input in C (Hindi)

C program to print an integer entered by the user

Let us see an example to enter an integer from the user using scanf() and display using printf():

#include <stdio.h>

int main() {
  int i;    
  
  // Asks the user to enter a number
  printf("Enter a number = ");    
  scanf("%d",&i);    
 
  // Displays the number entered by the user above
  printf("\nThe number entered by user = %d ",i);    
  
  return 0;
}

Output

Enter a number = 10
The number entered by user = 10

C program to print the multiplication of integers entered by the user

Let us see another example wherein we will get three numbers from the user and multiply them:

#include <stdio.h>

int main() {
  int a, b, c; // Three numbers
  int res;
  
  // Asks the user to enter three numbers one-by-one
  printf("Enter the 1st number = ");    
  scanf("%d",&a); 
  printf("Enter the 2nd number = ");    
  scanf("%d",&b); 
  printf("Enter the 3rd number = ");    
  scanf("%d",&c); 
 
  // Multiply the numbers entered by the user
  res = a*b*c;
  printf("Multiplication Result = %d ",res);    
  
  return 0;
}

Output

Enter the 1st number = 5
Enter the 2nd number = 10
Enter the 3rd number = 15
Multiplication Result = 750

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

Strings in C
C - Functions
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment