Home arrow C++ arrow Page 8 - More on Handling Basic Data Types

More on Handling Basic Data Types

Have you ever wanted to learn how basic types of C++ variables interact in complex situations? Ivor Horton explains this, and also describes some interesting features of C++. This article is from chapter 3 of Ivor Horton's Beginning ANSC C++ The Complete Language (Apress, 2004; ISBN 1590592271).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
March 02, 2005
  1. · More on Handling Basic Data Types
  2. · Try It Out: Explicit Casting
  3. · Finding Out About Types
  4. · Try It Out: Finding the Sizes of Data Types
  5. · Using the Bitwise AND
  6. · Using the Bitwise Exclusive OR
  7. · Try It Out: Using the Bitwise Operators
  8. · More on Output Manipulators
  9. · Enumerated Data Types
  10. · Try It Out: Enumerated Data Types
  11. · The Lifetime of a Variable
  12. · Try It Out: The Scope Resolution Operator
  13. · Declaring External Variables

print this article

More on Handling Basic Data Types - More on Output Manipulators
(Page 8 of 13 )

Taking the last chapter into account, you’ve now seen five of the modal output manipulators that the <iostream> header defines: scientific, fixed, dec, hex, and oct. The time seems right, therefore, to list these and all the other similar manipulators in one place (see Table 3-2). Don’t worry for now about the bool values mentioned in the last two entries—they’re coming up in the next chapter.

Table 3-2. Output Manipulators

Manipulator Action Performed
dec Formats integer values as base 10 (decimal). This is the default
hex Formats integer values as base 16 (hexadecimal).
oct Formats integer values as base 8 (octal).
left Left-aligns values in the output field and pads them on the right with the
fill character. The default fill character is a space, as you’ve seen.
right Right-aligns values in the output field and pads them on the left with the
fill character. This is the default alignment.
fixed Outputs floating-point values in fixed-point notation—that is, without an
scientific Outputs floating-point values in scientific notation—that is, as the
mantissa plus an exponent. The default mode selects fixed or scientific
notation, depending on the value to be displayed.
showpoint Shows the decimal point and trailing zeros for floating-point values.
noshowpoint The opposite of the showpoint manipulator. This is the default.
showbase Prefixes octal output with 0 and hexadecimal output with 0x or 0X.
noshowbase Shows octal and hexadecimal output without the prefix. This is the
showpos Shows plus signs (+) for positive values.
noshowpos Doesn’t show plus signs for positive values. This is the default.
uppercase Displays uppercase A through F for hexadecimal digits when outputting
integers in hexadecimal format and 0X if showbase is set. Displays E for
the exponent when outputting values in scientific notation, rather than
using lowercase e.
nouppercase Uses lowercase for the preceding items. This is the default.
boolalpha Displays bool values as true and false.

Displays bool values as 1 and 0.


You may want to set more than one of these modes at a time, and one way to do this is to insert multiple manipulators into the stream. For example, if you want to output your integer data as hexadecimal values that are left aligned in the output field, you could write

 std::cout << std::hex << std::left << value;

which will output value (and all subsequent integers in the program, unless the settings are changed) as a left-justified hexadecimal number.

Table 3-3 shows the manipulators that expect you to supply an argument value.

Table 3-3. Output Manipulators That Require an Argument Value

Manipulator Action Performed
setfill() Sets the fill character as specified by the argument. The default fill
character is a space.
setw() Sets the field width as specified by the argument.
setprecision() Sets the precision for floating-point values as specified by the argument.

The precision is the number of decimal digits in the output.

This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN  1590592271). Check it out at your favorite bookstore today. Buy this book now.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials