Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    C

    Format output using printf

    RyanBy RyanOctober 4, 2008Updated:October 9, 20084 Mins Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    printf and its family of functions allow you to easily format string data. There are so many formatting options that it is easy to miss some of its more powerful functionality. Here is a rundown of the more useful features and some examples.

    The basic printf format.

    printf(format string, format parameter list);
    

    The format string is composed of a literal string that may include one or more conversion specifiers. These all begin with ‘%’ and have the format below.

    %[flags][minimum field width][precision/max field width][length]conversion specifier
    

    flags

    # The value should be converted to an ”alternate form”. For o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively. For e, E and f, it forces the written output to contain a decimal point. For g or G the result is the same as with e or E but trailing zeros are not removed.
    0 The value should be zero padded.
    – The converted value is to be left justified.
    ‘ ‘ (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.
    + A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers.
    ‘ Thousands grouping.

    minimum field width

    A minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). If the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

    precision/max field width

    An optional precision, in the form of a period ‘.’ followed by a number which represents the maximum number of significant digits, or the maximum number of characters to be printed from a string.

    length

    hh char
    h short
    l long
    ll long long
    L long double
    j intmax_t, uintmax_t
    z size_t, ssize_t
    t ptrdiff_t

    conversion specifier

    d,i The int argument is converted to signed decimal notation.
    u An unsigned int.
    o,x,X An unsigned octal (o), or unsigned hexadecimal (x and X) notation.
    e,E The double [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision. if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent.
    f,F The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd.
    c An unsigned char.
    s A pointer to an array of character type (pointer to a string). The array must contain a terminating null.
    p The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx).
    % A ‘%’ is written. No argument is converted. The complete conversion specification is ‘%%’.

    Examples

    printf("%c \n", 'a');
    a
    
    printf("%d \n", 100);
    100
    
    printf("%ld \n", 5000000000);
    5000000000 
    
    printf("%06d \n", 100);
    000100 
    
    printf("%6d \n", 100);
       100 
    
    printf("%s \n", "14 char string");
    14 char string
    
    printf("%.10s \n", "14 char string");
    14 char st
    
    char * ptr = "string";
    printf("%p \n", ptr);
    0x400788
    
    printf("%X \n", 100);
    64
    
    printf("%#X \n", 100);
    0X64
    
    printf("%o \n", 100);
    144
    
    printf("%#o \n", 100);
    0144
    
    printf("%#x \n", 100);
    0x64
    
    printf("%f \n", 3.14159);
    3.141590
    
    printf("%e \n", 3.14159);
    3.141590e+00
    
    printf("%E \n", 3.14159);
    3.141590E+00
    
    printf("%6d %d \n", 100, 100);
       100 100
    
    printf("%-6d %d \n", 100, 100);
    100    100
    

    The entire family of printf functions including fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, and vsnprintf take advantage of these formatting rules as well. In addition, many other languages have printf implementations which operate in much the same way.

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleSubmit commands as root with sudo
    Next Article View program output with watch

    Related Posts

    Command substitution in BASH

    Find controlling terminal with ttyname

    Find IP address from remote end of a TCP socket

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.