Formatted and Unformatted Input/Output functions in C with Examples
Last Updated :
24 Jan, 2022
This article focuses on discussing the following topics in detail-
- Formatted I/O Functions.
- Unformatted I/O Functions.
- Formatted I/O Functions vs Unformatted I/O Functions.
Formatted I/O Functions
Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O functions can help to display the output to the user in different formats using the format specifiers. These I/O supports all data types like int, float, char, and many more.
Why they are called formatted I/O?
These functions are called formatted I/O functions because we can use format specifiers in these functions and hence, we can format these functions according to our needs.
List of some format specifiers-
S NO. |
Format Specifier |
Type |
Description |
1 |
%d |
int/signed int |
used for I/O signed integer value |
2 |
%c |
char |
Used for I/O character value |
3 |
%f |
float |
Used for I/O decimal floating-point value |
4 |
%s |
string |
Used for I/O string/group of characters |
5 |
%ld |
long int |
Used for I/O long signed integer value |
6 |
%u |
unsigned int |
Used for I/O unsigned integer value |
7 |
%i |
unsigned int |
used for the I/O integer value |
8 |
%lf |
double |
Used for I/O fractional or floating data |
9 |
%n |
prints |
prints nothing |
The following formatted I/O functions will be discussed in this section-
- printf()
- scanf()
- sprintf()
- sscanf()
printf():
printf() function is used in a C program to display any value like float, integer, character, string, etc on the console screen. It is a pre-defined function that is already declared in the stdio.h(header file).
Syntax 1:
To display any variable value.
printf(“Format Specifier”, var1, var2, …., varn);
Example:
C
#include <stdio.h>
int main()
{
int a;
a = 20;
printf ( "%d" , a);
return 0;
}
|
Syntax 2:
To display any string or a message
printf(“Enter the text which you want to display”);
Example:
C
#include <stdio.h>
int main()
{
printf ( "This is a string" );
return 0;
}
|
scanf():
scanf() function is used in the C program for reading or taking any value from the keyboard by the user, these values can be of any data type like integer, float, character, string, and many more. This function is declared in stdio.h(header file), that’s why it is also a pre-defined function. In scanf() function we use &(address-of operator) which is used to store the variable value on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
Example:
C
#include <stdio.h>
int main()
{
int num1;
printf ( "Enter a integer number: " );
scanf ( "%d" , &num1);
printf ( "You have entered %d" , num1);
return 0;
}
|
Output
Enter a integer number: You have entered 0
Output:
Enter a integer number: 56
You have entered 56
sprintf():
sprintf stands for “string print”. This function is similar to printf() function but this function prints the string into a character array instead of printing it on the console screen.
Syntax:
sprintf(array_name, “format specifier”, variable_name);
Example:
C
#include <stdio.h>
int main()
{
char str[50];
int a = 2, b = 8;
sprintf (str, "%d and %d are even number" ,
a, b);
printf ( "%s" , str);
return 0;
}
|
Output
2 and 8 are even number
sscanf():
sscanf stands for “string scanf”. This function is similar to scanf() function but this function reads data from the string or character array instead of the console screen.
Syntax:
sscanf(array_name, “format specifier”, &variable_name);
Example:
C
#include <stdio.h>
int main()
{
char str[50];
int a = 2, b = 8, c, d;
sprintf (str, "a = %d and b = %d" ,
a, b);
sscanf (str, "a = %d and b = %d" ,
&c, &d);
printf ( "c = %d and d = %d" , c, d);
return 0;
}
|
Unformatted Input/Output functions
Unformatted I/O functions are used only for character data type or character array/string and cannot be used for any other datatype. These functions are used to read single input from the user at the console and it allows to display the value at the console.
Why they are called unformatted I/O?
These functions are called unformatted I/O functions because we cannot use format specifiers in these functions and hence, cannot format these functions according to our needs.
The following unformatted I/O functions will be discussed in this section-
- getch()
- getche()
- getchar()
- putchar()
- gets()
- puts()
- putch()
getch():
getch() function reads a single character from the keyboard by the user but doesn’t display that character on the console screen and immediately returned without pressing enter key. This function is declared in conio.h(header file). getch() is also used for hold the screen.
Syntax:
getch();
or
variable-name = getch();
Example:
C
#include <conio.h>
#include <stdio.h>
int main()
{
printf ( "Enter any character: " );
getch();
return 0;
}
|
Output:
Enter any character:
getche():
getche() function reads a single character from the keyboard by the user and displays it on the console screen and immediately returns without pressing the enter key. This function is declared in conio.h(header file).
Syntax:
getche();
or
variable_name = getche();
Example:
C
#include <conio.h>
#include <stdio.h>
int main()
{
printf ( "Enter any character: " );
getche();
return 0;
}
|
Output:
Enter any character: g
getchar():
The getchar() function is used to read only a first single character from the keyboard whether multiple characters is typed by the user and this function reads one character at one time until and unless the enter key is pressed. This function is declared in stdio.h(header file)
Syntax:
Variable-name = getchar();
Example:
C
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf ( "Enter the character: " );
ch = getchar ();
printf ( "%c" , ch);
return 0;
}
|
Output:
Enter the character: a
a
putchar():
The putchar() function is used to display a single character at a time by passing that character directly to it or by passing a variable that has already stored a character. This function is declared in stdio.h(header file)
Syntax:
putchar(variable_name);
Example:
C
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf ( "Enter any character: " );
ch = getchar ();
putchar (ch);
return 0;
}
|
Output:
Enter any character: Z
Z
gets():
gets() function reads a group of characters or strings from the keyboard by the user and these characters get stored in a character array. This function allows us to write space-separated texts or strings. This function is declared in stdio.h(header file).
Syntax:
char str[length of string in number]; //Declare a char type variable of any length
gets(str);
Example:
C
#include <conio.h>
#include <stdio.h>
int main()
{
char name[50];
printf ( "Please enter some texts: " );
gets (name);
printf ( "You have entered: %s" ,
name);
return 0;
}
|
Output:
Please enter some texts: geeks for geeks
You have entered: geeks for geeks
puts():
In C programming puts() function is used to display a group of characters or strings which is already stored in a character array. This function is declared in stdio.h(header file).
Syntax:
puts(identifier_name );
Example:
C
#include <stdio.h>
int main()
{
char name[50];
printf ( "Enter your text: " );
gets (name);
printf ( "Your text is: " );
puts (name);
return 0;
}
|
Output:
Enter your text: GeeksforGeeks
Your text is: GeeksforGeeks
putch():
putch() function is used to display a single character which is given by the user and that character prints at the current cursor location. This function is declared in conio.h(header file)
Syntax:
putch(variable_name);
Example:
C
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf ( "Enter any character:\n " );
ch = getch();
printf ( "\nEntered character is: " );
putch(ch);
return 0;
}
|
Output:
Enter any character:
Entered character is: d
Formatted I/O vs Unformatted I/O
S No. |
Formatted I/O functions |
Unformatted I/O functions |
1 |
These functions allow us to take input or display output in the user’s desired format. |
These functions do not allow to take input or display output in user desired format. |
2 |
These functions support format specifiers. |
These functions do not support format specifiers. |
3 |
These are used for storing data more user friendly |
These functions are not more user-friendly. |
4 |
Here, we can use all data types. |
Here, we can use only character and string data types. |
5 |
printf(), scanf, sprintf() and sscanf() are examples of these functions. |
getch(), getche(), gets() and puts(), are some examples of these functions. |
Master C with our C Programming online course! From basics to advanced topics like data structures, enhance your skills with hands-on coding challenges. Take the Three 90 Challenge—complete 90% in 90 days, and earn a 90% refund. Start your learning journey today!
Similar Reads
Formatted and Unformatted Input/Output functions in C with Examples
This article focuses on discussing the following topics in detail- Formatted I/O Functions.Unformatted I/O Functions.Formatted I/O Functions vs Unformatted I/O Functions.Formatted I/O Functions Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O functions can help to dis
9 min read
All forms of formatted scanf() in C
C language has standard libraries that allow input and output in a program. The stdio.h or standard input-output library in C has methods for input and output. scanf(): The scanf() method n C Language, reads the value from the console as per the type specified. It returns an integer type, the number of successfully matched and assigned input items.
7 min read
isalpha() and isdigit() functions in C with cstring examples.
isalpha(c) is a function in C which can be used to check if the passed character is an alphabet or not. It returns a non-zero value if it's an alphabet else it returns 0. For example, it returns non-zero values for 'a' to 'z' and 'A' to 'Z' and zeroes for other characters.Similarly, isdigit(c) is a function in C which can be used to check if the pa
2 min read
asctime() and asctime_s() functions in C with Examples
asctime() function The asctime() function returns the pointer to the string that contains the information stored in the structure of the struct tm type. This function is used to return the local time defined by the system in string format. This function is defined in <time.h> header file. Syntaxchar *asctime(const struct tm* tm_ptr);Parameter
3 min read
atol(), atoll() and atof() functions in C/C++
In C/C++, atol(), atoll(), and atof() are functions used to convert strings to numbers of different types. These functions are Standard Library functions. In this article, we will learn these String-to-number conversion functions in C/C++.1. atol() in C/C++The atol() function converts a C-style string (array of characters), passed as an argument to
6 min read
C exit(), abort() and assert() Functions
The C exit(), abort(), and assert() functions are all used to terminate a C program but each of them works differently from the other. In this article, we will learn about exit, abort, and assert functions and their use in C programming language. 1. exit() in C The C exit() function is a standard library function used to terminate the calling proce
5 min read
Write one line functions for strcat() and strcmp()
Recursion can be used to do both tasks in one line. Below are one line implementations for stracat() and strcmp(). C /* my_strcat(dest, src) copies data of src to dest. To do so, it first reaches end of the string dest using recursive calls my_strcat(++dest, src). Once end of dest is reached, data is copied using (*dest++ = *src++)? my_strcat(dest,
2 min read
Functions that are executed before and after main() in C
With GCC family of C compilers, we can mark some functions to execute before and after main(). So some startup code can be executed before main() starts, and some cleanup code can be executed after main() ends. For example, in the following program, myStartupFun() is called before main() and myCleanupFun() is called after main(). C #include<stdi
1 min read
fill() and fill_n() functions in C++ STL
A vector, once declared, has all its values initialized to zero. Following is an example code to demonstrate the same. CPP // C++ program for displaying the default initialization // of the vector vect[] #include<bits/stdc++.h> using namespace std; int main() { // Creating a vector of size 8 vector<int> vect(8); // Printing default val
3 min read
strdup() and strndup() functions in C/C++
The strdup() and strndup() functions are used to duplicate a string. strdup() : Syntax : char *strdup(const char *s); This function returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by s. The memory obtained is done dynamically using malloc and hence it can be freed using free(). It returns a pointer
2 min read