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.
Formatted functions allow the programmer to specify exactly how the data should be read or displayed using format specifiers.
Used to send formatted output to the standard output device (usually the screen).
printf("format string", argument_list);
// Example: printf("Age is %d", age);
Used to read formatted input from the standard input device (usually the keyboard).
scanf("format string", &argument_list);
// Example: scanf("%d", &age);
| Specifier | Data Type |
|---|---|
| %d | Signed Decimal Integer |
| %f | Floating-point number |
| %c | Single Character |
| %s | String (Character Array) |
Unformatted I/O functions work with individual characters or strings of characters without using format specifiers.
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.
scanf() for non-string variables.gets() if you need to input a string that contains spaces; scanf("%s") stops at the first space.#include <stdio.h> is at the top of your program to use these functions.scanf(). This will cause the program to crash or behave unpredictably.puts() only works for strings and adds a newline, while printf() is versatile but requires format specifiers.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.