Certainly! In the C programming language, there are several input and output functions available. Here's an overview of the functions you mentioned:
Input Functions:
getchar(): Reads a single character from the standard input (keyboard) and returns it as an integer value.
gets(): Reads a line of text from the standard input and stores it as a string.
scanf(): Reads formatted data from the standard input based on the specified format string. It is commonly used to read different types of data, such as integers, floats, and strings.
Output Functions:
putchar(): Writes a single character to the standard output (console).
puts(): Writes a string to the standard output, followed by a newline character.
printf(): Prints formatted data to the standard output based on the specified format string. It is commonly used to display different types of data with specified formatting.
Here are a few examples demonstrating the usage of these input and output functions:
c
Copy code
#include stdio.h
int main() {
int num;
char ch;
char str[50];
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter a character: ");
getchar(); // Consumes the newline character from the previous input
ch = getchar();
printf("Enter a string: ");
getchar(); // Consumes the newline character from the previous input
gets(str);
printf("You entered: %d, %c, %s\n", num, ch, str);
putchar('A');
putchar('\n');
puts("Hello, World!");
printf("Formatted Output: %d + %d = %d\n", 10, 20, 10 + 20);
return 0;
}
In this example, scanf() is used to read an integer, getchar() is used to read a character, and gets() is used to read a string. The putchar() function is used to write a character, puts() is used to write a string, and printf() is used for formatted output.
Please note that the gets() function is generally considered unsafe due to the possibility of buffer overflow. It is recommended to use fgets() instead, which allows you to specify the buffer size.
Remember to use the appropriate format specifiers in the printf() and scanf() functions to correctly read and display different types of data.