Input and output (I/O) operations are essential aspects of C programming, allowing you to interact with users and handle data. In this article, we’ll delve into the basics of input and output operations in C, providing real-world examples and output demonstrations to help you understand and apply these concepts effectively. Understanding input and output operations is crucial for any C programmer. In this article, we’ve covered the basics of performing input and output operations, both for keyboard interactions (printf() and scanf()) and file operations (fprintf() and fscanf()).
Output Operations
Printing Text with printf()
The printf()
function is used to display text and data on the screen. You can format the output using format specifiers such as %d
, %f
, %c
, and %s
for integers, floats, characters, and strings, respectively.
Output:
Writing to Files with fprintf()
You can also use fprintf()
to write formatted text and data to files. This is especially useful for creating and manipulating data files.
Output (output.txt):
Input Operations
Reading from the Keyboard with scanf()
The scanf()
function is used to read data from the keyboard. You should specify format specifiers to match the data types you want to read.
Output (After User Input):
Reading from Files with fscanf()
Similarly, you can use fscanf()
to read formatted data from files. This is useful for processing data stored in files.
Input File (input.txt):
Output: