Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - An Overview Page - 5

Page 5 of 5
Author: Exforsys Inc.     Published on: 2nd Mar 2006    |   Last Updated on: 29th Jul 2011

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.

Ads

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:

Sample Code
  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. printf("second printf argument is %d, third is %dn", 12, 34 );
  6. }
Copyright exforsys.com


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

Sample Code
  1. d, i int decimal number
  2. o unsigned int octal number
  3. u unsigned int decimal number
  4. x, X unsigned int hexadecimal number
  5. e, E double [-]d.dddeądd where d is a decimal digit and [-] means a minus sign
  6. is printed if the value is negative.
  7. f, F double [-]ddd.ddd
  8. g, G double esentially acts as a f or e specifier, whichever is more compact.
  9. c char a single character
  10. s const char * a string
  11. % none prints a single % character.
Copyright exforsys.com


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:

Sample Code
  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. char c = 'x';
  6. int i = 1234;
  7. double d = 4.982761036e+3;
  8. char *s = "This is a character string";
  9.  
  10.  
  11. /* default */
  12. printf("Defaultn");
  13. printf("|1234567890123456789012345678901234567890|n");
  14. printf("|%c|n", c );
  15. printf("|%d|n", i );
  16. printf("|%f|n", d );
  17. printf("|%s|n", s );
  18.  
  19. /* right justified */
  20. printf("nRight Justifiedn");
  21. printf("|1234567890123456789012345678901234567890|n");
  22. printf("|%20c|n", c );
  23. printf("|%20d|n", i );
  24. printf("|%20f|n", d );
  25. printf("|%20s|n", s );
  26.  
  27. /* left justified */
  28. printf("nLeft Justifiedn");
  29. printf("|1234567890123456789012345678901234567890|n");
  30. printf("|%-20c|n", c );
  31. printf("|%-20f|n", d );
  32.  
  33. /* precision without width */
  34. printf("nPrecision without widthn");
  35. printf("|1234567890123456789012345678901234567890|n");
  36. printf("|%.5d|n", i );
  37. printf("|%.5f|n", d );
  38. printf("|%.5s|n", s );
  39.  
  40. /* both width and precision */
  41. printf("nPrecision and widthn");
  42. printf("|1234567890123456789012345678901234567890|n");
  43. printf("|%20.5d|n", i );
  44. printf("|%20.5f|n", d );
  45. printf("|%20.5s|n", s );
  46. }
Copyright exforsys.com


The output of this program is:

Sample Code
  1. Default
  2. |1234567890123456789012345678901234567890|
  3. |x|
  4. |1234|
  5. |4982.761036|
  6. |This is a character string|
  7.  
  8. Right Justified
  9. |1234567890123456789012345678901234567890|
  10. | x|
  11. | 1234|
  12. | 4982.761036|
  13. |This is a character string|
  14.  
  15. Left Justified
  16. |1234567890123456789012345678901234567890|
  17. |x |
  18. |4982.761036 |
  19.  
  20. Precision without width
  21. |1234567890123456789012345678901234567890|
  22. |01234|
  23. |4982.76104|
  24. |This |
  25.  
  26. Precision and width
  27. |1234567890123456789012345678901234567890|
  28. | 01234|
  29. | 4982.76104|
  30. | This |
Copyright exforsys.com


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.

Ads

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.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments