Menu Close

Input/Output Format Specifiers in C Program

Posted in C Programming

Format specifiers are used in C programming language to format input and output functions. A format string determines the format of the input and output.

1. The conversion specifiers for printf()

The format string always begins with the ‘%’ character.

Format Specifiers in C Program
Format Specifiers in C Program for printf()

1.1 C Flag Format Specifiers

These flags can be combined with format specifiers to control the output format in C language.

C language flag format specifiers include the following:

  • -: Left-align the output. Right-justified if this flag is not specified.
  • +: Print a plus sign (+) for positive numbers, If the value is negative, add a minus sign (-) before it.
  • 0: Pad the output with zeros. For d, i, o, u, x, X, e, E, f, g, and G conversions, leading zeroes (following any indication of sign or base) are used to pad to the field width. No space padding is performed. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 flag is ignored. If the 0 and ' flags both appear, the grouping characters are inserted before the zero padding.
  • (space): If the signed value is positive, display a space character before the value. If the signed value is negative, add a minus sign (-) before the value.
  • #: Use an alternative format for certain conversions.
  • .: Specify precision for floating-point conversions.
  • *: Use a variable argument to specify field width or precision.
  • l or L: Interpret the argument as a long integer or a long double.
  • h: Interpret the argument as a short integer or a character.

More about #

Convert the result to another format:

  • If it is %o, start with a leading 0.
  • If it is %x or %X, start with 0x or 0X.
  • For all floating-point types, forcibly retain the decimal point.
  • For %g or %G format, ensure it includes trailing zeros.

1.2  C width and Precision Format Specifiers

The minimum field width can be specified as a decimal digit string following any flag specifier, in which case the field width is set to the specified number of columns. The field width can also be specified as asterisk (*) in which case an additional argument of type int is accessed to determine the field width. For example, to print an integer x in a field width determined by the value of the int variable w, you would write the D statement:

printf("%*d", w, x);

The field width can also be specified using a ? character to indicate that the field width should be set based on the number of characters required to format an address in hexadecimal in the data model of the operating system kernel. The width is set to 8 if the kernel is using the 32–bit data model, or to 16 if the kernel is using the 64–bit data model.

Related:   What is the Difference Between printf() and scanf() ?

The precision for the conversion can be specified as a decimal digit string following a period (.) or by an asterisk (*) following a period. If an asterisk is used to specify the precision, an additional argument of type int prior to the conversion argument is accessed to determine the precision. If both width and precision are specified as asterisks, the order of arguments to printf() for the conversion should appear in the following order: width, precision, value.

 

width 含义 .precision 含义
number Specifies the minimum field width as an integer value. If the output is shorter than the specified width, it will be padded with spaces on the left. .number Output Precision
For %e,%E, and %f,Number of decimal places
For %g,%G,Maximum number of significant digits.
For %s,Maximum number of characters to be printed.
For integers,  Minimum number of digits to be printed.
* The value of “number” is determined by the corresponding variable in the list of items to be printed. .* Is determined by the corresponding variable in the list of items to be printed.

1.3 Length Format

The combination of length and specifier determines how to interpret the data type of the corresponding variables in the list to be printed. The combination relationship is shown in the following table.

length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char signed char*
h short int unsigned short int short int*
l long int unsigned long int wint_t wchar_t* long int*
ll long long int unsigned long long int long long int*
j intmax_t uintmax_t intmax_t*
z size_t size_t size_t*
t ptrdiff_t ptrdiff_t ptrdiff_t*
L long double

Note:

(1) intmax_t and uintmax_t are defined in stdint.h and are 128-bit integer types.

(2) size_t is defined in stddef.h (already included in stdio.h) and is the type used for the value of sizeof().

Related:   Download and Install the GCC Compiler Code Block for C on a Window Environment

(3) ptrdiff_t is the type used to represent the difference between two pointers.

1.4 Specifier Format

specifier Usage specifier Usage
i or d int a or A Hexadecimal Floating-Point Number
u unsigned int c char
o Unsigned Octal Integer C ISO Wide Character Type (wchar_t)
x or X Unsigned Hexadecimal Integer (using uppercase ‘X’) s char* string
f or F Floating-Point Number Type (default precision of six decimal places) p Pointer (output hexadecimal address)
e or E Display in scientific notation (with a precision of six decimal places) n No characters are outputted, but the number of characters outputted up to that point is stored in the variable pointed to by the corresponding pointer (defaulting to int*).
g or G Automatically choose whether to use scientific notation or not based on the value, excluding trailing zeros. The shorter representation is preferred. % Output the character ‘%’.

2. The conversion specifiers for scanf()

%[*][width][length]specifier

 

*

and

width

