Inputs and Outputs in C Programming Language :
In C programming, input and output (I/O) operations are essential for interacting with the user. The standard input device is typically the keyboard, and the standard output device is usually the screen. The C Standard Library provides functions to perform I/O operations, primarily through the stdio.h header file.
Output In C :
Output in C refers to the process of displaying or writing data to an external destination, typically to the screen (stdout) or a file.
Standard Output Function :
printf :
To print the data to standard output , you can use the "printf" function. The syntax is as follows :
Syntax :
printf("format_string", expression1, expression2,...);
NOTE:
format_string: This is a string that specifies the format in which you want to display the data.expression1, expressionn2, : These are the values or expressions you want to print , which should match the format specifiers in the format string.
Example:
#include <stdio.h>
int main() {
int num = 2;
printf("The value of number is %d\n", num);
return 0;
}
Output:
The value of number is 42
Standard Input Function :
scanf :
Standard input represents the source from which your program can read, typically the keyboard.
To read the data from standard input , you can use the "scanf" function. The syntax is as follows :
To read the data from standard input , you can use the "scanf" function. The syntax is as follows :
Syntax :
scanf("format_specifier", &variable1, &variable2 ...);
NOTE :
format_specifier: These are the placeholders that specify the type of data you want to read. For examle, %d for integers, %f for floating-point numbers, %c for characters, %s for string, etc.
&variable1, &variable2: These are the variables where the data will be stored after reading.
Example:
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); printf("You are %d years old.\n", age); return 0; }
Output:
Enter your age: 18 //Entered by the user
You are 18 years old. // After reading the input
getchar() and gets() :
These function are used for reading individual characters or strings from the keyboard.
Example of getchar() :
#include <stdio.h> int main() { char c; printf("Enter a character: "); c = getchar(); printf("You entered: %c\n", c); return 0; }
Output:
Enter a character: a You entered: a
Example of gets() :
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
gets(name); // Unsafe, use fgets instead.
printf("Hello, %s!\n", name);
return 0;
}
NOTE :
• Reads a string from the standard input until a newline character is encountered.
• '
gets'
is considered unsafe because it does not perform bounds checking. Use 'fgets'
instead.putchar() :
• Writes a single character to the standard output.
Example of putchar() :
#include <stdio.h> int main() { char c = 'A'; printf("The character is: "); putchar(c); putchar('\n'); return 0; }
Understanding and using input and output functions in C is crucial for building interactive programs and performing file operations. These functions provide a foundation for reading user input, displaying information, and manipulating files effectively. By mastering these concepts, you can create robust and user-friendly applications that can interact with users and handle data efficiently.