Technical Training
C TutorialsTable of Contents
C Programming - An Overview
C Programming - An Overview - Page 2
C Programming - An Overview - Page 3
C Programming - An Overview - Page 4
C Programming - An Overview - Page 5C Programming - An Overview Page - 5
C Programming - An Overview
Printing
In this section we will take a more detailed look at the printf() function that you have already seen used in several examples. It is one of the main ways to display output in a C program.
The printf() function is unusual in that it can take a variable number and types of arguments. The first argument to printf() is the format string. This is always required. It tells printf() what to print and how to print it. The second and further arguments are dependent on the format string.
The format string can contain conversion specifications. These are sequences of characters beginning with a % that tell printf() what to convert and how to print it. For example, the "%d" conversion specification tells printf() to convert an int argument to a string representing its decimal value. Each conversion specification applies to the next argument to printf(). Let us see how this works:
- #include <stdio.h>
- void main()
- {
- printf("second printf argument is %d, third is %dn", 12, 34 );
- }
The output of this program is: second printf argument is 12, third is 34
As you can see, the first %d used the second argument to printf() and the second %d used the third argument. Both of those arguments are integer constants that are converted to ints, then passed to printf(). The printf() function has many conversion specifiers. Here are some of them, what arguments they expect and how they appear when printed:
Specifier Expected type Printed as
- d, i int decimal number
- o unsigned int octal number
- u unsigned int decimal number
- x, X unsigned int hexadecimal number
- e, E double [-]d.dddeądd where d is a decimal digit and [-] means a minus sign
- is printed if the value is negative.
- f, F double [-]ddd.ddd
- g, G double esentially acts as a f or e specifier, whichever is more compact.
- c char a single character
- s const char * a string
- % none prints a single % character.
With each conversion specifier you can also give a width, a precision and a justification. These are specified as a numeric width followed by a period, then by a numeric precision, between the % and the conversion specifier. For example "%20.3f" specifies a width of 20 and a precision of 3 for a double argument.
The width gives the minimum number of characters to print (but does not specify the maximum) when converting. You can put a '-' character before the width to left justify the output; otherwise it is right justified. The precision value is used in different ways for different specifiers. For the %f and %e specifiers it gives the number of places after the decimal point. For %d the precision gives the number of digits to print (it is padded with zeroes if necessary) and for %s is gives the maximum number of characters to print.
Let us look at some examples. This program prints different types of variables with a field width, precision and justification:
- #include <stdio.h>
- void main()
- {
- char c = 'x';
- int i = 1234;
- double d = 4.982761036e+3;
- char *s = "This is a character string";
- /* default */
- printf("Defaultn");
- printf("|1234567890123456789012345678901234567890|n");
- printf("|%c|n", c );
- printf("|%d|n", i );
- printf("|%f|n", d );
- printf("|%s|n", s );
- /* right justified */
- printf("nRight Justifiedn");
- printf("|1234567890123456789012345678901234567890|n");
- printf("|%20c|n", c );
- printf("|%20d|n", i );
- printf("|%20f|n", d );
- printf("|%20s|n", s );
- /* left justified */
- printf("nLeft Justifiedn");
- printf("|1234567890123456789012345678901234567890|n");
- printf("|%-20c|n", c );
- printf("|%-20f|n", d );
- /* precision without width */
- printf("nPrecision without widthn");
- printf("|1234567890123456789012345678901234567890|n");
- printf("|%.5d|n", i );
- printf("|%.5f|n", d );
- printf("|%.5s|n", s );
- /* both width and precision */
- printf("nPrecision and widthn");
- printf("|1234567890123456789012345678901234567890|n");
- printf("|%20.5d|n", i );
- printf("|%20.5f|n", d );
- printf("|%20.5s|n", s );
- }
The output of this program is:
- Default
- |1234567890123456789012345678901234567890|
- |x|
- |1234|
- |4982.761036|
- |This is a character string|
- Right Justified
- |1234567890123456789012345678901234567890|
- | x|
- | 1234|
- | 4982.761036|
- |This is a character string|
- Left Justified
- |1234567890123456789012345678901234567890|
- |x |
- |4982.761036 |
- Precision without width
- |1234567890123456789012345678901234567890|
- |01234|
- |4982.76104|
- |This |
- Precision and width
- |1234567890123456789012345678901234567890|
- | 01234|
- | 4982.76104|
- | This |
Lines 12-17 print the character, integer, real number and string with the default formatting. Lines 20-25 print them right justified in a field 20 characters wide. Since the width only gives the minimum width, the string which is 26 characters long overflows the field. Lines 28-31 print the character and real number left justified in a field 20 characters wide.
Lines 34-38 print the integer, real number and the string with a precision of 5 specified. For the integer you can see that a 0 has been added to the number. The real number's fractional part .761036 is rounded to .76104 to give it 5 digits after the decimal point. And for the string the precision gives the number of characters to print, which results in "This ".
Lines 41 to 45 specify both width and precision.
The options and capabilities of printf() could take up an entire tutorial in itself. We have only discussed a few of its features. For more info consult your C compiler documentation.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







