4.3 formatted Input/Output Functions
Formatted Input/Output functions in C programming are used to read user input from standard input devices and write program output to standard output devices in a user -friendly way. The most common formatted input/output function in C, defined in header file “stdio.h” are:
The syntax of these functions are as follows:
printf("control string", arg1, arg2, ...., argn);
scanf("control string", &arg1, &arg2, ...., &argn);
4.3.1. Control String (flags, field width, precision, and specifier)
A control string in the context of C programming typically refers to the format string used with functions like printf and scanf(). It is a character string that contains format specifiers and optional additional characters, which control the conversion and formatting of input and output.
The format specifiers in a control string indicate the type and format of the data being processed. They begin with the percent sign (%) and are followed by a character that represents the type of conversion to be performed. For example, %d is a format specifier for integers, %f for floating-point numbers, %s for strings, etc.The most commonly used conversion characters are:
Conversion Character |
Meaning |
%c |
Read a single character |
%d |
Read a decimal Integer |
%e |
Read a floating point value |
%f |
Read a floating point value |
%g |
Read a floating point value |
%h |
Read a short integer value |
%i |
Read a decimal, hexadecimal or octal integer |
%o |
Read an octal integer |
%s |
Read a string |
%x |
Read a hexadecimal integer |
The following letters may be used as prefix for certain conversion character:
h for short integer
I for long integer or double
L for long double
C program for control string with a format specifier and additional characters:
#include<stdio.h>
int main()
{
int number = 42;
printf("The number is: %05d\n", number);
return 0;
}
Output:
The number is: 00042
In this example, the control string is "The number is: %05d\n". It includes the format specifier %d for integers and the additional characters '05', where '0' is a flag for zero-padding and '5' is the field width, indicating that the number should be printed with a minimum width of 5 characters, zero-padded if necessary.
4.3.2. Formatted I/O (scanf(), printf())
scanf() function
To read data in from standard input (keyboard), we call the scanf function. The basic form of a call to scanf is:
scanf("control string", &arg1, &arg2, ...., &argn);
C program to understand basic usage of scanf() function:
#include<stdio.h>
int main()
{
int num;
float fnum;
char ch;
printf("\nEnter a number: ");
//read integer using %d
scanf("%d", &num);
printf("\nEnter a floating point number: ");
//read float value using %f
scanf("%f", &fnum);
printf("\nEnter a character: ");
//read character using %c
scanf(" %c", &ch);
printf("\n you entered following values; ");
printf("\n%d\t%f\t%c\n", num, fnum, ch);
return 0;
}
Output:
Enter a number: 10
Enter a floating point number: 2.4
Enter a character: a
you entered following values;
10 2.400000 a
Now, let us understand the control string used in scanf() function:
Flags modify the behavior of conversion. In the case of scanf, the most common flag is *, which indicates that the corresponding input should be read but ignored.
#include <stdio.h>
int main() {
int x, y;
// Ignore the first integer
scanf("%*d %d", &y);
printf("In y: %d\n", y);
return 0;
}
Field width specifies the maximum number of characters to be read for the input. It prevents overflows and limits the number of characters to be read.
#include <stdio.h>
int main() {
char name[10];
// Read up to 9 characters for the name
scanf("%9s", name);
printf("Name: %s\n", name);
return 0;
}
Precision in scanf is not as commonly used as in printf. However, for string inputs, it specifies the maximum number of characters to be read.
#include <stdio.h>
int main() {
char greeting[5];
// Read up to 4 characters for the greeting
scanf("%4s", greeting);
printf("Greeting: %s\n", greeting);
return 0;
}
Specifiers in scanf are used to indicate the type of input to be read.
#include <stdio.h>
int main() {
int num;
float price;
// Read an integer followed by a floating-point number
scanf("%d %f", &num, &price);
printf("Integer: %d\n", num);
printf("Float: %.2f\n", price);
return 0;
}
printf() function
To write program data to the standard output device (terminal) we call printf() function. The basic form of printf() function is as follows:
printf("control string", list of variables);
where:
C program to understand basic usage of printf() function:
#include<stdio.h>
int main()
{
int a = 10;
float b = 2.5;
char c = 'x';
printf("\nHello world\n\n");
printf("\na= %d\tb=%f\tc=%c\n", a, b, c);
printf("\"Hello World\"\n");
return 0;
}
Output:
Hello world
a= 10 b=2.500000 c=x
"Hello World"
Various control string used in printf() function to control output display:
Flags:
Flags modify the output format by changing the alignment or padding. Two common flags are '-' for left-justification and '0' for zero-padding.
#include <stdio.h>
int main() {
int integerNumber = 42;
// Left-justified with a field width of 10
printf("Left-justified: |%-10d|\n", integerNumber);
// Zero-padded with a field width of 10
printf("Zero-padded: |%010d|\n", integerNumber);
return 0;
}
Field Width:
Field width specifies the minimum width of the output field. It can be used to ensure a minimum number of characters are printed.
#include <stdio.h>
int main() {
int integerNumber = 42;
// Minimum field width of 5
printf("Minimum width of 5: |%5d|\n", integerNumber);
// Minimum field width of 10 for a string
printf("Minimum width of 10: |%10s|\n", "Hello");
return 0;
}
Precision:
Precision is used with floating-point numbers and specifies the number of digits after the decimal point.
#include <stdio.h>
int main() {
float floatingNumber = 3.14159;
// Display two digits after the decimal point
printf("Two digits after decimal: |%.2f|\n", floatingNumber);
return 0;
}
Specifiers:
Specifiers define the type of the variable to be printed. Common specifiers include %c for characters and %s for strings.
#include <stdio.h>
int main() {
char character = 'A';
// Display as a character
printf("Character: |%c|\n", character);
// Display as a string
printf("String: |%s|\n", "Hello, World!");
return 0;
}