Input and Output (I/O)
In this you will learn to use scanf() function to take input from user and printf() function to display the result to the user.
C Output
In C programming, Printf() funcation is the main output function to display result to user. The function sends formatted output to the screen.
Example 1 : C Output
Output
C Programming
How does this program work
- All valid C program must contain the main() function.The codes execution begins from the start the main() function.
- The printf() is a library function to send formatted output to the screen. The function prints the string inside the quotations.
- To use prinf() in our program, we need to include stdio.h header file the #include<stdio.h> statement.
- The return 0; inside the main() function is the " Exist status " of the program.
Example 2 : C Integer Output
Output
Number =5
We use
%d specifier to print
int types. Here, the
%d inside the quotations will be replaced the value of testInteger.
Example 3 : C float and double Output
Output
number1 = 13.5000
number2 = 12.4000
To print float, we use %f format specifier, similarly we use %if to print double
values.
Example 4 : C print character
Output
character = a
To print char , we use %c format specifier.
Input
In C programming, scanf is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyword.
Example 5 : C integer Input/Output
Output
Enter an integer : 4
Result
Number = 4
Here, we have used %d format specifier inside the scanf() function to take int from the user. When the user entered an integer, it is stored in the testinteger variable.
Example 6 : C float and double Input/Output
Output
Enter a number =12.54
Enter another number =34.54
num1=12.54
num2=34.54
We use
%f and %if
format specifier for
float and
double respectively.
Example 7 : C Character Input/Output
Output
Enter a character : a
You entered a
When a character entered by the user in the above program, the character itself is not stored. Instead, an integer value
ASCII is stored.
And when we display that value using
%c text format, the entered character is displayed. If we use
%d to display the character, its
ASCII value is printed.
Example 8 : C ASCII value Input/Output
Output
When a character :g
you entered :g
ASCII value is 103
Input/Output Multiple Values
Output
Enter integer and then float : -3
3.5
You entered -3 and -3.50000
Format Specifier for I/O
- %d <=====> Interger_value
- %f <=====> float_value
- %if <=====> double_value
- %c <=====> Character_value