×

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In this function,  we can use format specifiers in this functions.

Format specifiers in C:

Serial numberFormat specifierData typeDescription
1.%dInteger: int/ signed intUsed to read and print integer values.
2.%cCharacter: charUsed to read and print Character values.
3.%fFloating point: floatUsed to read and print decimal values
4.%sStringThe String is the collection of characters.
5.%ldlong intUsed to read and print large integer values.
6.% youUnsigned intUsed to read and print unsigned integer values.
7.%iUnsigned intUsed to read and print unsigned integer values.
8.%lfdoubleUsed to read and print decimal values or fractional values.
9.%nprintsIt is used to print nothing.

Formatted input and output functions in C: 

  1. printf()
  2. scanf()
  3. sprint()
  4. sscanf()

1. printf():

In C printf() is a in-build function present in stdio.h header file. It is used to print the user input on the console output screen. We can print all data types like int, char, double, float etc., by the printf() function.

Syntax:

printf(“Format Specifier ”, variable);
to print only statement: printf(“ Printing statements ”);

Example program to print the given variable using printf() function:

// program to show the function of printf() function
#include <stdio.h>
int main()
{
  int a; // variable declaration
  a = 100; // Assigning the value to the declared variable
  // printing the value of the  given variable
  printf("The value of the given variable is: %d", a); 
  return 0;
}

Output:

The value of the given variable is: 100

Example program to  print only statement:

// program to show the function of printf() function
#include <stdio.h>
int main()
{
  // printing the statement
  printf(" Hello, Welcome to JavaTpoint Tutorials and Examples: "); 
  return 0;
}

Output:

Hello, Welcome to JavaTpoint Tutorials and Examples:

In this example, we are not declaring any variables and not initializing any values. Only printing the "Hello, Welcome to JavaTpoint Tutorials and Examples:” statement.

2. scanf():

In C scanf() is a in-build function present in stdio.h header file. It is used to read the user input. By using printf(), we can display the desired output. We can read all data types like int, char, double, float etc., by the scanf() function.

   Syntax:

scanf(“format specifier”, &variable name);

Example program to read the input from the user

// program to show the function of scanf() function
#include <stdio.h>
int main()
{
    int A, B, sum;
    printf(" Enter the value of A: ");
    scanf(" %d", &A); // reading a value using scanf() function
    printf(" Enter the value of B: ");
    scanf(" %d", &B); // reading b value using scanf() function
    sum= A+B; // calculating the sum 
  // printing the addituion of two numbers
  printf(" The sum of the two numbers is= %d ", sum); 
  return 0;
}

Output:

Enter the value of A: 10
Enter the value of B: 30
The sum of the two numbers is= 40

3. sprintf()

sprintf stands for “string print”. In C sprintf() is a in-build function present in stdio.h header file. It is a function used in file handling. By sprintf(), we can send formatted output to the String instead of printing on the console output screen. It is similar to printf() function.

Syntax:

sprintf( array name, “ format specifier”, variable name);

Example on sprintf() function:

// C program to show the function of sprintf() function
#include <stdio.h>
int main()
{
  char str[50]; // string declaration
int A, B; // integer declaration
  printf(" Enter the value of A: ");
  scanf(" %d", &A); // reading a value using scanf() function
  printf(" Enter the value of B: ");
  scanf(" %d", &B); // reading b value using scanf() function
  sprintf(str, "%d and %d values are integer values", A, B);
  /* after execution of the above statement " A and B values are integer values " is stored in the str string */
  printf("%s", str); // // Displays the string
  return 0;
}

Output:

Enter the value of A: 50
Enter the value of B: 100
50 and 100 values are integer values

4. sscanf(): 

sscanf stands for “string scanf”. In C sscanf() is a in-build function present in stdio.h header file. It is a function used in file handling. By sscanf(), we can read formatted input from the String instead of reading from the console output screen. It is similar to the scanf() function.

Syntax:

sscanf( array name, “ format specifier”, &variable name);   

Example program on sscanf() function in C:

// C program to implement sscanf() function
#include <stdio.h>
int main()
{
  char str[50];
  int A, B, C, D; // integer declaration
  printf(" Enter the value of A: ");
  scanf(" %d", &A); // reading a value using scanf() function
  printf(" Enter the value of B: ");
  scanf(" %d", &B); // reading b value using scanf() function
  sprintf(str, "A = %d and B = %d", A, B);
  /* after execution of the above statement " A and B " is stored in the str string */
  printf(" %s", str);
  sscanf(str, "A = %d and B = %d", &C, &D);
  /* after execution of the above statement A and B values are copied into C and D from str String */
  printf(" \nC = %d and D = %d", C, D); // printing the values of C and D
  return 0;
}

Output:

Enter the value of A: 100
Enter the value of B: 200
A = 100 and B = 200 
C = 100 and D = 200

Advantages of formatted Input and Output functions in C:

  1. We can read and print the desired input in the desired format.
  2. Formatted Input and Output functions support all data types.
  3. We can store data in a user-friendly manner.

Conclusion:

In this article, we learned about format specifiers, their uses, and the syntax of format specifiers. Formatted Input and Output function in C, types of formatted I/O functions in C, the syntax of formatted I/O functions, examples of formatted Input and Output function, advantages of formatted Input and Output functions in C.


Related Topics

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.