Home arrow C++ arrow Page 3 - 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 - 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.

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


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