I/O Formatting Functions in C: printf, scanf, getchar, putchar, gets, puts | Unit 3 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 3
I/O Functions in C: Formatted and Unformatted Console I/O Explained
Complete Guide to printf, scanf, getchar, putchar, getc, putc, gets, and puts with Format Specifiers
#include <stdio.h>. These utilities are broadly categorized into Formatted I/O (which transform raw binary values into structured text representations) and Unformatted I/O (which process raw characters or lines of text without conversion specifiers). This post breaks down both mechanisms for BKNMU BCA Semester 1 (Unit 3).
The standard I/O library handles user interaction across four primary channels:
1. Formatted I/O
Converts internal data formats to and from structured text streams using conversion specifiers like %d, %f, %c, and %s.
2. Character I/O
Reads and writes a single character directly from standard input (stdin) or to standard output (stdout) with line buffering.
3. Stream Character I/O
Reads or writes a single character to any specified stream, including standard streams (stdin/stdout) or custom file pointers.
4. String Line I/O
Reads a complete line of text up to a newline character, and outputs a null-terminated string followed by an automatic newline.
A complete reference of function prototypes, operational behavior, and return values:
| Function Prototype | Functional Purpose | Return Value / Type |
|---|---|---|
int printf(const char *f, ...) |
Writes formatted output data to standard output stream (stdout) according to format flags. | Returns total number of printed characters as int. |
int scanf(const char *f, ...) |
Reads formatted input data from standard input stream (stdin) and stores into address pointers. | Returns total successfully scanned input items as int. |
int getchar(void) |
Reads the next available single character from the standard input stream (stdin). | Returns character read as int (or EOF on end/error). |
int putchar(int c) |
Writes a single character to standard output stream (stdout) at current cursor position. | Returns written character as int (or EOF on error). |
int getc(FILE *stream) |
Reads a single character from the specified input stream (often defined as a fast macro). | Returns character read as int (or EOF on end/error). |
int putc(int c, FILE *stream) |
Writes a single character to the designated target stream buffer (stdin, stdout, or file). | Returns written character as int (or EOF on error). |
char* gets(char *str) |
Reads an entire line of text from stdin until newline; replaces newline with null terminator. | Returns string pointer str (or NULL on error). |
int puts(const char *str) |
Writes a null-terminated string to stdout and automatically appends a newline character (\n). | Returns non-negative integer on success (or EOF on error). |
- Formatted Functions (printf, scanf): Require format specifiers (e.g.,
%d,%f,%s). They perform internal data conversion between binary representation and ASCII text. - Unformatted Functions (getchar, putchar, gets, puts): Process character and string data directly without formatting overhead or specifiers.
- Buffer Flush Rule:
puts()automatically adds a trailing newline (\n), whereasprintf("%s", str)requires an explicit\n.
1. Formatted Console Interaction using printf() and scanf():
#include <stdio.h>
int main() {
int roll_no;
float percentage;
printf("Enter Roll Number and Percentage: ");
// Reads integer and float using memory addresses (&)
scanf("%d %f", &roll_no, &percentage);
// Prints formatted values with fixed float precision
printf("Roll No: %d | Percentage: %.2f%%\n", roll_no, percentage);
return 0;
}
2. Single Character and String I/O (getchar, putchar, puts):
#include <stdio.h>
int main() {
char grade;
char message[] = "BCA Sem 1 NEP 2020 Exam";
// Unformatted string output (automatically appends \n)
puts(message);
printf("Enter your grade: ");
// Read single character from stdin
grade = getchar();
printf("Recorded Grade: ");
// Write single character to stdout
putchar(grade);
putchar('\n');
return 0;
}
Step 1: Keyboard Input to Stream Buffer
Key presses enter the operating system standard input stream buffer (stdin). Characters sit in buffer until Enter (\n) is pressed.
Step 2: Stream Parsing & Format Conversion
The chosen function consumes bytes from the buffer. Formatted functions convert ASCII strings to numbers; unformatted functions grab raw characters directly.
Step 3: Flushing to Output Terminal
Output data is routed to the stdout buffer and flushed to the console screen as pixels representing text characters.
getchar() is equivalent to calling getc(stdin). While getchar() strictly reads from keyboard standard input, getc() accepts any custom file stream pointer as an argument.
gets() function does not verify buffer boundary limits and causes severe memory buffer overflow vulnerabilities. Although included in traditional university syllabi, modern C standards (C11 onwards) have removed gets() in favor of fgets(str, size, stdin).
- Header:
#include <stdio.h> - Formatted I/O:
printf()andscanf()use format specifiers like%d,%c,%s,%f. - Character I/O:
getchar()andputchar()process one character at a time on standard streams. - Generic Streams:
getc(stream)andputc(char, stream)operate on any file pointer. - Line Processing:
gets()reads full line until\n;puts()writes string with automatic trailing newline.