Formatted And Unformatted Input Output In C

The input/output process is commonly referred to as "formatted I/O". This technique allows for the reading or writing of data in a specific layout. Printf and scanf represent a pair of C functions responsible for managing formatted I/O. To define the data's type and arrangement during reading or writing, format strings are employed by these functions. Within these operations, the placeholders within the format strings are substituted with the real data during program execution.

Syntax:

The syntax for displaying formatted output using the printf function is as follows:

Example

printf(format string, argument list);

Here, the parameter list consists of the variables or values to be displayed, while the pattern string defines the format of the output.

Formatted input syntax for the scanf function:

Here, the list of arguments includes the variables that will accept the input, while the string format defines the input's structure.

Example:

Let's explore a demonstration of formatted input/output in C:

Example

#include <stdio.h>

int main() {
   char name[20];
   int age;

printf("Enter your name: ");
scanf("%s", name);

printf("Enter your age: ");
scanf("%d", &age);

printf("Your name is %s and your age is %d\n", name, age);

   return 0;
}

Output:

Output

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

Explanation:

We initially define the variables "name" and "age" in the previous scenario. Subsequently, the user is asked to input their name and age through the printf function. We employ the scanf function to capture and store the user's input in the respective variables. Finally, we use the printf function again to format the input.

Unformatted I/O in C

Unformatted input/output (I/O) pertains to a group of I/O tasks that involve the reading or writing of data in a byte stream without consideration for any specific layout. In the C programming language, unformatted I/O is accomplished using functions such as fread and fwrite. These functions allow for the direct reading and writing of data to and from files, bypassing any formatting processes.

Syntax:

Utilize the fwrite function syntax to output raw data:

Example

fwrite(data, size, count, file pointer);

Here, the variable "count" represents the quantity of elements to be recorded, "size" indicates the dimensions of each element to be written, and the file pointer points to the specific file where the data is intended to be stored.

The syntax for utilizing the fread function with unstructured input is as follows:

fread(data, size, count, file pointer);

In this structure, a reference to the buffer where the information will be retrieved, the dimensions of each item to be retrieved, the quantity of items to be retrieved, and a reference to the file from which the information will be retrieved.

Example

Let's explore a demonstration of unstructured input/output in C:

Example

#include <stdio.h>
int main() {
   char data[100];
   FILE *fp;

fp = fopen("file.txt", "w+");

fputs("This is an example of unformatted output.", fp);
fseek(fp, 0, SEEK_SET);

fread(data, sizeof (
char), 100, fp);

printf("%s\n", data);
fclose(fp);

return 0;
}

Output:

Output

This is an example of unformatted output.

Explanation:

In this illustration, we initially define a file pointer (fp) and a character array (data). Subsequently, the 'fopen' function is employed to open the 'file.txt' file in write mode, and the file pointer is linked to 'fp'. The phrase "This is an instance of unformatted output." is inserted into the file utilizing the 'fputs' method. Following this, the file pointer is repositioned to the start of the file with the 'fseek' function. Then, the content is displayed by utilizing the 'printf' function to retrieve the data 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:

  • 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.
  • 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.
  • 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.
  • Efficiency: Because unformatted I/O doesn't require any formatting or data type conversion, it is more efficient than formatted I/O.
  • Conclusion

Unformatted and Formatted Input/Output (I/O) operations are essential in C programming. Unformatted I/O involves handling data as raw bytes without any specific structure, while formatted I/O deals with data in a predetermined layout. Determining whether to use unformatted or formatted I/O depends on the specific needs of the program. Each type of I/O operation offers advantages and disadvantages.

Input Required

This code uses input(). Please provide values below: