Javatpoint Logo
Javatpoint Logo

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.

Syntax:

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.

Example:

Let's examine an illustration of formatted I/O in C:

Output:

Enter your name: Avi
Enter your age: 22
Your name is Avi and your age is 22

Explanation:

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:

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.

Example

Let's examine an illustration of unformatted I/O in C:

Output:

This is an example of unformatted output.

Explanation:

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:

  1. Unformatted I/O reads and writes data as a stream of bytes without any format, whereas formatted I/O enables you to read and write data in a predefined format.
  2. Flexibility: Formatted I/O offers greater options for output formatting and data representation, whereas unformatted I/O offers less flexibility and is better suited to reading and writing binary data.
  3. Usability: Formatted I/O is more user-friendly for straightforward input and output activities, whereas unformatted I/O necessitates more code and is better suited for intricate input and output procedures.
  4. Efficiency: Because unformatted I/O doesn't require any formatting or data type conversion, it is more efficient than formatted I/O.

Conclusion

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA