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 number | Format specifier | Data type | Description |
1. | %d | Integer: int/ signed int | Used to read and print integer values. |
2. | %c | Character: char | Used to read and print Character values. |
3. | %f | Floating point: float | Used to read and print decimal values |
4. | %s | String | The String is the collection of characters. |
5. | %ld | long int | Used to read and print large integer values. |
6. | % you | Unsigned int | Used to read and print unsigned integer values. |
7. | %i | Unsigned int | Used to read and print unsigned integer values. |
8. | %lf | double | Used to read and print decimal values or fractional values. |
9. | %n | prints | It is used to print nothing. |
Formatted input and output functions in C:
- printf()
- scanf()
- sprint()
- 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:
- We can read and print the desired input in the desired format.
- Formatted Input and Output functions support all data types.
- 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.