Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Master C input/output essentials — learn how to use printf, scanf, getchar, putchar, and format specifiers to handle user data with clear examples.
Input and Output (I/O) operations are fundamental to any programming language, including C. This tutorial will provide a comprehensive overview of input and output in C, covering various functions, standard streams, and examples to illustrate their use.
Table of contents [Show]
Input and output operations in C are the processes of taking data from external sources (input) and sending data to external destinations (output). These operations are essential for interacting with users, reading from files, and displaying results.
In C, input and output are facilitated through standard streams:
stdin (Standard Input): Represents the source of input data, usually the keyboard.stdout (Standard Output): Represents the destination for normal program output, usually the console.stderr (Standard Error): Represents the destination for error messages, also typically the console.To read a single character from the standard input (keyboard), you can use the getchar() function:
#include
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c\n", ch);
return 0;
}
To read a string (a sequence of characters) from the standard input, you can use the scanf() function with the %s format specifier:
#include
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
To display a character on the standard output, you can use the putchar() function:
#include
int main() {
char ch = 'A';
putchar(ch);
return 0;
}
To display a string on the standard output, you can use the printf() function:
#include
int main() {
char greeting[] = "Hello, World!";
printf("%s\n", greeting);
return 0;
}
The printf() and scanf() functions support formatted input and output. You can use format specifiers to control how data is displayed and read. Some common format specifiers include %d for integers, %f for floating-point numbers, %s for strings, and %c for characters.
#include
int main() {
int num = 42;
float pi = 3.141592;
char letter = 'A';
printf("Integer: %d\n", num);
printf("Float: %.2f\n", pi);
printf("Character: %c\n", letter);
return 0;
}
C provides functions for reading from and writing to files. To open a file for reading or writing, you can use fopen(). To read from a file, use fscanf() or fgets(). To write to a file, use fprintf() or fputs(). Don't forget to close the file using fclose() when you're done.
#include
int main() {
FILE *file;
char text[] = "Hello, File I/O!\n";
// Open the file for writing
file = fopen("output.txt", "w");
if (file == NULL) {
printf("Failed to open the file.\n");
return 1;
}
// Write to the file
fprintf(file, "%s", text);
// Close the file
fclose(file);
return 0;
}
Error handling is crucial when dealing with I/O operations. Always check for errors after opening or closing files, and handle them appropriately.
#include
int main() {
FILE *file;
file = fopen("nonexistent_file.txt", "r");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
// File operations...
fclose(file);
return 0;
}
Let's explore some complete example programs to illustrate input and output operations:
#include
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
In this example, we read a name from the user and display a personalized greeting.
#include
int main() {
FILE *file;
char text[100];
// Open the file for writing
file = fopen("output.txt", "w");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
printf("Enter text to write to the file: ");
scanf("%s", text);
// Write to the file
fprintf(file, "%s", text);
// Close the file
fclose(file);
printf("Data written to the file successfully!\n");
return 0;
}
In this example, we read user input and write it to a file named "output.txt."
Input and output operations are essential in C programming, allowing you to interact with users, read from and write to files, and handle data in various formats. Understanding the use of standard streams, format specifiers, and error handling is crucial for effective I/O operations in C.
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


