Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Standard Library: stdio.h

Lesson 18 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Standard Library: stdio.h

The stdio.h (Standard Input/Output) header provides functions for reading and writing data to streams (console, files).

---

Console I/O Functions

FunctionPrototypeDescription
printfint printf(const char *fmt, ...)Formatted output to stdout
scanfint scanf(const char *fmt, ...)Formatted input from stdin
getcharint getchar(void)Read one character from stdin
putcharint putchar(int c)Write one character to stdout
getschar gets(char s)Read string (unsafe, deprecated)
putsint puts(const char *s)Write string + newline

---

File I/O Functions

FunctionDescription
fopenOpen a file
fcloseClose a file
fprintfFormatted write to file
fscanfFormatted read from file
fgetsRead line from file
fputsWrite string to file
freadRead binary data
fwriteWrite binary data
feofTest end-of-file
fseekMove file position
ftellGet file position
rewindReset file position

---

String Formatting Functions

char buf[100];
sprintf(buf, "Value is %d", 42);   /* print to string */
printf("%s\n", buf);               /* "Value is 42" */

int n;
sscanf("42 hello", "%d", &n);      /* scan from string */
printf("%d\n", n);                 /* 42 */

---

Standard Streams

StreamDescriptionType
stdinStandard inputFILE *
stdoutStandard outputFILE *
stderrStandard errorFILE *
fprintf(stderr, "Error: file not found\n");  /* write to stderr */

---

Error Handling

#include <stdio.h>
#include <errno.h>

FILE *fp = fopen("missing.txt", "r");
if (fp == NULL) {
    perror("fopen");   /* prints: fopen: No such file or directory */
}

---

Complete I/O Example

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Sum = %d, Product = %d\n", a + b, a * b);
    return 0;
}