Home arrow C++ arrow Page 3 - 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 - Finding Out About Types
(Page 3 of 13 )

Iíve mentioned several times that the number of bytes used for some types isnít specified in the C++ standard and that this is therefore set by your compiler. Itís quite possible that you would want to know how many bytes particular types of variables will occupy in your compiler. You could hunt for this information in your compilerís documentation, but you can also get the information programmatically by using the sizeof operator.

sizeof is a unary operator, so it takes a single operand. It will return an integer value that is a measure of the amount of memory occupied by a particular variable, or by a type. The value returned by sizeof is actually defined as a multiple of the size of type char, but because variables of type char occupy 1 byte, the value returned will be a measure of the number of bytes that the operand occupies.

To obtain the number of bytes occupied by variables of type type, you use the expression sizeof(type). You could therefore output the size of variables of type int with this statement:

 std::cout << std::endl
               << "Size of type int is "
               << sizeof(int);             // Output the  
                                     size of type int

The expression sizeof(int) returns the size of anything declared as type int, and you can find out the size of any data type in this way. To get the size of long double values, you could write this:

 std::cout << std::endl
           << "Size of type long double is "
           << sizeof(long double);  //Output the size of                  type long double            

You can also apply the sizeof operator to a particular variable, or even to an expression. In this case, the expression doesnít have to be between parentheses, although you can include them if you wish. Hereís an example that will output the number of bytes occupied by the variable number:

 long number = 999999999;
 std::cout << std::endl
           << "Size of the variable number is "
           << sizeof number;         // Output the size of
                               a variable

You can treat the value returned by the sizeof operator as an integer, but in fact itís of type size_t. This isnít really a new type, though. The word size_t is defined in the standard header as a synonym for one of the fundamental integer types, usually as unsigned int. Because itís a synonym and not a name for an entity in the standard library, the name is not within the std namespace, so you can use it as is. I can hear your next question already: ďWhatís the point of having a different type name here? Why not just make it unsigned int?Ē

The reason for specifying the type returned by the sizeof operator is that it builds in flexibility. The sizeof operator always returns a value of type size_t, which your compiler may well define to be unsigned int, but it doesnít have to be so. It might be convenient for the developers of a C++ compiler for a particular hardware platform to define size_t to be equivalent to some other integral type, and theyíre free to do so. It wonít affect your code, because your assumption is that its type is size_t. Youíll see how to define a synonym for an existing type for yourself later in this chapter. Incidentally, the t in size_t stands for ďtype,Ē so the name was chosen to indicate that it is a type for a size.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials