C++
  Home arrow C++ arrow Page 4 - First Steps in (C) Programming, conclusion
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, conclusion
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 54
    2005-12-01

    Table of Contents:
  • First Steps in (C) Programming, conclusion
  • More Numeric Data Types
  • Try It Out: Character Building
  • Unsigned Integers: Using Positive Integers
  • Try It Out: Finding the Limits
  • Mathematical Functions
  • Designing a Program
  • The Solution
  • Summary

  • 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, 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.

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

    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. 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 5 hosted by Hostway
    Stay green...Green IT