Basic Input and Output In C
Hello Everyone, In this article, we will discuss basic Input and Output In C. We will look at how to get data from the user using input functions and how to display results using output functions. Whether you are new to C or have some experience, this guide will help you to understand basics of Input and Output Function In C effectively with examples. Let’s get started.
In C programming, input and output (I/O) are essential operations that enable a program to interact with users, files, or other devices. Input is the process of receiving data, while output is the process of sending data. C programming provides various functions and libraries to perform I/O operations.
Standard Input/Output Library (stdio.h): This library contains functions to perform I/O operations using standard input and output devices, such as the keyboard and the screen.
Some common input and output functions include:
printf(): This function is used for output. It displays formatted text on the screen. The syntax is:
printf("format string", arguments);
Example:
#include <stdio.h>int main() {
int age = 25;
printf("My age is %d.\\n", age);
return 0;
}
scanf(): This function is used for input. It reads formatted data from the standard input device (usually the keyboard). The syntax is:
scanf("format string", &variable);
Example:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d.\\n", age);
return 0;
}
File Input/Output (fopen, fclose, fread, fwrite, etc.): C programming also provides functions to work with files, allowing you to read and write data to a file on the disk. These functions are included in the standard I/O library (stdio.h).
Here’s an example of reading and writing to a file:
#include <stdio.h>
int main() {
FILE *file;
char text[100];
// Open the file for writing
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening the file.\\n");
return 1;
}
// Write some text to the file
fprintf(file, "Hello, World!\\n");
// Close the file
fclose(file);
// Open the file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening the file.\\n");
return 1;
}
// Read the text from the file
fgets(text, sizeof(text), file);
// Print the text to the screen
printf("File content: %s", text);
// Close the file
fclose(file);
return 0;
}
This example demonstrates the basic file I/O operations in C programming, including opening a file, writing data to it, reading data from it, and closing the file.
In C programming, “getchar()” and “putchar()” are standard input/output functions used for reading a single character from the keyboard and writing a single character to the screen respectively.
1. getchar() Function: The “getchar()” function reads a single character from the standard input device, which is usually the keyboard. It takes no arguments and returns an integer value representing the character read. The function waits for the user to enter a character and press the enter key.
Example:
char ch;
ch = getchar();
In this example, the “getchar()” function reads a single character from the keyboard and stores it in the variable “ch”.
2. putchar() Function: The “putchar()” function is used to display a single character to the standard output device, which is usually the screen. It takes a single argument of type “int”, which is the character to be displayed, and returns an integer value representing the character displayed.
Example:
char ch = 'A';
putchar(ch);
In this example, the “putchar()” function displays the character ‘A’ on the screen.
These functions are commonly used in C programming for simple input and output operations involving a single character.
Here is an example C program that demonstrates the use of “getchar()” and “putchar()” functions:
#include <stdio.h>int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
return 0;
}
This program prompts the user to enter a character, reads the character using the “getchar()” function, and then displays the character using the “putchar()” function.
When you run the program, you will see the message “Enter a character: ” displayed on the screen. The program then waits for the user to enter a character and press the enter key. Once the user has entered a character, the program reads the character using the “getchar()” function and stores it in the variable “ch”. The program then displays the message “You entered: ” on the screen and displays the character entered by the user using the “putchar()” function.
if the user enters the character ‘A’, the program will output:
Enter a character: A
You entered: A