Chapter-3 (Input/Output functions in C)
In C, input and output functions are used to interact with the user and the external world. These functions allow you to read data from the user or files and write data to the screen or files. The standard library provides several functions for input and output. Here are some of the most commonly used input and output functions in C:
Input Functions:-
- scanf: Used for reading input from the standard input (usually the keyboard) or files based on a specified format.
Example int num; scanf("%d", &num);
It is used to input the data from User - getchar and getch: Used for reading a single character from the standard input. getch is a non-standard function often used for console-based input without echoing characters.
Example char ch = getchar();
- gets (deprecated): Used to read a string from the standard input. However, it's considered unsafe and is deprecated in favor of fgets.
Output Functions:-
- printf: Used for formatted output to the standard output (usually the console) or files.
Example int num = 42; printf("The value of num is %d\n", num);
It is used to print the content on Screen.
- putchar: Used for writing a single character to the standard output.
Example char ch = 'A'; putchar(ch);
- puts: Used to write a string to the standard output followed by a newline character.
Example char str[] = "Hello, World!"; puts(str);
These are some of the basic input and output functions in C. It's important to handle errors and check return values when using file-related functions, as file operations can fail for various reasons (e.g., file not found, insufficient permissions). Additionally, remember to include the necessary header files (