Input and output (I/O) operations are fundamental in any programming
language, as they allow programs to interact with users and other systems. In
C, the standard library provides functions for handling input and output
through various streams. This blog will introduce you to the basics of I/O in
C, including standard input, output, and error handling, with practical
examples.
Standard Input and Output
In C, the standard input (stdin), standard output (stdout), and standard error (stderr) are predefined file streams. The stdio.h library provides functions to read from and write to these streams.
1. printf
The printf function is used to print formatted output to the standard output (usually the console). The function takes a format string followed by a list of arguments.
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
}
In this example:
- %d is a format
specifier for integers.
- %.1f is a format
specifier for floating-point numbers with one decimal place.
- %c is a format
specifier for characters.
The puts function writes a string to the standard output followed by a newline character.
puts("Hello, World!");
return 0;
}
1. scanf
The scanf function reads formatted input from the standard input (usually the keyboard). The function takes a format string followed by pointers to variables where the input will be stored.
int age;
float height;
char grade;
scanf("%d", &age);
scanf("%f", &height);
scanf(" %c", &grade); // Note the space before %c to consume any leftover newline character
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
}
The gets function reads a string from the standard input until a newline character is encountered. However, gets is considered unsafe due to the potential for buffer overflow. It is recommended to use fgets instead.
char name[50];
fgets(name, sizeof(name), stdin); // safer alternative to gets
}
The standard error stream (stderr) is used to output error messages. You can use fprintf to write to stderr.
int age;
if (scanf("%d", &age) != 1) {
fprintf(stderr, "Invalid input! Please enter a number.\n");
return 1;
}
return 0;
}
In addition to standard I/O, C provides functions for file input and output. The fopen, fclose, fread, fwrite, fprintf, and fscanf functions are commonly used for file operations.
#include <stdio.h>
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error opening file!\n");
return 1;
}
fclose(file);
}
#include <stdio.h>
FILE *file = fopen("output.txt", "r");
if (file == NULL) {
fprintf(stderr, "Error opening file!\n");
return 1;
}
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
return 0;
}
Understanding input and output functions in C is crucial for interacting with users and external systems. The stdio.h library provides a comprehensive set of functions for handling standard I/O and file operations. By mastering these functions, you can create robust and user-friendly C programs. Practice using these I/O functions to gain confidence and enhance your programming skills.
No comments:
Post a Comment