Formatted and Unformatted Input Output in C
Computer programming requires input/output (I/O) operations. Using I/O operations, the data is read and written to and from various sources, including files, keyboards, and screens. I/O operations can be either formatted or unformatted in the C computer language. In this blog post, we will cover the distinctions between formatted and unformatted I/O in C, as well as code samples and outputs.
Formatted I/O in C
The I/O procedure is known as "formatted I/O". It enables you to read or write data in a certain format. Printf() and scanf() are two examples of C routines that handle formatted I/O. The type and format of the data to be read or written are specified by format strings, which are used by these operations. The program's execution replaces the placeholders for the data found in the format strings with the actual data.
Formatted output syntax for the printf() function:
Here, the argument list comprises the variables or values to be printed, and the format string determines the output format.
Formatted input syntax for the scanf() function:
Here, the argument list comprises the variables that will receive the input, and the format string describes the format of the input.
Let's examine an illustration of formatted I/O in C:
Enter your name: Avi Enter your age: 22 Your name is Avi and your age is 22
We first declare the variables name and age in the above example. After that, the user is prompted for their name and age using the printf() function. We utilize the scanf() function to read user input and assign it to the appropriate variables. To format the input, we finally employ the printf() function once more.
Unformatted I/O in C
Unformatted I/O refers to a category of I/O operations that reads or writes data as a stream of bytes without regard to any format. Unformatted I/O in C is carried out with the aid of functions like fread() and fwrite(). Without formatting, these operations are used to read and write data directly to and from files.
Syntax for using the fwrite() function to print unformatted data:
Here, count is the number of elements to be written, size is the size of each element to be written, and the file pointer is a pointer to the file where the data will be written.
Syntax for using the fread() method with unformatted input:
fread(data, size, count, file pointer);
In this syntax, a pointer to the buffer where the data will be read, the size of each element to be read, the number of elements to be read, and a pointer to the file from which the data will be read.
Let's examine an illustration of unformatted I/O in C:
This is an example of unformatted output.
In the example, we first declare a file pointer (fp) and a character array (data). After that, the file pointer is assigned to 'fp' after using the 'fopen()' method to open the 'file.txt' file in write mode. The string "This is an example of unformatted output." is written to the file using the 'fputs()' function. The file pointer is then moved to the file's beginning using the 'fseek()' function. After that, the data is shown using the 'printf()' function after being read from the file into the 'data' buffer using the 'fread()' function.
Formatted and Unformatted I/O differences
In C, there are a few distinctions between formatted and unformatted I/O. Some of these variations include:
Formatted and Unformatted I/O operations are Input/Output operations that are utilized in C programming. Unformatted I/O is used to read and write data as a stream of bytes without any format, whereas formatted I/O is used to read and write data in a predefined format. The choice of which form of I/O operation to use relies on the requirements of the program. Both types of I/O operations have benefits and drawbacks.