C++
  Home arrow C++ arrow Page 2 - 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  
Moblin 
JMSL Numerical Library 
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 / 53
    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 - More Numeric Data Types


    (Page 2 of 9 )

    To complete the set of numeric data types, I’ll now cover those that I haven’t yet discussed. The first is one that I mentioned previously: type char. A variable of type char can store the code for a single character. Because it stores a character code, which is an integer, it’s considered to be an integer type. Because it’s an integer type, the value stored can be treated just as an integer and can participate in arithmetic calculations.

    The Character type

    Values of typecharoccupy the least amount of memory of all the data types. They require just 1 byte. The integer that’s stored in a variable of typecharcan be interpreted as a signed or unsigned value, depending on your compiler. An unsigned integer type is simply one that allows negative as well as positive integral values to be stored. As an unsigned type, the value stored in a variable of typechar can range from 0 to 255. As a signed type, a variable of typecharcan store values from –128 to +127. Of course, both ranges correspond to the same set of bit patterns: from 0000 0000 to 1111 1111. With unsigned values, all 8 bits are data bits, so 0000 0000 corresponds to 0 and 1111 1111 corresponds to 255. With unsigned values, the leftmost bit is a sign bit, so –128 is the binary value 1000 0000, 0 is 0000 0000, and 127 is 0111 1111. The value 1111 1111 as a signed binary value is the decimal value –1.

    Thus, from the point of view of representing character codes, which are bit patterns, it doesn’t matter whether typecharis regarded as signed or unsigned. Where it does matter is when you perform arithmetic operations on values of typechar.

    Acharvariable can hold any single character, so you can specify the initial value for a variable of typecharby a character constant. A character constant is a character written between single quotes. Here are some examples:

    char letter = 'A';
    char digit = '9';
    char exclamation = '!';

    You can use escape sequences to specify character constants too:

    char newline = '\n';
    char tab = '\t';
    char single_quote = '\'';

    Of course, in every case the variable will be set to the code for the character between single quotes. The actual code value will depend on your computer environment, but by far the most common is American Standard Code for Information Interchange (ASCII). You can find the ASCII character set in Appendix B.

    You can also initialize a variable of typecharwith an integer value, as long as the value fits into the range for typecharwith your compiler, for example:

    char character = 74;   /* ASCII code for the letter J */

    A variable of typecharhas a sort of dual personality: you can interpret it as a character or as an integer. Here’s an example of an arithmetic operation with a value of typechar:

    char letter = 'C';    /* letter contains the decimal code value 67 */
    letter = letter +3;   /* letter now contains 70, which is 'F'   */

    Thus, you can perform arithmetic on a value of typecharand still treat it as a character.

    Character Input and Character Output

    You can read a single character from the keyboard and store it in a variable of typecharusing thescanf()function with the format specifier%c:

    char ch = 0;
    scanf("%c", &ch); /* Read one character */

    To write a single character to the command line with theprintf()function, you use the same format specifier,%c:

    printf("The character is %c", ch);

    Of course, you can output the numeric value of a character too:

     printf("The character is %c and the code value is %d", ch, ch);

    This statement will outputchas a character and as a numeric value.

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

    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

    - 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
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






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