Formatted And Unformatted Input/Output Functions In C With Examples

Formatted And Unformatted Input/Output Functions In C With Examples

Formatted IO Functions

Formatted I/O functions are essential for handling user inputs and displaying outputs in a user-friendly way. They enable programmers to:

  1. Receive User Inputs: Formatted input functions help programs collect user input in a structured manner. They use format specifiers to interpret and extract specific data types from user input.
  2. Present Data to Users: Formatted output functions allow programs to present data to users in various formats. Format specifiers are used to control how data is displayed, making it more readable and visually appealing.
  3. Support Multiple Data Types: These I/O functions are versatile and support a wide range of data types, including integers, floating-point numbers, characters, and more. Each data type has corresponding format specifiers for precise formatting.
  4. Formatting Control: Programmers can use format specifiers to control the alignment, width, precision, and other formatting aspects of displayed data, ensuring it’s presented as intended.

Why they are called Formatted I/0 Functions

These functions are called formatted I/O functions as they can use format specifiers in these functions. Moreover, we can format these functions according to our needs.

Here are the list of some specifiers

NO.Format Specifier          Type            Description                                                    
1%dint/signed intused for I/O signed integer value
2%ccharUsed for I/O character value
3%ffloatUsed for I/O decimal floating-point value        
4%sstringUsed for I/O string/group of characters                                                       
5%ldlong intUsed for I/O long signed integer value
6%uunsigned int   Used for I/O unsigned integer value
7%iunsigned intused for the I/O integer value
8%lfdoubleUsed for I/O fractional or floating data 
9%nprintsprints nothing 

The following formatted I/O functions will be discussed in this section-

  1. printf()
  2. scanf()
  3. sprintf()
  4. sscanf()
  1. printf():

In C, printf() is a built-in function used to display values like numbers, characters, and strings on the console screen. It’s pre-defined in the stdio.h header, allowing easy output formatting in C programs.

Syntax 1

printf(“Format Specifier”, var1, var2, …., varn);  

Example

// C program to implement
// printf() function
#include <stdio.h>
  
// Driver code
int main()
{
    // Declaring an int type variable
    int a;
  
    // Assigning a value in a variable
    a = 20;
  
    // Printing the value of a variable
    printf("%d", a);
  
    return 0;
}

Output

20

Syntax 2:  

printf(“Enter the text which you want to display”);

Example

// C program to implement
// printf() function
#include <stdio.h>
  
// Driver code
int main()
{
    // Displays the string written
    // inside the double quotes
    printf("This is a string");
    return 0;
}

Output

This is a string

2. scanf():

  • scanf(): In C, scanf() is a built-in function for reading user input from the keyboard. It can read values of various data types like integers, floats, characters, and strings. scanf() is a pre-defined function declared in the stdio.h header file. It uses the & (address-of operator) to store user input in the memory location of a variable.

Syntax

scanf(“Format Specifier”, &var1, &var2, …., &varn);  

Example

// C program to implement
// scanf() function
#include <stdio.h>
  
// Driver code
int main()
{
    int num1;
  
    // Printing a message on
    // the output screen
    printf("Enter a integer number: ");
  
    // Taking an integer value
    // from keyboard
    scanf("%d", &num1);
  
    // Displaying the entered value
    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

3. sprintf():

sprintf(): Short for “string print,” sprintf() is similar to printf() but it stores the formatted string into a character array instead of displaying it on the console screen.

Syntax

// C program to implement
// the sprintf() function
#include <stdio.h>
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8;
  
    // The string "2 and 8 are even number"
    // is now stored into str
    sprintf(str, "%d and %d are even number",
            a, b);
  
    // Displays the string
    printf("%s", str);
    return 0;
}

Output

2 and 8 are even number

4. sscanf():

  • sscanf(): Abbreviated for “string scanf,” sscanf() resembles scanf() but reads data from a string or character array rather than from the console screen.

Syntax

sscanf(array_name, “format specifier”, &variable_name);

Example

// C program to implement
// sscanf() function
#include <stdio.h>
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8, c, d;
  
    // The string "a = 2 and b = 8"
    // is now stored into str
    // character array
    sprintf(str, "a = %d and b = %d",
            a, b);
  
    // The value of a and b is now in
    // c and d
    sscanf(str, "a = %d and b = %d",
           &c, &d);
  
    // Displays the value of c and d
    printf("c = %d and d = %d", c, d);
    return 0;
}

Output

c = 2 and d = 8

Unformatted Input/Output functions

  • Unformatted I/O Functions: These functions are used exclusively for character data types or character arrays/strings. They are designed for reading single inputs from the user at the console and for displaying values on the console.
  • Why “Unformatted”?: They are referred to as “unformatted” I/O functions because they do not support format specifiers. Unlike formatted I/O functions like printf() and scanf(), you cannot use format specifiers to control the formatting of the data. They display or read data as-is without formatting options.

The following are unformatted I/O functions

  1. getch()
  2. getche()
  3. getchar()
  4. putchar()
  5. gets()
  6. puts()
  7. putch()
  1. getch():

getch(): In C, getch() reads a single character from the keyboard without displaying it on the console screen. It immediately returns without requiring the user to press the Enter key. This function is declared in the conio.h header file and is often used for controlling screen display.

Syntax

