eHow launches Android app: Get the best of eHow on the go.

How To

How to Use the Printf Command in C++

Contributor
By eHow Contributing Writer
(4 Ratings)

The printf function in C++ writes data to the standard output which is the monitor by default. This command takes a format as a parameter followed by as many format specifiers as are required by the format. There are many format specifiers, so this article will concentrate on the basic uses of printf.

Difficulty: Challenging
Instructions
  1. Step 1

    Understand that the printf function in C++ is kept in the cstdio library. You may need to include the stdio.h header file to use this function.

  2. Step 2

    Learn the syntax of printf. The complete syntax is int printf ( const char * format, ... ). This function takes character pointers as arguments and returns the number of characters written if the command is successful. Otherwise, printf returns a negative number.

  3. Step 3

    Know that the format may contain format tags using the following prototype: %[flags][width][.precision][length]specifier. Fields that are enclosed in brackets are optional. Note that the specifier is the only required component of the tag. The specifier must be one of the following: c (character); d or i (signed decimal integer);
    e or E (Scientific notation using e or E); f (decimal floating point); g or G (use the shorter of %e/%E or %f); o (signed octal integer); s (character string); u (unsigned decimal integer); x (unsigned hexadecimal integer using lowercase letters); X (unsigned hexadecimal integer using uppercase letters); p (pointer); n (nothing printed)

  4. Step 4

    Look at the following complete program for some simple examples of how to use printf:

    #include

    int main()
    {
    printf ("This format contains no specifiers.\n");
    printf ("This format uses some characters: %c, %c, %c\n", 'a', 'b', 'c');
    printf ("This format uses some decimals: %d, %d, %d\n", 1, 2, 3);
    printf ("This format uses a string: %s, %s\n", "first string", "second string");
    printf ("We will not use a new line character");
    printf ("to print this line.");
    return 0;
    }

    This program will give the following output:

    This format contains no specifiers.
    This format uses some characters: a, b, c
    This format uses some decimals: 1, 2, 3
    This format uses a string: first string, second string.
    We will not use a new line character to print this line.



  5. Step 5

    Note how the new line character ('\n') causes printf to output a new line. Otherwise, the next printf will output to the same line.

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics