C stdio.h Library

C stdio.h Library

The stdio.h library stands for Standard Input/Output in C. It provides functions for input, output, file handling, and formatting. Most C programs rely on it.


📌 Common stdio.h Functions

1️⃣ Input Functions

Function Description
scanf() Reads formatted input from the console
getchar() Reads a single character from the console
gets() Reads a string from the console (unsafe, avoid in modern C)
fgets() Reads a string from a file or stdin (safe alternative)

Example:

#include <stdio.h>

int main() {
int age;
char name[50];

printf("Enter your age: ");
scanf("%d", &age);

printf("Enter your name: ");
getchar(); // consume leftover newline
fgets(name, 50, stdin);

printf("Hello %sYour age is %d\n", name, age);
return 0;
}


2️⃣ Output Functions

Function Description
printf() Prints formatted output to the console
putchar() Prints a single character
puts() Prints a string with newline

Example:

#include <stdio.h>

int main() {
printf("Hello World\n");
putchar('A');
puts(" is a character");
return 0;
}


3️⃣ File Handling Functions

Function Description
fopen() Opens a file in a specific mode (r, w, a, etc.)
fclose() Closes a file
fprintf() Writes formatted output to a file
fscanf() Reads formatted input from a file
fgetc() Reads a single character from a file
fputc() Writes a single character to a file
fgets() Reads a string from a file
fputs() Writes a string to a file
feof() Checks end-of-file
fflush() Flushes output buffer

Example: Writing to a File

#include <stdio.h>

int main() {
FILE *fp = fopen("data.txt", "w");
if(fp == NULL) {
printf("Error opening file\n");
return 1;
}

fprintf(fp, "Hello File!\n");
fputs("Welcome to C programming\n", fp);

fclose(fp);
return 0;
}

Example: Reading from a File

#include <stdio.h>

int main() {
FILE *fp = fopen("data.txt", "r");
char line[100];

if(fp == NULL) {
printf("File not found\n");
return 1;
}

while(fgets(line, sizeof(line), fp)) {
printf("%s", line);
}

fclose(fp);
return 0;
}


4️⃣ Other Useful Functions

Function Purpose
remove() Deletes a file
rename() Renames a file
rewind() Moves file pointer to beginning
ftell() Returns current position of file pointer
fseek() Moves file pointer to specified location

Example: Using fseek() and ftell()

FILE *fp = fopen("data.txt", "r");
fseek(fp, 0, SEEK_END); // move to end
long size = ftell(fp); // get file size
rewind(fp); // go back to start
fclose(fp);
printf("File size: %ld bytes\n", size);

📌 Summary Table

Category Functions
Console Input scanf, getchar, fgets
Console Output printf, putchar, puts
File Handling fopen, fclose, fprintf, fscanf, fgetc, fputc, fgets, fputs, feof, fflush
File Utilities remove, rename, rewind, ftell, fseek

✅ Notes

  1. Always check if file opened successfully.

  2. Prefer fgets() over gets() for safety.

  3. printf and scanf use format specifiers (%d, %f, %c, %s).

  4. fflush(stdin) is non-standard, avoid it; better use getchar() to clear input buffer.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *