C - User Input
C - User Input
C - User Input
Need for User Input in C
Every computer application accepts certain data from the user, performs a predefined
process on the same to produce the output. There are no keywords in C that can read user
inputs.
The standard library that is bundled with the C compiler includes stdio.h header file, whose
library function scanf() is most commonly used to accept user input from the
standard input stream. In addition, the stdio.h library also provides other functions for
accepting input.
Example
To understand the need for user input, consider the following C program −
Open Compiler
#include <stdio.h>
int main(){
int price, qty, ttl;
price = 100;
qty = 5;
ttl = price*qty;
return 0;
}
Output
The above program calculates the total purchase amount by multiplying the price and
quantity of an item purchased by a customer. Run the code and check its output −
Total: 500
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 1/12
10/23/24, 11:25 AM C - User Input
For another transaction with different values of price and quantity, you need to edit the
program, put the values, then compile and run again. To do this every time is a tedious
activity. Instead, there must be a provision to assign values to a variable after the
program is run. The scanf() function reads the user input during the runtime, and assigns
the value to a variable.
The scanf() function converts the input to a desired data type with appropriate format
specifiers.
Syntax of Scanf()
This is how you would use the scanf() function in C −
The first argument to the scanf() function is a format string. It indicates the data type of
the variable in which the user input is to be parsed. It is followed by one or more pointers
to the variables. The variable names prefixed by & gives the address of the variable.
%c Character
%d Signed integer
%f Float values
%i Unsigned integer
%lf Double
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 2/12
10/23/24, 11:25 AM C - User Input
Open Compiler
#include <stdio.h>
int main(){
return 0;
}
Output
When the above program is run, C waits for the user to enter the values −
When the values are entered and you press Enter, the program proceeds to the
subsequent steps.
What is more important is that, for another set of values, you don't need to edit and
compile again. Just run the code and the program again waits for the user input. In this
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 3/12
10/23/24, 11:25 AM C - User Input
way, the program can be run any number of times with different inputs.
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
Integer Input
The %d format specifier has been defined for signed integer. The following program reads
the user input and stores it in the integer variable num.
Open Compiler
#include <stdio.h>
int main(){
int num;
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 4/12
10/23/24, 11:25 AM C - User Input
The scanf() function can read values to one or more variables. While providing the input
values, you must separate the consecutive values by a whitespace, a tab or an Enter.
Open Compiler
#include <stdio.h>
int main(){
return 0;
}
Output
or
Float Input
For floating point input, you need to use the %f format specifier.
Open Compiler
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 5/12
10/23/24, 11:25 AM C - User Input
#include <stdio.h>
int main(){
float num1;
return 0;
}
Output
Open Compiler
#include <stdio.h>
int main(){
int num1;
float num2;
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 6/12
10/23/24, 11:25 AM C - User Input
return 0;
}
Output
Run the code and check its output −
Character Input
The %c format specifier reads a single character from the keyboard. However, we must
give a blank space before %c in the format string. This is because the %c conversion
specifier won't automatically skip any leading whitespaces.
If there is a stray newline in the input stream (from a previous entry, for example) the
scanf() call will consume it immediately.
The blank space in the format string tells scanf to skip the leading whitespace, and the
first non-whitespace character will be read with the %c conversion specifier.
Open Compiler
#include <stdio.h>
int main(){
char ch;
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 7/12
10/23/24, 11:25 AM C - User Input
return 0;
}
Output
Open Compiler
#include <stdio.h>
int main(){
return 0;
}
Output
Run the code and check its output −
The stdio.h header file also provides the getchar() function. Unlike scanf(), getchar()
doesn't have a format string. Also, it reads a single key stroke without the Enter key.
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 8/12
10/23/24, 11:25 AM C - User Input
Open Compiler
#include <stdio.h>
int main(){
char ch;
return 0;
}
Output
Run the code and check its output −
Enter a character: W
You entered:
W
You entered character: W
You can also use the unformatted putchar() function to print a single character.
Open Compiler
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 9/12
10/23/24, 11:25 AM C - User Input
#include <stdio.h>
int main(){
char ch;
char word[10];
int i = 0;
printf("Enter characters. End by pressing the Enter key: ");
while(1){
ch = getchar();
word[i] = ch;
if (ch == '\n')
break;
i++;
}
printf("\nYou entered the word: %s", word);
return 0;
}
Output
String Input
There is also a %s format specifier that reads a series of characters into a char array.
Open Compiler
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 10/12
10/23/24, 11:25 AM C - User Input
#include <stdio.h>
int main(){
char name[20];
return 0;
}
Output
Run the code and check its output −
C uses the whitespace as the delimiter character. Hence, if you try to input a string that
contains a blank space, only the characters before the space are stored as the value.
The gets() function overcomes this limitation. It is an unformatted string input function. All
the characters till you press Enter are stored in the variable.
Open Compiler
#include <stdio.h>
#include <stdlib.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 11/12
10/23/24, 11:25 AM C - User Input
char name[20];
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 12/12