×

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

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.