๐Ÿ“šC Standard Library Headers
Estimated reading: 4 minutes 342 views

C <stdio.h> โ€“ Standard Input and Output in C Programming


Introduction โ€“ What Is <stdio.h> in C?

<stdio.h> stands for Standard Input/Output Header. It is one of the most fundamental and frequently used C standard library headers, enabling input and output operations like reading from the keyboard, writing to the console, and handling files. It contains declarations for functions, macros, and types that support I/O functionality.

In this guide, youโ€™ll learn:

  • What <stdio.h> provides
  • How to use its functions for console and file I/O
  • Common use cases and examples
  • Best practices and tips

Key Features of <stdio.h>

The <stdio.h> header includes:

  • Standard streams: stdin, stdout, stderr
  • Console I/O functions: printf(), scanf(), getchar(), putchar()
  • File I/O functions: fopen(), fread(), fwrite(), fclose()
  • Formatted I/O: fprintf(), fscanf()
  • Line and character operations: fgets(), fputs(), getc(), putc()

Console I/O Functions

printf() โ€“ Print to Console

printf("Hello, %s!\n", "World");  // Output: Hello, World!

Supports format specifiers like %d, %f, %c, %s.


scanf() โ€“ Read from Console

int age;
scanf("%d", &age);

Reads formatted input from stdin. Always use & for variables unless reading strings.


Character I/O

  • getchar() โ€“ Reads a single character from stdin
  • putchar(c) โ€“ Writes a character to stdout

File I/O Functions

You can use <stdio.h> to work with files on disk using a FILE* pointer.

Open and Close File

FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
    perror("File open failed");
}
fclose(fp);

Modes: "r", "w", "a", "rb", "wb", etc.


Read and Write to Files

fprintf(fp, "ID: %d\n", 1001);  // Write to file
fscanf(fp, "%d", &id);          // Read from file
  • fgetc(), fputc() โ€“ Character-based file I/O
  • fgets(), fputs() โ€“ Line-based file I/O
  • fread(), fwrite() โ€“ Binary file I/O

Buffer and Stream Functions

  • fflush() โ€“ Flushes the output buffer
  • feof() โ€“ Checks end of file
  • ferror() โ€“ Checks for I/O error
  • rewind(), fseek(), ftell() โ€“ Navigate within file

Real-World Use Cases

TaskFunction(s) Used
Displaying outputprintf(), putchar()
Reading user inputscanf(), getchar()
Reading from a filefopen(), fgets(), fscanf()
Writing logs to a filefprintf(), fputs()
Copying binary filesfread(), fwrite()

Best Practices & Tips

Always check file pointers (FILE *) after fopen() to avoid segmentation faults.

Use fgets() for reading strings to avoid buffer overflows caused by scanf("%s").

Don’t forget to fclose() every opened file to prevent memory leaks and file lock issues.


Summary โ€“ Recap & Next Steps

The <stdio.h> header is the gateway to Cโ€™s input/output capabilities. Whether you’re writing text to the screen, reading user input, or working with files, <stdio.h> gives you the tools to handle it all.

Key Takeaways:

  • Use printf() and scanf() for console I/O
  • Use fopen() and related functions for file I/O
  • Handle streams with care to manage buffers and file navigation
  • Itโ€™s the most included header in C programs for a reason!

Real-World Relevance:

Critical in text editors, command-line utilities, loggers, data processors, and file converters.


Frequently Asked Questions (FAQ)

What does <stdio.h> stand for?

It stands for Standard Input/Output header.


What are the standard streams defined in <stdio.h>?

stdin (input), stdout (output), stderr (error stream)


How do I write to a file?

Use fopen() to open a file and fprintf() or fputs() to write data:

FILE *fp = fopen("file.txt", "w");
fprintf(fp, "Hello\n");
fclose(fp);

Can I use <stdio.h> for binary files?

Yes. Use modes like "rb", "wb" and functions like fread()/fwrite().


What happens if I forget to close a file?

It may result in buffer not flushed, file corruption, or resource leaks.


Share Now :
Share

๐Ÿ“˜ C <stdio.h>

Or Copy Link

CONTENTS
Scroll to Top