Exploring C Programming Input and Output Functions: Formatted vs.
Unformatted
Introduction
In the world of programming, efficient input and output handling is
crucial. This blog post explores the different input and output
functions available in the C programming language, categorizing them as
formatted and unformatted, and discusses their special features and use
cases.
Formatted Input Functions:
Function |
Description |
Syntax |
scanf |
Reads formatted input from standard input, allows specifying format
specifiers for input data.
|
int scanf(const char *format, ...); |
fscanf |
Reads formatted input from a file stream, supports format specifiers
for data parsing.
|
int fscanf(FILE *stream, const char *format, ...); |
sscanf |
Reads formatted input from a string, similar to scanf, but operates on
strings.
|
int sscanf(const char *str, const char *format, ...); |
scanf_s |
A safer version of scanf introduced in C11, which includes buffer size
specification to prevent buffer overflows.
|
int scanf_s(const char *format, ...); |
fscanf_s |
A safer version of fscanf introduced in C11, with buffer size
specification for file input.
|
int fscanf_s(FILE *stream, const char *format, ...); |
Unformatted Input Functions:
Function |
Description |
Syntax |
getchar |
Reads a single character from standard input, useful for raw character
input.
|
int getchar(void); |
getch |
Reads a single character from standard input without echoing to the
screen, often used for input masking.
|
int getch(void); |
getche |
Similar to getch, but echoes the character back to the screen,
suitable for password input.
|
int getche(void); |
gets |
Deprecated in C11 due to security concerns, reads a line of text from
standard input.
|
char *gets(char *str); |
fgets |
Reads a line of text from standard input or a file stream, with buffer
size specification for safety.
|
char *fgets(char *str, int size, FILE *stream); |
getw |
Reads a binary integer from a file stream. |
int getw(FILE *stream); |
getwc |
Reads a wide character from standard input or a file stream. |
wint_t getwc(FILE *stream); |
Formatted Output Functions:
Function |
Description |
Syntax |
printf |
Outputs formatted data to standard output, uses format specifiers for
display.
|
int printf(const char *format, ...); |
fprintf |
Outputs formatted data to a file stream, supports format specifiers
for file output.
|
int fprintf(FILE *stream, const char *format, ...); |
sprintf |
Outputs formatted data to a string, similar to printf, but stores the
result in a string.
|
int sprintf(char *str, const char *format, ...); |
printf_s |
A safer version of printf introduced in C11, includes buffer size
specification to prevent buffer overflows.
|
int printf_s(const char *format, ...); |
fprintf_s |
A safer version of fprintf introduced in C11, with buffer size
specification for file output.
|
int fprintf_s(FILE *stream, const char *format, ...); |
Unformatted Output Functions:
Function |
Description |
Syntax |
putchar |
Writes a single character to standard output. |
int putchar(int c); |
putch |
Writes a single character to standard output without newline, often
used for inline character output.
|
int putch(int c); |
putc |
Writes a character to a file stream. |
int putc(int character, FILE *stream); |
puts |
Writes a string with a newline to standard output. |
int puts(const char *str); |
fputc |
Writes a character to a file stream. |
int fputc(int character, FILE *stream); |
fputs |
Writes a string to a file stream. |
int fputs(const char *str, FILE *stream); |
putw |
Writes a binary integer to a file stream. |
int putw(int w, FILE *stream); |
putwc |
Writes a wide character to a file stream. |
wint_t putwc(wchar_t wc, FILE *stream); |
Conclusion
Understanding input and output functions in C is essential for building
robust programs. Whether you need controlled, formatted input or
flexible unformatted input, C provides a range of functions to suit your
needs. Similarly, for output, you can format and display data as
required.