Home arrow C++ arrow Page 4 - First Steps in (C) Programming, conclusion
C++

First Steps in (C) Programming, conclusion


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 third 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 / 63
December 01, 2005
TABLE OF CONTENTS:
  1. · First Steps in (C) Programming, conclusion
  2. · More Numeric Data Types
  3. · Try It Out: Character Building
  4. · Unsigned Integers: Using Positive Integers
  5. · Try It Out: Finding the Limits
  6. · Mathematical Functions
  7. · Designing a Program
  8. · The Solution
  9. · Summary

print this article
SEARCH DEVARTICLES

First Steps in (C) Programming, conclusion - Unsigned Integers: Using Positive Integers
(Page 4 of 9 )

If you’re sure that you’ll be dealing with just positive integers, there’s a way of increasing the maximum value that you can use. Theunsigned modifier can be used with all of the basic integer types that I’ve covered, to define types that store only positive integers. This allows you to handle integers roughly twice as large as you otherwise might. You use theunsignedmodifier as follows:

unsigned char a_value;     /* Can be from 0 to +255    */
unsigned int number;       /* Can be from 0 to +65,535 */
unsigned long big_number;  /* Can be from 0 to 4,294,967,295 */

You must be absolutely sure that you need only positive values if you want to use these. Unsigned variables can’t store negative values at all.

When you want to specify an integer constant as unsigned, you append the letterUas upper- or lowercase to the numeric value. For example, you could write

number = 25U;

This sets the unsigned variablenumberof typeintto 25.

If you want to define a constant that is of typeunsigned long, you need a Uand an Las a suffix to the numeric value. You can specify the letters in upper- or lowercase, for example:

big_number = 100000000UL;

This stores the value 100,000,000 in the variablebig_number.

You use the format specifier%uto output an unsigned integer. You can also include a field width specification and a-for left alignment in the field if you wish.

For example, you could output the value of the unsigned integer variable callednumberwith this statement:

printf("The value of number is %-12u", number);

The field width is 12 here and the value will be left-aligned in the field.

I’ve now covered all the numeric data types in C; Table 2-6 presents all of them.

Table 2-6.Numeric Data Types

Variable Type

Keyword

Number of Bytes

Range of Values

Character

char

1

–128 to +127 or 0 to 255

Unsigned character

unsigned char

1

0 to 255

Integer

int

2 or 4

–32,768 to +32,767 or –2,147,438,648 to +2,147,438,647

Unsigned integer

unsigned int

2 or 4

0 to 65,535 or 0 to 4,294,967,295

Short integer

short

2

–32,768 to +32,767

Unsigned short integer

unsigned short

2

0 to 65,535

Long integer

long

4

–2,147,438,648 to +2,147,438,647

Unsigned long integer

unsigned long

4

0 to 4,294,967,295

Single precision floating-point

float

4

±3.4E38 (6 digits)

Double precision floating-point

double

8

±1.7E308 (15 digits)

Extended double precision floating-point

long double

10

±1.2E4932 (19 digits)

Finding the Limits

I mentioned earlier and in various other places in this chapter how the limits for the range of values for some of these numerical types can vary from one system and/or compiler to another. This is obviously going to be a problem in some situations in which you really need to be able to determine within your program what the limits are in a particular instance. The C standard library includes header files that can provide you with this information. Thelimits.hheader file provides information about integer types and thefloat.hheader file does the same for floating-point types.

Thelimits.hheader file contains definitions for symbols (not variables) such that define the upper and lower limits for each signed integer type. For instance,INT_MINandINT_MAXare the symbols representing the upper and lower limits for typeint, andCHAR_MINandCHAR_MAXrepresent the limits for typechar. For unsigned types only symbols for upper limits are defined because the lower limit is always zero. There are symbols with similarly formed names for each of the integer types in Table 2-6.

In thefloat.hheader are symbols with the namesFLT_MINandFLT_MAXthat represent the minimum and maximum positive values of typefloat. There are similar symbols such as DBL_MIN,DBL_MAX,LDBL_MIN, andLDBL_MAXfor thedoubleandlong doubletypes. If you look in the documentation for the library that comes with your compiler, you’ll find many other symbols that represent values for other parameters relating to the implementation of the floating-point types on your system.

Let’s see how you can access some of these with an example.

..........................................................................................


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