Unit 3: Unformatted and Formatted I/O

Table of Contents

1. Introduction to I/O Functions

Input/Output (I/O) functions are essential for interaction between the user and the computer. In C, these functions are categorized based on whether they allow the user to control the format of the data being transferred.

2. Formatted I/O: printf() and scanf()

Formatted functions allow the programmer to specify exactly how the data should be read or displayed using format specifiers.

printf() Function

Used to send formatted output to the standard output device (usually the screen).

printf("format string", argument_list);
// Example: printf("Age is %d", age);
    

scanf() Function

Used to read formatted input from the standard input device (usually the keyboard).

scanf("format string", &argument_list);
// Example: scanf("%d", &age);
    

Common Format Specifiers

Specifier Data Type
%d Signed Decimal Integer
%f Floating-point number
%c Single Character
%s String (Character Array)

3. Unformatted I/O: Character and String Functions

Unformatted I/O functions work with individual characters or strings of characters without using format specifiers.

Character I/O

String I/O

4. Expressions in C

An expression is a combination of operators, constants, and variables arranged according to the rules of the language.

Definition: An expression evaluates to a single value. For example, a + b is an arithmetic expression.

5. Exam Focus Enhancements

Exam Tips

Common Mistakes

Frequently Asked Questions

Q: What is the primary difference between formatted and unformatted I/O?
A: Formatted I/O uses format specifiers (%d, %f) to handle different data types, whereas unformatted I/O handles raw characters and strings.

Q: Why do we use putchar() instead of printf()?
A: putchar() is faster and more efficient when you only need to output a single character.