Table of contents
1.
Introduction
2.
Formatted I/O Functions
2.1.
1. printf()
2.2.
2. scanf()
2.3.
3. sprintf()
2.4.
4. sscanf()
3.
Why they are called formatted I/O?
4.
Unformatted I/O Functions
4.1.
1. getch()
4.2.
2. getche()
4.3.
3. getchar()
4.4.
4. putchar()
4.5.
5. gets()
4.6.
6. puts()
4.7.
7. putch()
5.
Why are they called unformatted I/O?
6.
Formatted I/O Functions vs Unformatted I/O Functions
7.
Frequently Asked Questions 
7.1.
Can we mix formatted and unformatted I/O functions in a program?
7.2.
Is it necessary to use format specifiers with formatted I/O functions?
7.3.
Are unformatted I/O functions faster than formatted I/O functions?
8.
Conclusion
Last Updated: Dec 4, 2024
Easy

Formatted and Unformatted Input Output in C

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C, input and output operations are very important for interacting with users and exchanging data between the program and external devices. C provides two main types of I/O functions: formatted and unformatted. Formatted I/O functions like printf and scanf help you read and write data in a specific format, while unformatted I/O functions getchar and puts handle raw data without any formatting. 

Formatted and Unformatted Input Output in C

In this article, we'll discuss both types of I/O functions, their use, and key differences.

Formatted I/O Functions

Formatted I/O functions in C allow you to read & write data in a specified format. They provide control over how the data is presented or interpreted. The most commonly used formatted I/O functions are:

1. printf()

 This function is used to print formatted output to the console. It takes a format string that specifies how the data should be displayed, along with the corresponding arguments. For example:

#include <stdio.h>
int main() {
    int age = 20;
    printf("My age is %d\n", age);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

My age is 20

2. scanf()

This function is used to read formatted input from the console. It takes a format string that specifies the type of data to be read, along with the corresponding pointers to variables where the input will be stored. For example:

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You entered: %d\n", age);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Enter your age: 20
You entered: 20

3. sprintf()

This function is similar to printf(), but instead of printing to the console, it writes the formatted output to a character array (string). It is useful when you need to store the formatted output for later use. For example:

#include <stdio.h>
int main() {
    char str[100];
    int age = 20;
    sprintf(str, "My age is %d", age);
    printf("%s\n", str);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

My age is 20

4. sscanf()

This function is similar to scanf(), but instead of reading from the console, it reads formatted input from a string. It is useful when you have a string containing formatted data that needs to be parsed. For example:

#include <stdio.h>
int main() {
    char str[] = "My age is 20";
    int age;
    sscanf(str, "My age is %d", &age);
    printf("Extracted age: %d\n", age);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Extracted age: 20

Why they are called formatted I/O?

Formatted I/O functions are called "formatted" because they allow you to specify the format of the input or output using format specifiers. Format specifiers are special characters that define how the data should be interpreted or displayed. For example, %d is used for integers, %f for floating-point numbers, %s for strings, etc. This formatting capability provides flexibility & control over how data is read or written.

Unformatted I/O Functions

Unformatted I/O functions in C handle raw data without any formatting. They deal with individual characters or strings as a whole. Some commonly used unformatted I/O functions are:

1. getch()

This function reads a single character from the console without echoing it back to the screen. It doesn't wait for the user to press Enter. For example:

#include <stdio.h>
#include <conio.h>
int main() {
    char ch;
    printf("Press any key: ");
    ch = getch();
    printf("\nYou pressed: %c\n", ch);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Press any key: (User presses 'A')
You pressed: A

2. getche()

This function is similar to getch(), but it echoes the character back to the screen. For example:

#include <stdio.h>
#include <conio.h>
int main() {
    char ch;
    printf("Press any key: ");
    ch = getche();
    printf("\nYou pressed: %c\n", ch);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Press any key: A (User presses 'A')
You pressed: A

3. getchar()

This function reads a single character from the console & waits for the user to press Enter. It returns the ASCII value of the character. For example:

#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: %c\n", ch);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Enter a character: A (User enters 'A' & presses Enter)
You entered: A

4. putchar()

This function writes a single character to the console. It takes a character as an argument & returns the same character if successful. For example:

#include <stdio.h>

int main() {
    char ch = 'A';
    putchar(ch);
    putchar('\n');
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

A

5. gets()

This function reads a line of text from the console until a newline character ('\n') is encountered. It stores the input in a character array (string). However, gets() is considered unsafe as it doesn't perform bounds checking & can lead to buffer overflow vulnerabilities. For example:

#include <stdio.h>
int main() {
    char str[100];
    printf("Enter a string: ");
    gets(str);
    printf("You entered: %s\n", str);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Enter a string: Hello, World!
You entered: Hello, World!

6. puts()

This function writes a string to the console followed by a newline character. It takes a string as an argument & returns a non-negative value if successful. For example:

#include <stdio.h>
int main() {
    char str[] = "Hello, World!";
    puts(str);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Hello, World!

7. putch()

This function is similar to putchar(), but it is a non-standard function specific to certain compilers like Turbo C. It writes a single character to the console. For example:

#include <stdio.h>
#include <conio.h>
int main() {
    char ch = 'A';
    putch(ch);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

A

Why are they called unformatted I/O?

Unformatted I/O functions are called "unformatted" because they deal with raw data without any formatting. They handle individual characters or strings as a whole without the need for format specifiers. Unformatted I/O functions provide a simple way to read & write characters or strings directly without any formatting overhead.

Formatted I/O Functions vs Unformatted I/O Functions

Formatted I/O FunctionsUnformatted I/O Functions
Formatted I/O functions allow you to read and write data in a specific format, providing control over how data is presented or interpreted.Unformatted I/O functions handle raw data without any formatting, dealing with individual characters or strings as a whole.
Formatted I/O functions use format specifiers to define the type and format of the data being read or written.Unformatted I/O functions do not require format specifiers as they work with raw characters or strings directly.
Examples of formatted I/O functions include printf(), scanf(), sprintf(), and sscanf().Examples of unformatted I/O functions include getch(), getche(), getchar(), putchar(), gets(), puts(), putch().
Formatted I/O functions provide flexibility in handling different data types, such as integers, floating-point numbers, and strings, by using appropriate format specifiers.Unformatted I/O functions are simpler and more straightforward. They deal with characters or strings as a whole without any formatting.
Formatted I/O functions allow you to specify the width, precision, and alignment of the output, giving you more control over the presentation of data.Unformatted I/O functions do not provide formatting options and work with raw data as they are.
Formatted I/O functions are generally used when you need to read or write data in a specific format or when you want to control the presentation of the data.Unformatted I/O functions are useful when you want to read or write individual characters or strings without any formatting requirements.

Frequently Asked Questions 

Can we mix formatted and unformatted I/O functions in a program?

Yes, you can use both formatted and unformatted I/O functions in the same program as per your requirements.

Is it necessary to use format specifiers with formatted I/O functions?

Yes, format specifiers are essential when using formatted I/O functions to specify the type and format of the data.

Are unformatted I/O functions faster than formatted I/O functions?

Generally, unformatted I/O functions are faster because they don't have the overhead of parsing format specifiers.

Conclusion

In this article, we learned about formatted and unformatted I/O functions in C. Formatted I/O functions like printf(), scanf(), sprintf(), and sscanf() provide control over data formatting using format specifiers. Unformatted I/O functions such as getch(), getche(), getchar(), putchar(), gets(), puts(), and putch() handle raw data without formatting. 

You can also check out our other blogs on Code360.

Live masterclass