Input and Output in C – Input refers to the act of providing data to software.
A file or the command line can be used to provide an input.
The built-in functions in C programming make it possible to read the input and pass it along to the program as needed.
The term “output” refers to the presentation of data in a file, on the screen, or on a printer.
A collection of built-in functions for C programming are available to output data to the computer screen as well as save it to text or binary files.
Standard Files
Every device is treated as a file in C programming.
In order to allow access to the keyboard and screen while a program runs, the following three files are automatically opened.
Devices like the display are thus accessed in the same way as files.
Standard File | File Pointer | Device |
---|---|---|
Standard input | stdin | Keyboard |
Standard output | stdout | Screen |
Standard error | stderr | Your screen |
Example 1 of Output in C
#include <stdio.h> int main() { // Displays the string inside quotations printf("C Programming Tutorial in Input and Output"); return 0; }
Output of the above example:
C Programming Tutorial in Input and Output
How does this program work?
- The main() function must be present in every legitimate C program. The main() function is where the code execution starts.
- A library function called printf() is used to display formatted output on screens. The string is printed within quote marks by the function.
- We must use the #include stdio.h> declaration to include the stdio.h header file in order to use printf() in our application.
- The main() function’s return 0; line represents the program’s “Exit status.” It’s not required.
You can test the above example here! ➡ C Online Compiler
Example 2 of Integer Output in C
#include <stdio.h> int main() { int exampleInteger = 45; printf("Number = %d", exampleInteger); return 0; }
Output of the above example:
Number = 45
To print int types, we utilize the percent d format specifier.
Here, the value of exampleInteger will be used in place of the percentage d enclosed in quote marks.
You can test the above example here! ➡ C Online Compiler
Example 3 of float and double Output in C
#include <stdio.h> int main() { float testnum1 = 45.5; double testnum2 = 35.4; printf("number1 = %f\n", testnum1); printf("number2 = %lf", testnum2); return 0; }
Output of the above example:e
number1 = 45.500000
number2 = 35.400000
We utilize the percent %f format specifier to output float. In the same manner, we print double numbers by using percent %lf.
You can test the above example here! ➡ C Online Compiler
Example 4 of Print Characters in C Output
#include <stdio.h> int main() { char chr = 'x'; printf("character = %c", chr); return 0; }
Output of the above example:
character = x
We employ the percent %C format specifier to print char.
You can test the above example here! ➡ C Online Compiler
Input in C
One of the often used functions in C programming to receive user input is scanf().
Using common input devices like keyboards, the scanf() function reads formatted input.
Example 5 of Integer Input and Output in C
#include <stdio.h> int main() { int exampleInteger; printf("Enter an integer: "); scanf("%d", &exampleInteger); printf("Number = %d",exampleInteger); return 0; }
Output of the above example:
Enter an integer:30
Number = 30
In order to accept int input from the user, we have utilized the percent d format specifier inside the scanf() function.
An integer is entered by the user and kept in the testInteger variable.
You can test the above example here! ➡ C Online Compiler
Example 6 of Float and Double Input and Output in C
#include <stdio.h> int main() { float number1; double number2; printf("Enter a number: "); scanf("%f", &number1); printf("Enter another number: "); scanf("%lf", &number2); printf("number1= %f\n", number1); printf("number2= %lf", number2); return 0; }
Output of the above example:
Enter a number:45
Enter another number:69
number1= 45.000000
number2= 69.000000
For float and double, we utilize the percent %f and percent %lf format specifiers, respectively.
You can test the above example here! ➡ C Online Compiler
Example 7 of Character Input and Output in C
#include <stdio.h> int main() { char character; printf("Enter a character: "); scanf("%c",&character); printf("You entered %c.", character); return 0; }
Output of the above example:
Enter a character: x
You entered x.
The character itself is not kept when a character is typed into the application mentioned above by the user.
An integer value (ASCII value) is instead kept on file.
You can test the above example here! ➡ C Online Compiler
Input and Output Multiple Values in C Program
Here’s how to display multiple user inputs after accepting numerous inputs.
Most of the time, novice programmers are looking for methods to have a function return more than one value.
Unfortunately, C directly prohibits this.
Example 9 of Input and Output Multiple Values in C
#include <stdio.h> int main() { int x; float y; printf("Enter integer and then a float: "); // Taking multiple inputs scanf("%d%f", &x, &y); printf("You entered %d and %f", x, y); return 0; }
Output of the above example:
Enter integer and then a float: 45
5.9
You entered 45 and 5.900000
You can test the above example here! ➡ C Online Compiler
Format Specifiers for Input and Output in C
From the above examples, you can see that we use
%d
forint
%f
forfloat
%lf
fordouble
%c
forchar
This section lists the most popular C data types and the format specifiers used with them.
Data Type | Format Specifier |
---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %Lf |
Summary
In this course, we learned about the C language’s input and output.
We learned how to utilize the printf() and scanf() functions in code to display output to and collect input from users, respectively.
A few other functions for displaying results and accepting user input were also covered.
If you have any questions or suggestions about What is Formatted Input and Output Function in C Language, please feel free to leave a comment below.
About Next Tutorial
In the next following post, I’ll create a File Input and Output in C and try to explain its many components in full detail.
I hope you enjoy this post on What is Formatted Input and Output Function in C Language, in which I attempt to illustrate the language’s core grammar.
PREVIOUS
NEXT