C++
  Home arrow C++ arrow Page 3 - First Steps in (C) Programming, continued
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++

First Steps in (C) Programming, continued
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-11-23

    Table of Contents:
  • First Steps in (C) Programming, continued
  • Variables and Memory
  • Integer Constants
  • Floating-Point Variables
  • More on Format Specifiers
  • More Complex Expressions
  • Defining Constants
  • Try It Out: The Right Types of 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


    First Steps in (C) Programming, continued - Integer Constants


    (Page 3 of 8 )

    Because you can have different kinds of integer variables, you might expect to have different kinds of integer constants, and you do. If you just write an integer value 100, for example, this will be of typeint. If you want to make sure it is typelong, you must append an upper- or lowercase letterLto the numeric value. So the integer 100 as alongvalue is written100L. Although it’s perfectly legal, a lowercase letter lis best avoided because it’s easily confused with the digit 1.

    To declare and initialize the variableBig_Number, you could write this:

    long Big_Number = 1287600L;

    An integer value will also be typelongif it’s outside the range of typeint. Thus, if your compiler implementation uses 2 bytes to store typeintvalues, the values1000000and33000will be of typelongby default, because they won’t fit into 2 bytes.

    You write negative integer constants with a minus sign, for example:

    int decrease = -4;
    long below_sea_level = -100000L;

    You can also write integer values in hexadecimal form—that is, to base 16. The digits in a hexadecimal number are the equivalent of decimal values 0 to 15, and they’re represented by 0 through 9 andAthroughF(orathroughf). Because there needs to be a way to distinguish between 9910and 9916, hexadecimal numbers are written with the prefix0x or0X. You would therefore write 9916in your program as0x99or as0X99.

    Hexadecimal constants are most often used to specify bit patterns because each hexadecimal digit corresponds to 4 binary bits. The bitwise operators that you’ll see in the next chapter are usually used with hexadecimal constants that define masks. If you’re unfamiliar with hexadecimal numbers, you can find a detailed discussion of them in Appendix A.

    Floating-Point Values

    Floating-point variables are used to store floating-point numbers. Floating-point numbers hold values that are written with a decimal point, so you can represent fractional as well as integral values. The following are examples of floating-point values:

    1.6
    0.00008
    7655.899

    Because of the way floating-point numbers are represented, they hold only a fixed number of decimal digits; however, they can represent a very wide range of values—much wider than integer types. Floating-point numbers are often expressed as a decimal value multiplied by some power of 10. For example, each of the previous examples of floating-point numbers could be expressed as shown in Table 2-4.

    Table 2-4. Expressing Floating-Point Numbers

    Value 1.6

    With an Exponent 0.16✕101

    Can Also Be Written in C As 0.16E1

    0.00008

    0.8✕10-4

    0.8E4

    7655.899

    0.7655899✕104

    0.7655899E4

    The center column shows how the numbers in the left column could be represented with an exponent. This isn’t how you write them in C; it’s just an alternative way of representing the same value designed to link to the right column. The right column shows how the representation in the center column would be expressed in C. TheEin each of the numbers is for exponent, and you could equally well use a lowercasee. Of course, you can write each of these numbers in your program without an exponent, just as they appear in the left column, but for very large or very small numbers, the exponent form is useful. I’m sure you’d rather write0.5E-15than0.0000000000000005, wouldn’t you?

    More C++ Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning C", published by Apress. We hope...
     

    Buy this book now. This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out at your favorite bookstore today. Buy this book now.

    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