Meaning
* Skip the corresponding input item. (For example, in the statement scanf("%*d %*d %d", &n);, only the third integer read will be stored in the variable n, and the preceding two values will be ignored.)
number Maximum character count. Input will stop once the maximum character count is reached or when the first whitespace character is encountered.
length d i u o x f e g a c s [] [^] p n
(none) int* unsigned int* float* char* void** int*
hh signed char* unsigned char* signed char*
h short int* unsigned short int* short int*
l long int* unsigned long int* double* wchar_t* long int*
ll long long int* unsigned long long int* long long int*
j intmax_t* uintmax_t* intmax_t*
z size_t* size_t* size_t*
t ptrdiff_t* ptrdiff_t* ptrdiff_t*
L long double*
specifier 用于
i Interpret the input as an integer, automatically detecting the base. If the input starts with “0x”, it will be interpreted as a hexadecimal number. If it starts with “0”, it will be interpreted as an octal number. If it starts with any digit from 1 to 9, it will be interpreted as a decimal number.
d or u Interpret the input as a decimal integer (d denotes signed, u denotes unsigned).
o Interpret the input as a signed octal integer.
x Interpret the input as a signed hexadecimal integer.
s String (input includes all characters from the first non-whitespace character until the next whitespace character).
p Interpret the input as a pointer (address).
a,e,f,g Interpret the input as a floating-point number (decimal or hexadecimal).
c Interpret the input as a char character (including whitespace characters).
[chatacters]  and [^characters] Scan set: Read only the characters enclosed within square brackets [], excluding the characters enclosed within [^]. Stop at the first character that does not match any character within [] (or matches a character within [^]). Finally, store the read characters as a string in the consecutive memory space pointed to by the corresponding pointer in the list.

Note: This is only applicable to character variables.

Related:   C Input Function : scanf()

Example 2.1 Printf() function specifier format

#include <stdio.h>
#include <stdlib.h>
 
int main(void){
   int a=15;
   float b=123.1234567;
   double c=12345678.1234567;
   char d='p';
   printf("a=%d\n", a);
   printf("a(%%d)=%d, a(%%5d)=%5d, a(%%o)=%o, a(%%x)=%x\n\n",a,a,a,a);        //%%可以输出%
   printf("a=%f\n", b);
   printf("b(%%f)=%f, b(%%lf)=%lf, b(%%5.4lf)=%5.4lf, b(%%e)=%e\n\n",b,b,b,b);
   printf("c=%f\n", c);
   printf("c(%%lf)=%lf, c(%%f)=%f, c(%%8.4lf)=%8.4lf\n\n",c,c,c);
   printf("d=%c\n", d);
   printf("d(%%c)=%c, d(%%8c)=%8c\n",d,d);
   system("pause");
}

 

Results:

a=15
a(%d)=15, a(%5d)= 15, a(%o)=17, a(%x)=f

a=123.123459
b(%f)=123.123459, b(%lf)=123.123459, b(%5.4lf)=123.1235, b(%e)=1.231235e+002

c=12345678.123457
c(%lf)=12345678.123457, c(%f)=12345678.123457, c(%8.4lf)=12345678.1235

d=p
d(%c)=p, d(%8c)= p

Example 2.2 printf() function output format

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    char c, s[20];
    int a=1234;
    float f=3.141592653589;
    double x=0.12345678912345678;
    strcpy(s, "Hello,World");
    c='\x41';
    printf("a=%d\n", a);        //Display in decimal integer format. a=1234
    printf("a=%d%%\n", a);      //output'%'  a=1234%
    printf("a=%6d\n", a);       //Display as a 6-digit decimal integer with left padding of spaces. a= 1234
    printf("a=%06d\n", a);      //Display as a 6-digit decimal integer with left padding of zeros. a=001234
    printf("a=%2d\n", a);       //If 'a' has more than 2 digits, display it without any specific padding. a=1234
    printf("a=%-6d\n", a);      //Display as a 6-digit decimal integer with right padding of spaces. a=1234
    printf("f=%f\n", f);        //The floating-point number will be displayed with 7 significant digits. f=3.141593
    printf("f=6.4f\n", f);      //Display in 6 columns with 4 decimal places. f=3.1416
    printf("x=%lf\n", x);       //Display as a long floating-point number. x=0.123457
    printf("x=%18.16lf\n", x);  //Display in 18 columns with 16 decimal places. x=0.1234567891234567
    printf("c=%c\n", c);        //Display a character. c=A
    printf("c=%x\n", c);        //Display the ASCII code of a character in hexadecimal format. c=41
    printf("s[]=%s\n", s);      //Display an array of characters as a string. s[]=Hello,World
    printf("s[]=%6.9s\n", s);   //Display a string with a maximum of 9 characters. s[]=Hello,Wor
    system("pause");
}

Results:

a=1234
a=1234%
a= 1234
a=001234
a=1234
a=1234
f=3.141593
f=6.4f
x=0.123457
x=0.1234567891234568
c=A
c=41
s[]=Hello,World
s[]=Hello,Wor
Press any key to continue . . .

wer

Leave a Reply