BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

I/O Formatting Functions in C: printf, scanf, getchar, putchar, gets, puts | Unit 3 | BKNMU BCA

by BCA School



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

BKNMU Junagadh BCA Sem 1 Unit 3 stdio.h Header Formatted I/O Unformatted I/O
In simple words: In C programming, Input/Output operations are not part of the language syntax itself; they are handled via standard library functions defined in #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).
📖 Core Categories of Standard I/O Functions

The standard I/O library handles user interaction across four primary channels:

printf() / scanf()

1. Formatted I/O

Converts internal data formats to and from structured text streams using conversion specifiers like %d, %f, %c, and %s.

getchar() / putchar()

2. Character I/O

Reads and writes a single character directly from standard input (stdin) or to standard output (stdout) with line buffering.

getc() / putc()

3. Stream Character I/O

Reads or writes a single character to any specified stream, including standard streams (stdin/stdout) or custom file pointers.

gets() / puts()

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.

1️⃣ Master Table: I/O Functions Breakdown

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 vs. Unformatted I/O Key Differences:
  • 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), whereas printf("%s", str) requires an explicit \n.
💻 Practical Code Examples

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-by-Step Flow: How I/O Streams Operate
1

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.

2

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.

3

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.

💡 Critical Exam Difference: getc vs getchar: 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.
⚠️ Security Warning on gets(): The 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).
📝 Quick Revision — Key Exam Points
  • Header: #include <stdio.h>
  • Formatted I/O: printf() and scanf() use format specifiers like %d, %c, %s, %f.
  • Character I/O: getchar() and putchar() process one character at a time on standard streams.
  • Generic Streams: getc(stream) and putc(char, stream) operate on any file pointer.
  • Line Processing: gets() reads full line until \n; puts() writes string with automatic trailing newline.