C++
  Home arrow C++ arrow Page 3 - More on Handling Basic Data Types
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

More on Handling Basic Data Types
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-03-02

    Table of Contents:
  • More on Handling Basic Data Types
  • Try It Out: Explicit Casting
  • Finding Out About Types
  • Try It Out: Finding the Sizes of Data Types
  • Using the Bitwise AND
  • Using the Bitwise Exclusive OR
  • Try It Out: Using the Bitwise Operators
  • More on Output Manipulators
  • Enumerated Data Types
  • Try It Out: Enumerated Data Types
  • The Lifetime of a Variable
  • Try It Out: The Scope Resolution Operator
  • Declaring External Variables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Apress Publishing


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT