Input, process and output processed data is the main goal of every computer program. A program without data is a useless program.
In this post we will learn some basic C functions to input and output value of a variable.
C provides a header file stdio.h
(Standard Input Output) that contains various Input/Output functions. In order to perform any I/O operation you must add an include statement at the top of your C code.
#include <stdio.h>
Character input and output
C language provides two function for character input/output getchar()
and putchar()
.
Input a character using getchar()
In C we use getchar()
function to read single character from keyboard. The getchar()
function returns an integer i.e. ASCII value of input character.
getchar()
function waits for input till user presses any character. It accepts any character as an input. Hence be cautious while using getchar()
. As non-printable characters like blank space, new lines, tab etc. are also considered input to getchar()
.
Output a character using putchar()
We use putchar()
function in C to display or output a character on console (monitor). The putchar()
function takes an integer argument (ASCII value of character) and displays its character representation on console.
Example program to demonstrate character input/output in C
/**
* C program to demonstrate character input output
* using getchar() and putchar() function.
*/
#include <stdio.h>
int main()
{
char grade;
printf("Enter student grade: ");
/* Input character using getchar() and store in grade */
grade = getchar();
/* Output grade using putchar() */
putchar(grade);
return 0;
}
Input using scanf()
function
scanf()
is a versatile function to read input from standard input device (keyboard). It can input any primitive or derived type.
Read more – List of all primitive and derived types in C
Syntax of scanf()
function
scanf("format-specifiers", var1-memory-address, ... , varN-memory-address);
scanf()
accepts a string containing list of format specifiers. Format specifier is a special string starts with %
symbol, followed by a character. It directs scanf()
or printf()
function to input or output a variable value of some type.
For example – %d
is a format specifier that tells scanf()
to read an integer value. %f
is used for float
. Likewise there are lot of format specifiers for different types.
Read more – List of all format specifiers in C
Next parameter, var1-memory-address directs scanf()
to store the input value to some memory location (variable). Here we provide a list of variables to store input data. The variable type should be compatible with mentioned format specifier. In addition, scanf()
accepts variable memory address not variable name. Hence, we must prefix variable names with &
symbol.
We use &
(address operator) to get actual memory address of any variable.
Output using printf()
function
We use printf()
function to print/display output on console (monitor).
Syntax of printf()
function
printf("format string containing format-specifiers", variable_list);
printf()
function accepts a format string as first parameter. Format string contains string or message we want to print on screen. Optionally, it may contain format specifiers in case we need to print variable value.
printf()
replaces all occurrence of format specifiers with the variable value. Optionally it typecasts a variable value to specifier type if needed.
Example program to input/output variable using scanf()
and printf()
function
/**
* C program to demonstrate input output using scanf() and print()
*/
#include <stdio.h>
int main()
{
int num1, num2, sum;
/* Simple message to user */
printf("Enter two numbers: ");
/* Input value in two variables */
scanf("%d%d", &num1, &num2);
/* Find sum */
sum = num1 + num2;
/* Print sum */
printf("Sum = %d", sum);
return 0;
}
Let us observe the above program.
- Statement
int num1, num2, sum;
declares three integer variables. - Next,
printf("Enter two numbers: ");
display simple message to user to input two numbers. It is considered as a good programming practice to display a message before every input to user. Doing this will notify the user to provide some input. - In statement
scanf("%d%d", &num1, &num2);
,%d
tellsscanf()
function to read an integer variable and store innum1
. Similarly, second%d
also tellsscanf()
to read integer value and store innum2
. - The statement
sum = num1 + num2
calculates sum ofnum1
andnum2
and stores insum
. - Finally, in the statement
printf("Sum = %d", sum);
Sum = is printed as it is as a simple message.
Next, afterprintf()
encounters format specifier%d
. It replaces%d
with the value ofsum
, which finally prints sum of both numbers.
Hope, I didn’t puzzled you with this. Feel free to drop your queries in comments section if any.
Read more – Program to perform input output of all primitive types