Home arrow C++ arrow Page 3 - First Steps in (C) Programming, continued
C++

First Steps in (C) Programming, continued


If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the second of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 22
November 23, 2005
TABLE OF CONTENTS:
  1. · First Steps in (C) Programming, continued
  2. · Variables and Memory
  3. · Integer Constants
  4. · Floating-Point Variables
  5. · More on Format Specifiers
  6. · More Complex Expressions
  7. · Defining Constants
  8. · Try It Out: The Right Types of Variables

print this article
SEARCH DEVARTICLES

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?


blog comments powered by Disqus
C++ ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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