Standard Input-Output Device
There are two types of the input-output device present in our computer. They are:
Keyboard: It is a standard Input Device.
Monitor: It is a standard Output Device.
In the C language, two predefined functions exist in the C library. They are printf () function and scanf () function.
printf ()
printf () is not a keyword.
printf () is a predefined function.
With the help pf printf () function, there are two types of messages printed on the screen:
1. Printing Text as it is.
2. Printing value of expression or value of the variable.
Programming Example 1
Output
“ \n ” is a special type of character called newline character. The function of “\n” does not print itself on the screen but changes the cursor position to the new line after printing a statement. \n is anEscape Sequence.
How to Print the Value of a Variable?
Programming Example 2
Output
Explanation
To print the value of a variable, we use %d or %f or %c. There are special types of Format Specifier, which does not print themselves but helps to print values of different types of variables like int, float, char, etc.
We want to see the value, a= 4, then we write:
We want to see the value of a is 4, then we write:
We want to see if the value of a is 4 and b is 5; we write:
Programming Example 3
int main ()
{
int a=4, b=5;
printf (" Sum of %d and %d is %d", a, b, a+b);
return 0;
}
Output
Explanation
Here we declare two variables, a and b. Initialization is done at the time of declaration. We assign values 4 to a variable and 5 to b variable. Then we sum these two values as a + b directly in the standard output function printf () function. Inside the printf () function, we use format specifier ( %d ), which does not print itself but helps to print the values of the integer type variable.
Programming Example 4
Output
Explanation
“gotoxy ()“ is a predefined function, which function is to move the cursor position anywhere in our output screen. But this particular function runs on turbo c IDE. But if we want to run this function on other IDE like Linux, it does not work. We have to use the gotoxy () function as a user-defined function. gotoxy () function is not an inbuilt function here. In the output screen, we write a maximum of 80 characters and 25 lines in a line. With the help of this function, we can move the cursor as we like on the screen.
scanf()
scanf () is not a keyword.
scanf () is a predefined function.
scanf ( “ format specifier “, variable address ) ;
printf () To print some value on the screen, we use printf ().
scanf () To take an input value from the keyboard, we use scanf ().
scanf () eco the value which we press on the keyboard. getch () gets only one character, but scanf () takes multiple digits until we press ENTER from the keyboard.
Programming Example 5
Output
Explanation
We want to show how the input instruction scanf () function works. We declare a variable named number. We want to take a value from the keyboard or the user. We use standard input instruction or inbuilt function scanf () function. It helps the programmer store the value that the user will get to a specific address where our variable exists. Scanf () function performs this action with the help of the address of the ( & ) operator.
Programming Example 6
Output
Explanation
This programming example declares three integer type variables x, y, and z. We want to sum up the two variables. We take two integer values from the user using the input instruction scanf () function. Then add these two values using add operator and keep the added value to the variable z. Now we print the value of z using the output instruction printf () function.
Conclusion
We have understood from the above example that input-output instructions are widely used in every C program. Without these, we cannot build any robust program. This helps to take input from the user and shows the output to the user. These are the base of any program like a C program.