1. Home
  2. Computing & Technology
  3. C / C++ / C#

C Tutorial Lesson Two - Handling Numbers

By , About.com Guide

5 of 6

Specifying Output Formats in Printf

Printf() is a very powerful function. It can display multiple variables in one statement with a wide range of output formats. It is a variadic function taking a varying number of parameters.

The format of printf is like this:

printf("output String including 0 or more Format Specifiers", expression1,expression 2...expression n) ;

A Format Specifier is a two character symbol. The frst character is always a % and the second a letter from the list below. Here is a full list of Format Specifiers.

%c The character format specifier.
%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%e The scientific notation format specifier.
%E The scientific notation format specifier.
%g Uses %f or %e, whichever result is shorter.
%G Uses %f or %E, whichever result is shorter.
%o The unsigned octal format specifier.
%s The string format specifier.
%u The unsigned integer format specifier.
%x The unsigned hexadecimal format specifier.
%X The unsigned hexadecimal format specifier.
%p Displays the corresponding argument that is a pointer.
%n Records the number of characters written so far.
%% Outputs a percent sign.

Additionally we can add modifers to specify the width of the output fields. On the next page we'll see this and some examples.

On the Next Page : Learn more about Format specifiers.

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C
  5. C Tutorials
  6. Specifying Output Formats

©2009 About.com, a part of The New York Times Company.

All rights reserved.