C - User Input
C - User Input
C - User Input
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 −
#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
6/16/24, 10:59 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()
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
6/16/24, 10:59 AM C - User Input
Going back to the previous example, we shall use the scanf() function to accept the
value for "price" and "qty", instead of assigning them any fixed value.
#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.
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 3/12
6/16/24, 10:59 AM C - User Input
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 way, the program can be run any number of times with different inputs.
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.
#include <stdio.h>
int main(){
int num;
return 0;
}
Output
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
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 4/12
6/16/24, 10:59 AM C - User Input
Enter.
#include <stdio.h>
int main(){
return 0;
}
Output
or
Float Input
For floating point input, you need to use the %f format specifier.
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 5/12
6/16/24, 10:59 AM C - User Input
float num1;
return 0;
}
Output
The scanf() function may read inputs for different types of variables. In the following
program, user input is stored in an integer and a float variable −
#include <stdio.h>
int main(){
int num1;
float num2;
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 6/12
6/16/24, 10:59 AM C - User Input
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.
#include <stdio.h>
int main(){
char ch;
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 7/12
6/16/24, 10:59 AM C - User Input
The following program reads two characters separated by a whitespace in two char
variables.
#include <stdio.h>
int main(){
return 0;
}
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.
#include <stdio.h>
int main(){
char ch;
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 8/12
6/16/24, 10:59 AM C - User Input
return 0;
}
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.
The following program shows how you can read a series of characters till the user
presses the Enter key −
#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')
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 9/12
6/16/24, 10:59 AM C - User Input
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.
#include <stdio.h>
int main(){
char name[20];
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 10/12
6/16/24, 10:59 AM C - User Input
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.
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[20];
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 11/12
6/16/24, 10:59 AM C - User Input
https://www.tutorialspoint.com/cprogramming/c_user_input.htm 12/12