- Asked 4/16
- 2074 Chaitra · 4 marks
- 2070 Chaitra · 4 marks
- 2068 Chaitra · 4 marks
- 2072 Kartik · 1 mark
Formatted vs unformatted I/O
Short answer
Formatted I/O uses format specifiers like %d and %f to control conversion and handle all data types; functions include scanf and printf. Unformatted I/O works with characters and strings as-is, using getchar, putchar, gets, puts, and is simpler. Example: scanf("%d",&age) reads an integer, while ch=getchar() reads a character.
What are the differences between formatted and unformatted input/output functions? Write down the syntax and give suitable examples.
Formatted I/O functions read or display data according to a format given by the programmer (type, width, precision). Unformatted I/O functions read or write a single character or string as it is, without any format specification.
| Formatted | Unformatted |
|---|---|
Uses format specifiers (%d, %f, %c) | No format specifiers |
| Can handle all data types | Handle only char and strings |
| Output can be controlled by width, precision | No control over appearance |
scanf(), printf(), fscanf(), fprintf() | getchar(), putchar(), gets(), puts(), getch() |
| Slower, more flexible | Faster, simpler |
Syntax
printf("format string", variable_list);scanf("format string", &variable_list);ch = getchar();,putchar(ch);,puts("string");
Example
#include <stdio.h>
int main(void)
{
int age;
char ch;
printf("Enter age: ");
scanf("%d", &age); /* formatted input */
printf("Age = %d\n", age); /* formatted output */
getchar(); /* consume newline */
printf("Enter a character: ");
ch = getchar(); /* unformatted input */
printf("You typed: ");
putchar(ch); /* unformatted output */
putchar('\n');
puts("Done"); /* unformatted output of a string */
return 0;
}
Output (with input 25 and Z):
Enter age: Age = 25
Enter a character: You typed: Z
Done
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Formatted and unformatted I/O in FORTRAN · Computer Programming, chapter 11
- Formatted I/O in FORTRAN with syntax · Computer Programming, chapter 11
- Pattern: PULCHOWK using unformatted output · Computer Programming, chapter 4
- Pattern N, eee, PPPPP using unformatted I/O · Computer Programming, chapter 4
- I/O functions used with data files · Computer Programming, chapter 10 · asked 2×
- ios class functions and flags for formatted I/O · Object Oriented Programming, chapter 8 · asked 2×
- CE amplifier input, output resistance and gain (RE1, RE2) · Electronics Devices and Circuit, chapter 2
- Bridge with diode, α-dependent output and input current · Power Electronics, chapter 2
- Output of e^(−2t)u(t) to a rectangular input · Signals and Systems, chapter 5 · asked 4×