Format Specifiers in C Programming Language

Rumman Ansari   Software Engineer   2022-08-18   13580 Share
☰ Table of Contents

Table of Content:


The % Format Specifiers

Format specifiers can be defined as the operators which are used in association with printf() function for printing the data that is referred by any object or any variable. When a value is stored in a particular variable, then you cannot print the value stored in the variable straightforwardly without using the format specifiers. You can retrieve the data that are stored in the variables and can print them onto the console screen by implementing these format specifiers in a printf() function.

Format specifiers start with a percentage % operator and followed by a special character for identifying the type of the data.

There are mostly six types of format specifiers that are available in C.

Format specifier Description
%d Integer Format Specifier
%f Float Format Specifier
%c Character Format Specifier
%s String Format Specifier
%u Unsigned Integer Format Specifier
%ld Long Int Format Specifier

The % specifiers that you can use in ANSI C are:

Usual variable type Display

%c char single character

%d (%i) int signed integer

%e (%E) float or double exponential format

%f float or double signed decimal

%g (%G) float or double use %f or %e as required

%o int unsigned octal value

%p pointer address stored in a pointer

%s array of char sequence of characters

%u int unsigned decimal

%x (%X) int unsigned hex value

Formatting Your Output

The type conversion specifier only does what you ask of it - it convert a given bit pattern into a sequence of characters that a human can read. If you want to format the characters then you need to know a little more about the printf function's control string.

Each specifier can be preceded by a modifier which determines how the value will be printed. The most general modifier is of the form:

flag width.precision

The flag can be any of:

flag meaning

- left justify

+ always display sign

space display space if there is no sign

0 pad with leading zeros

# use alternate form of specifier

The width specifies the number of characters used in total to display the value and precision indicates the number of characters used after the decimal point.

For example, %10.3f will display the float using ten characters with three digits after the decimal point. Notice that the ten characters includes the decimal point, and a - sign if there is one. If the value needs more space than the width specifies then the additional space is used - width specifies the smallest space that will be used to display the value. (This is quiet reassuring, you won't be the first programmer whose program takes hours to run but the output results can't be viewed because the wrong format width has been specified!)

The specifier %-1Od will display an int left justified in a ten character space. The specifier %+5d will display an int using the next five character locations and will add a + or - sign to the value.

The only complexity is the use of the # modifier. What this does depends on which type of format it is used with:

%#o adds a leading 0 to the octal value

%#x adds a leading 0x to the hex value

%#f or

%#e ensures decimal point is printed

%#g displays trailing zeros

Strings will be discussed later but for now remember: if you print a string using the %s specifier then all of the characters stored in the array up to the first null will be printed. If you use a width specifier then the string will be right justified within the space. If you include a precision specifier then only that number of characters will be printed.

For example:

printf("%s,Hello")

will print Hello,

printf("%25s ,Hello")

will print 25 characters with Hello right justified and

printf("%25.3s,Hello")

will print Hello right justified in a group of 25 spaces.

Also notice that it is fine to pass a constant value to printf as in printf("%s,Hello").

Finally there are the control codes:

\b backspace

\f formfeed

\n new line

\r carriage return

\t horizontal tab

\' single quote

\0 null

If you include any of these in the control string then the corresponding ASCII control code is sent to the screen, or output device, which should produce the effect listed. In most cases you only need to remember \n for new line.

scanf

Now that we have mastered the intricacies of printf you should find scanf very easy. The scanf function works in much the same way as the printf. That is it has the general form:

scanf(control string,variable,variable,...)

In this case the control string specifies how strings of characters, usually typed on the keyboard, should be converted into values and stored in the listed variables. However there are a number of important differences as well as similarities between scanf and printf.

The most obvious is that scanf has to change the values stored in the parts of computers memory that is associated with parameters (variables).

To understand this fully you will have to wait until we have covered functions in more detail. But, just for now, bare with us when we say to do this the scanf function has to have the addresses of the variables rather than just their values. This means that simple variables have to be passed with a preceding &&&&&(Note for future reference: There is no need to do this for strings stored in arrays because the array name is already a pointer.)

The second difference is that the control string has some extra items to cope with the problems of reading data in. However, all of the conversion specifiers listed in connection with printf can be used with scanf.

The rule is that scanf processes the control string from left to right and each time it reaches a specifier it tries to interpret what has been typed as a value. If you input multiple values then these are assumed to be separated by white space - i.e. spaces, newline or tabs. This means you can type:

3 4 5

or

3

4

5

and it doesn't matter how many spaces are included between items. For example:

scanf("%d %d",&i,&j);

will read in two integer values into i and j. The integer values can be typed on the same line or on different lines as long as there is at least one white space character between them.

The only exception to this rule is the %c specifier which always reads in the next character typed no matter what it is. You can also use a width modifier in scanf. In this case its effect is to limit the number of characters accepted to the width.

For example:

scanf("%lOd",&i)

would use at most the first ten digits typed as the new value for i.

There is one main problem with scanf function which can make it unreliable in certain cases. The reason being is that scanf tends to ignore white spaces, i.e. the space character. If you require your input to contain spaces this can cause a problem. Therefore for string data input the function getstr() may well be more reliable as it records spaces in the input text and treats them as an ordinary characters.