C ProgrammingUnit 33 min read
Input and Output: Formatted and Unformatted I/O Functions in C
Unit 3 of C Programming: This unit covers the fundamental mechanisms for data interaction in C, focusing on the distinction between formatted and unformatted input/output functions, their syntax, usage in program development, and the role of standard streams in data processing.
Key points
- Formatted I/O functions like `printf()` and `scanf()` allow data to be displayed or read in a specific, user-defined format.
- Unformatted I/O functions like `getchar()` and `putchar()` handle single characters or strings without specific formatting requirements.
- The `stdio.h` header file is essential for accessing standard I/O library functions in C programs.
- Conversion specifiers (e.g., `%d`, `%f`, `%s`) are critical for interpreting data types during formatted I/O operations.
- Understanding buffer management and the role of the newline character is vital for preventing common I/O errors in C.
Overview of Input and Output in C
In C programming, input and output operations are not part of the language syntax itself but are provided through the Standard Input/Output library (stdio.h). These functions allow the program to communicate with the user via standard devices: the keyboard (standard input) and the monitor (standard output).
Formatted Input and Output
Formatted I/O functions allow the programmer to specify the format of the data being read or written. This is achieved using format specifiers.
Formatted Output: printf()
The printf() function sends formatted output to the standard output device (stdout).
- Syntax:
int printf(const char *format, ...); - Format Specifiers:
%dor%i: Integer%f: Float%c: Character%s: String%lf: Double
Formatted Input: scanf()
The scanf() function reads formatted input from the standard input device (stdin).
- Syntax:
int scanf(const char *format, ...); - Note: It requires the address-of operator (
&) for variables (except strings) because it needs to store the value at a specific memory location.
Unformatted Input and Output
Unformatted functions handle data as raw characters or strings without requiring format specifiers. They are generally faster than formatted functions.
Character I/O
getchar(): Reads a single character from stdin.putchar(int c): Writes a single character to stdout.
String I/O
gets(): Reads a line of text (unsafe, deprecated in modern C).fgets(): The safer alternative togets(), which reads a specified number of characters.puts(const char *s): Writes a string to stdout followed by a newline.
Comparison Table: Formatted vs. Unformatted I/O
| Feature | Formatted I/O | Unformatted I/O |
|---|---|---|
| Flexibility | High (customizable output) | Low (fixed format) |
| Complexity | More complex (uses specifiers) | Simple |
| Performance | Slower (due to parsing) | Faster |
| Examples | printf(), scanf() |
getchar(), putchar(), puts() |
Worked Example: Formatted I/O
The following program demonstrates reading an integer and a float, then printing them with specific formatting.
#include <stdio.h>
int main() {
int age;
float height;
printf("Enter your age and height: ");
scanf("%d %f", &age, &height);
// Printing with field width and precision
printf("Age: %d, Height: %.2f meters\n", age, height);
return 0;
}
Buffer Management and Common Pitfalls
When using scanf(), a common issue is the "leftover" newline character in the input buffer. If you call scanf() followed by getchar(), the getchar() might consume the newline character left by the previous scanf().
Solution: Use a space before the format specifier in scanf (e.g., scanf(" %c", &ch);) to skip leading whitespace.
Exam Tip
In TU/PU exams, you are often asked to differentiate between formatted and unformatted functions. Always provide a table and mention that formatted functions use conversion specifiers while unformatted ones do not. When writing code, always include the necessary header files (#include <stdio.h>) and ensure you use the & operator in scanf for non-array variables, as examiners look for these specific syntax details.
Based on the TU BSc CSIT syllabus for C Programming (CSC115), unit 3.
Discussion
Loading…