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

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


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