I/O in C
Chapter: Input and Output in C
Input and output (I/O) operations are essential in any program, as they allow interaction with the user or external devices. In C, I/O operations are handled using functions from the standard input/output library (<stdio.h>
). This chapter covers various methods for reading inputs and displaying outputs, ranging from simple console I/O to working with files.
1. Introduction to I/O in C
C provides three standard streams for input and output:
Standard Input (
stdin
): Used for reading input, typically from the keyboard.Standard Output (
stdout
): Used for displaying output, typically to the screen.Standard Error (
stderr
): Used for error messages or diagnostics.
The most commonly used I/O functions in C are:
printf
andscanf
for formatted I/O.putchar
andgetchar
for character-based I/O.puts
andgets
for string-based I/O.
2. Console Input and Output
2.1 Printing Output with printf
printf
The printf
function is used for formatted output.
Syntax:
The format string contains text and format specifiers (e.g.,
%d
,%s
) to display variables.The arguments correspond to the format specifiers.
Example:
Output:
Common Format Specifiers:
%d
Integer
123
%f
Floating-point number
123.45
%c
Character
'A'
%s
String
"Hello"
%x
Hexadecimal
7b
(for 123
)
%o
Octal
173
(for 123
)
%%
Prints %
character
%
2.2 Reading Input with scanf
scanf
The scanf
function is used to read formatted input.
Syntax:
The format string specifies the data type of the input.
The
&
(address-of operator) is used to pass the memory address of the variable where the input will be stored.
Example:
Output:
Notes on scanf
:
Multiple inputs can be read at once:
scanf
stops reading strings at spaces. Usefgets
for multi-word input.
2.3 Character Input and Output
For single-character I/O, use getchar
and putchar
.
getchar
: Reads a single character from the input.Example:
putchar
: Prints a single character to the output.Example:
Example Program:
Output:
2.4 String Input and Output
gets
: Reads an entire line, including spaces. Avoidgets
, as it can cause buffer overflows.fgets
(Preferred): Reads a line of text safely, including spaces.Example:
puts
: Prints a string followed by a newline.Example:
Example Program:
3. File Input and Output
File I/O allows reading from and writing to files. The <stdio.h>
library provides functions like fopen
, fprintf
, fscanf
, fclose
, etc.
3.1 Opening and Closing Files
Use fopen
to open a file:
Modes include:
"r"
: Read mode."w"
: Write mode (overwrites the file)."a"
: Append mode."r+"
: Read and write.
Use fclose
to close the file:
3.2 Writing to a File
Use fprintf
or fputs
for writing data to a file.
Example:
3.3 Reading from a File
Use fscanf
or fgets
to read data from a file.
Example:
4. Formatted and Unformatted I/O
4.1 Formatted I/O
Formatted I/O functions, like printf
and scanf
, allow the use of format specifiers for structured data input and output.
4.2 Unformatted I/O
Unformatted I/O functions, like getchar
, putchar
, gets
, and puts
, deal directly with raw data.
5. Best Practices for I/O in C
Validate Inputs: Always check for valid inputs to prevent errors or undefined behavior.
Handle File Errors: Check file pointers for
NULL
afterfopen
.Use Safe Functions: Prefer
fgets
overgets
, as it avoids buffer overflows.Close Files: Always close files after reading or writing.
6. Sample Questions and Solutions
Question 1: Write a program to read and print a sentence using fgets
and puts
.
fgets
and puts
.Solution:
Question 2: Write a program to copy content from one file to another.
Solution:
Summary
Input and output operations are fundamental for creating interactive and file-based C programs. In this chapter, we covered console I/O using printf
, scanf
, getchar
, and putchar
, as well as file I/O with fopen
, fscanf
, and fprintf
. By understanding these concepts, you can create robust programs that interact with users and external files. Practice the sample questions to solidify your skills.
Last updated
Was this helpful?