Formatted Vs Unformatted Input/Output

Before we go into detail about the printf function, it’s important to understand the difference between formatted and unformatted input/output functions. In this tutorial, we will cover two topics: formatted input/output functions and unformatted input/output functions.

Let’s start with the first topic.


Formatted Input/Output Function

Formatted input/output functions allow users to provide inputs and display outputs in a specific format using format specifiers. For instance, the printf function can be used to display output in a particular format as desired by the programmer.

To better understand how formatted input/output functions work, let’s look at a simple example. Here is a code snippet that uses the printf function to print the value of variable x, along with some text for clarity.

#include <stdio.h>

int main()
{
    int x = 20;
    printf("The value of x is %d", x);
    return 0;
}

Inside the main() function, the first line tells us that x is a variable of type integer (int means integer) and it has a value of 20. Think of a variable as a container or a box that holds a value for later use. In our example, the variable x holds the value 20. Instead of using the number 20 directly in our code, we can use the variable x.

The next line contains a printf() function, which is used to display a value on the screen. A function is something that takes input(s), processes them, and produces an output. In our example, the printf() function has two inputs:

  1. “The value of x is %d”
  2. x

The first input is a string, which is identified by the double quotes in the C programming language. The second input is the variable x, which we created in the first line.

Now, let’s focus on the first input – “The value of x is %d”. In this string, %d is a format specifier or a placeholder for an integer value, which will be provided as the second input to the printf() function.

A format specifier is used to indicate the position where a separate value will be inserted. When we run the code, %d will be replaced by the value of x, resulting in the output “The value of x is 20” (without the double quotes) being displayed on the console.

There are various format specifiers available, such as %c for characters and %s for strings. However, we will discuss them later. For now, it’s important to understand that functions like printf are called formatted output functions because they produce outputs based on the format specified by the user using format specifiers. These format specifiers help determine the type of data being used and how it should be displayed. Similarly, we have formatted input functions too which will be discussed in the next chapter.


Unformatted Input/Output

Unformatted input/output (I/O) functions are used to accept inputs and generate outputs without utilizing format specifiers. These functions are called unformatted I/O functions because they do not rely on format specifiers to process inputs or generate outputs based on user-defined formats.

For instance, the getchar() function is an example of an unformatted input function. It is employed to retrieve the first individual character from a string entered by the user in the console.

You can run the following program to observe the functionality of the getchar() function:

#include <stdio.h>

int main()
{
    char c;
    c = getchar();
    printf("%c", c);
    return 0;
}

Inside the main() function, the first line declares a variable called ‘c’ of character type. This is the way a variable is created in the C programming language.

In the second line, the getchar() function is invoked. It retrieves the character input entered by the user in the console and assigns it to the variable ‘c’. The ‘=’ symbol is an assignment operator, used to assign the value generated on the right-hand side of the operator to the variable on the left-hand side. Similar to the statement ‘x = 10’, where the value 10 is assigned to the variable ‘x’.

In the third line, the printf() function is called. The ‘%c’ is a placeholder that represents a character. It will be substituted with the character stored in the variable ‘c’ when the code is executed.

When the code is built and executed, a blank screen with a blinking cursor will appear, prompting for input. Let’s input the characters ‘S’, ‘a’, ‘m’, and press the enter key.

You will observe that only the first character, ‘S’, is printed. This indicates that the getchar() function has returned the first character of the string entered by the user.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.