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
§ 5.1.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 example: width and precision
.2 rounds to 2 decimal places.
↳ Result: 3.146 is the minimum field width; 3.14 is 4 characters, so 2 spaces of padding go on the left.
Practice: test yourself
Original practice questions in the GATE style, with worked explanations. Not past-paper questions.
Which fopen mode opens an existing file for both reading and writing, without truncating it?
fgets(buf, 10, fp) reads at most:
What does printf("%x", 255) print?
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 for clarity, rigorous numerical solving, and semester excellence:
Systematic beginner-to-advanced C with clear examples.
Clear step-by-step C explanations — great for semester lab preparation.