getch(); 

or 

variable-name = getch();

Example

// C program to implement
// getch() function
#include <conio.h>
#include <stdio.h>
  
// Driver code
int main()
{
    printf("Enter any character: ");
  
    // Reads a character but
    // not displays
    getch();
  
    return 0;
}

Output

Enter any character: 

2. getche():

In C, getche() reads a single character from the keyboard, displays it on the console screen, and immediately returns without requiring the user to press the Enter key. This function is declared in the conio.h header file.

Syntax

getche(); 

or 

variable_name = getche();

Example

// C program to implement
// the getche() function
#include <conio.h>
#include <stdio.h>
  
// Driver code
int main()
{
    printf("Enter any character: ");
  
    // Reads a character and
    // displays immediately
    getche();
    return 0;
}

Output

Enter any character: g

3. getchar(): 

In C, getchar() reads a single character from the keyboard and waits until the Enter key is pressed. It processes one character at a time. This function is declared in the stdio.h header file

Syntax

Variable-name = getchar();

Example


// C program to implement
// the getchar() function
#include <conio.h>
#include <stdio.h>
  
// Driver code
int main()
{
    // Declaring a char type variable
    char ch;
  
    printf("Enter the character: ");
  
    // Taking a character from keyboard
    ch = getchar();
  
    // Displays the value of ch
    printf("%c", ch);
    return 0;
}

Output

Enter the character: a

a

4. putchar():

In C, putchar() is used to display a single character at a time, either by passing the character directly or by using a variable that stores the character. This function is declared in the stdio.h header file.

Syntax

putchar(variable_name);  

Example

// C program to implement
// the putchar() function
#include <conio.h>
#include <stdio.h>
  
// Driver code
int main()
{
    char ch;
    printf("Enter any character: ");
  
    // Reads a character
    ch = getchar();
  
    // Displays that character
    putchar(ch);
    return 0;
}

Output

Enter any character: Z

Z

5. gets():

In C, gets() reads a group of characters or strings from the keyboard, and these characters are stored in a character array. It allows you to input space-separated texts or strings. This function is declared in the stdio.h header file. However, please note that gets() is considered unsafe due to the risk of buffer overflow and is generally discouraged in favor of safer alternatives like fgets().

Syntax

char str[length of string in number]; //Declare a char type variable of any length 

gets(str); 

Example

// C program to implement
// the gets() function
#include <conio.h>
#include <stdio.h>
  
// Driver code
int main()
{
    // Declaring a char type array
    // of length 50 characters
    char name[50];
  
    printf("Please enter some texts: ");
  
    // Reading a line of character or
    // a string
    gets(name);
  
    // Displaying this line of character
    // or a string
    printf("You have entered: %s",
           name);
    return 0;
}

Output

Please enter some texts: Skill Vertex

You have entered: Skill Vertex

6. puts():

In C programming, puts() is used to display a group of characters or strings that are already stored in a character array. This function is declared in the stdio.h header file.

Syntax

 puts(identifier_name );

Example

// C program to implement
// the puts() function
#include <stdio.h>
  
// Driver code
int main()
{
    char name[50];
    printf("Enter your text: ");
  
    // Reads string from user
    gets(name);
  
    printf("Your text is: ");
  
    // Displays string
    puts(name);
  
    return 0;
}

Output

Enter your text: Skill Vertex

Your text is: Skill Vertex

7. putch():

In C, putch() is used to display a single character provided by the user, and it prints the character at the current cursor location. This function is declared in the conio.h header file

Syntax

putch(variable_name);  

Example

// C program to implement
// the putch() functions
#include <conio.h>
#include <stdio.h>
  
// Driver code
int main()
{
    char ch;
    printf("Enter any character:\n ");
  
    // Reads a character from the keyboard
    ch = getch();
  
    printf("\nEntered character is: ");
  
    // Displays that character on the console
    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                       
1These Formatted I/O functions will provide input or display output in the user’s desired format.Whereas, Unformatted I/O functions won’t allow to take input or display output in user desired format.
2They will support format specifiers.They will support format specifiers.
3These will store data more user-friendlyThese functions are notuser-friendly.
4In Formatted I/0 Functions, we can use all data types.These functions are not user-friendly.
5Examples -printf(), scanf, sprintf() and sscanf()Example-getch(), getche(), gets() and puts()

FAQ- Formatted and Unformatted Input/Output functions in C with Examples

Q1. What is an example of formatted input output statement?

Ans. Interactive Input

Example: int age; double gpa; char answer; printf(“Please enter your age: “); scanf(“%d”, &age); printf(“Please enter your gpa: “); scanf(“%lf”, %gpa); printf(“Do you like pie (Y/N)? “); scanf(“%c”, %answer);

Q2. Which are the types of formatted input and output?

Ans. %d. Integer: int/ signed int. Used to read and print integer values.
%c. Character: char. Used to read and print Character values.
%f. Floating point: float. Used to read and print decimal values.
%s. String.
%ld. long int.
% you. Unsigned int.
%i. Unsigned int.
%lf. double.

Q3. What is formatted input output functions in C with example?

Ans. Formatted I/O functions are used in programming to handle input from users and display information in different formats. These functions support various data types like integers, floating-point numbers, and characters, and they use format specifiers to control how data is presented to users.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment