C++
  Home arrow C++ arrow Page 3 - 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 - Try It Out: Character Building


    (Page 3 of 9 )

    If you’re completely new to programming, you may be wondering how on earth the computer knows whether it’s dealing with a character or an integer. The reality is that it doesn’t. It’s a bit like in Alice’s Adventures in Wonderland when Humpty Dumpty told Alice, “When I use a word, it means just what I choose it to mean—neither more nor less.” An item of data in memory can mean whatever you choose it to mean. A byte containing the value 70 is a perfectly good integer. It’s equally correct to regard as the code for the letter J.

    Let’s look at an example that should make it clear. Here, you’ll use the conversion specifier%c, which indicates that you want to output a value of typecharas a character rather than an integer:

    /* Program 2.13 Characters and numbers */ #include <stdio.h>
    void main()
    {
      char first = 'T';
     
    char second = 20;
     
    printf("\nThe first example as a letter looks like this - %c", first);
     
    printf("\nThe first example as a number looks like this - %d", first);
     
    printf("\nThe second example as a letter looks like this - %c", second);
     
    printf("\nThe second example as a number looks like this - %d\n", second);
    }

    The output from this program is

    --------------------------------------------
    The first example as a letter looks like this - T
    The first example as a number looks like this - 84
    The second example as a letter looks like this - ¶
    The second example as a number looks like this - 20
    --------------------------------------------

    HOW IT WORKS

    The program starts off by declaring two variables of typechar:

    char first = 'T';
    char second = 20;

    One is initialized with a letter and the other with a number.

    The next four statements output each of the variables in two ways:

    printf("\nThe first example as a letter looks like this - %c", first_example); printf("\nThe first example as a number looks like this - %d", first_example); printf("\nThe second example as a letter looks like this - %c", second_example); printf("\nThe second example as a number looks like this - %d\n", second_example);

    The%cconversion specifier interprets the contents of the variable as a single character, and the%dspecifier interprets it as an integer. The numeric values that are output are the codes for the corresponding characters. These are ASCII codes in this instance, and will be in most instances, so that’s what you’ll assume throughout this book.

    As I’ve noted, not all computers use the ASCII character set, so you may get different values than those shown previously. As long as you use the notation 'character' for a character constant, you’ll get the character that you want regardless of the character coding in effect.

    You could also output the integer values of the variables of typecharas hexadecimal values by using the format specifier%xinstead of%d. You might like to try that.

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

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

    Try It Out: Arithmetic with Values of Type char

    Let’s look at another example in which you apply arithmetic operations to values of type char:

    /* Program 2.14 Using type char */
    #include <stdio.h>
    void main()
    {
      char first ='A';
      char second ='B'’;
      char last ='Z';
      char number = 40;
      char ex1 = first + 2;      /* Add 2 to 'A' */
      char ex2 = second - 1;  /* Subtract 1 from 'B' */
      char ex3 = last + 2;       /* Add 2 to 'Z' */
      printf("Character values   %-5c%-5c%-5c", ex1, ex2, ex3);
      printf("\nNumerical equivalents %-5d%-5d%-5d", ex1, ex2, ex3);
      printf("\nThe number %d is the code for the character %c\n", number, number);
    }

    When you run the program you should get the following output:

    --------------------------------------------
    Character values       C  A  \
    Numerical equivalents 67 65 92
    The number 40 is the code for the character (
    --------------------------------------------

    HOW IT WORKS

    This program demonstrates how you can happily perform arithmetic withcharvariables that you’ve initialized with characters. The first three statements in the body ofmain()are as follows:

    char first = 'A';
    char second = 'B';
    char last = 'Z';

    These initialize the variablesfirst,second, andlast to the character values you see. The numerical value of these variables will be the ASCII codes for the respective characters. Because you can treat them as numeric, as well as characters, you can perform arithmetic operations with them.

    The next statement initializes a variable of typechar with an integer value:

    char number = 40;

    The initializing value must be within the range of values that a 1-byte variable can store, so with my compiler wherecharis a signed type it must be between –128 and 127. Of course, you can interpret the contents of the variable as a character. In this case, it will be the character that has the ASCII code value 40, which happens to be a left parenthesis.

    The next three statements declare three more variables of typechar:

    char ex1 = first + 2;   /* Add 2 to 'A'  */
    char ex2 = second - 1;  /* Subtract 1 from 'B' */
    char ex3 = last + 2;    /* Add 2 to 'Z'  */

    These statements create new values and therefore new characters from the values stored in the variablesfirst,second, andlast; the results of these expressions are stored in the variablesex1,ex2, andex3.

    The next two statements output the three variablesex1,ex2, andex3in two different ways:

    printf("Character values      %-5c%-5c%-5c", ex1, ex2, ex3);
    printf("\nNumerical equivalents %-5d%-5d%-5d", ex1, ex2, ex3);

    The first statement interprets the values stored as characters by using the%-5cconversion specifier. This specifies that the value should be output as a      character left-aligned in a field width of 5. The second statement outputs the same variables again, but this time interprets the values as integers by using the    %-5dspecifier. The alignment and the field width are the same butdspecifies the output is an integer. You can see that the two lines of output show the three characters with their ASCII codes aligned below on the next line.

    The last line outputs the variablenumber as a character and as an integer:

    printf("\nThe number %d is the code for the character %c\n", number, number);

    To output the variable twice, you just write it twice—as the second and third arguments to theprintf()function. It’s output first as an integer value and then as a character.

    This ability to perform arithmetic with characters can be very useful. For instance, to convert from uppercase to lowercase, you simply add the result of'a'-'A'(which is 32 for ASCII) to the uppercase character. To achieve the reverse, just subtract'a'-'A'. You can see how this works if you have a look at the decimal ASCII values for the alphabetic characters in Appendix B.

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

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