Basic Input/Output Channels
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.
At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.
In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.
In C programming, file descriptors are integer values that represent open files or input/output streams. They are used to interact with files, devices, and other input/output mechanisms. The three standard file descriptors commonly used in C are:
stdin (Standard Input): File descriptor 0. It is associated with the keyboard by default, and it is used for reading input from the user.
stdout (Standard Output): File descriptor 1. It is associated with the console or terminal by default, and it is used for displaying output to the user.
stderr (Standard Error): File descriptor 2. Like stdout, stderr is associated with the console or terminal by default. However, it is typically used for error messages and diagnostics to separate them from regular program output.
Output to stdout (Standard Output):
To output information to the console using stdout, you can use the printf function:
#include <stdio.h>
int main() {
printf("Hello, stdout!\n");
fprintf(stdout, "Hello, stdout!\n"); // Same effect
return 0;
}
In this example, the printf function is used to print the string "Hello, stdout!" to the console. The \n at the end represents a newline character, causing the cursor to move to the next line.
Output to stderr (Standard Error):
To output error messages to stderr, you can also use the fprintf function:
#include <stdio.h>
int main() {
fprintf(stderr, "This is an error message.\n");
return 0;
}
In this example, the fprintf function is used to print an error message to stderr.
Input from stdin (Standard Input):
To read input from the user via stdin, you can use the scanf function or other input functions:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num); // can also use fscanf(stdin, "%d", &num);
printf("You entered: %d\n", num);
return 0;
}
In this example, the program prompts the user to enter a number, reads the input from the keyboard using scanf, and then prints the entered number back to the console.