Your first step to c/c++ programming :=-[PART 3]-=:

Go down

Your first step to c/c++ programming :=-[PART 3]-=:

Post by Admin on Sun Sep 20, 2009 5:50 pm

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

..........................................................................................
avatar
Admin
Admin

Posts : 121
Points : 24800
Reputation : 15
Join date : 2009-08-18
Age : 30
Location : algeria

View user profile http://vixt.all-up.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum