PROGRAMMING IN C/Student Engineering Notebook
Page 10·notes
PREPROCESSOR DIRECTIVES, MACROS & FILE I/O—~28 MIN READ

Standard I/O, Format Specifiers & File Streams

Optional C enrichment; file handling is not named in the GATE 2027 CS/IT C syllabus

“printf/scanf return values, format specifiers, and FILE* buffered file handling.”

1.Formatted I/O: Return Values & Format Traps

Return Values of printf and scanf:

- printf("format", ...): Returns the total number of characters successfully printed.

Example: int n = printf("GATE"); prints GATE and sets n = 4.

- scanf("format", ...): Returns the number of input items successfully matched and assigned.

Example: scanf("%d %d", &a, &b) returns 2 on valid integers, or EOF (-1) on end-of-file.

File Streams in C:

- Files are represented by FILE* stream pointers.

- Functions: fopen("file.txt", "r"), fclose(fp), fgetc(fp), fputc(ch, fp), fread(), fwrite(), fseek(fp, offset, SEEK_SET/CUR/END), ftell(fp).

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: A '-' flag (%-6.2f) left-aligns instead; width never truncates a longer value.

Worked example: width and precision

Problem Statement:
What does printf("[%6.2f]", 3.14159) print?
Step 1: Precision

.2 rounds to 2 decimal places.

↳ Result: 3.14
Step 2: Width

6 is the minimum field width; 3.14 is 4 characters, so 2 spaces of padding go on the left.

Final Answer:[ 3.14]
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · FOUNDATION

Which fopen mode opens an existing file for both reading and writing, without truncating it?

Problem 2 · GATE-LEVEL

fgets(buf, 10, fp) reads at most:

Problem 3 · FOUNDATION

What does printf("%x", 255) print?

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
The C Programming Language (2nd Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Neso Academy · C Programming Full Course↗Instructor: Neso Academy · Covers: Data types, pointers, arrays, functions, recursion, file I/O

Systematic beginner-to-advanced C with clear examples.

Open YouTube↗
Jenny's Lectures CS/IT · C Programming↗Instructor: Jenny's Lectures · Covers: Arrays, strings, pointers, structures, file handling, preprocessor

Clear step-by-step C explanations — great for semester lab preparation.

Open YouTube↗