MESSAGE
DATE | 2010-02-15 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
Data Representation in Detail:
chars: Characters are the most basic data type in C and C++ being stored in memory in a single signed or unsigned byte.
chars can be represented in several ways, and internally are represented as small ints, either signed or unsigned. We can create character constants by putting keyboard characters into single quotes ==> 'B' and assigned them to either char, unsigned char, or pointer indirectly by addressing to char variable or some other memory construction that stores a char or unsigned char.
Characters can be represented in programming code, not only with the
char var = 'X';
syntax, but also by Integer, Octal Code and Hexadecimal code which represent ASCII mapped characters. The syntax for these codes includes the following:
Integer constants: (note the single quotes)
char letter = 65; //stores an ASCII A. char letterOct = '\102'; char letterHex = '\x43'; char letterOct = 0102; char letterHex = 0x43;
Octals have the generic form of 3 digits preceded by a backslash if quoted, but the integer syntax can be used. And integer that starts with a 0 is interpreted as non-decimal. Remember that you feel more comfortable with decimal numbers, but the machine couldn't care less. If the 0 is followed by an x or X, then it is hexadecimal. If you put it in single quotes, it needs a backslash first.
Their is also a short hand for special characters that can not be readily typed from a US 105 key keyboard. These are also backslashed. I'll show a complete list in the code example, but the most important two, by far, are '\n', the line feed, and '\t' the horizontal tab. The '\n' can also be represented in C++ with the endl symbol (which stands for end of line). I'm not going to discuss the broken MS end of line.
A character literal can be also have an 'L' in front of it to use for double wide characters, such as in Chinese etc. It has to be stored in an appropriate variable called a wchar_t.
You can't use any of these literal representations on the left side of an assignment operator. For some, this might seem obvious that you can do this assignment:
'0' = 'G';
but trust me, some day you will do just this for some twisted reason.
Here is a sample program to show all the character variations:
Three Files: First the Header File
http://www.nylxs.com/docs/workshops/cpp/data.h.html http://www.nylxs.com/docs/workshops/cpp/data.h
1 #ifndef DATA_H 2 #define DATA_H 3 #endif /* DATA_H */ 4 5 void show_chars(); 6 void show_ints(); 7 void show_floats(); 8 void show_arrays(); 9 void show_cstrings(); 10 void show_strings(); 11
The Library Source File: http://www.nylxs.com/docs/workshops/cpp/file_1.cc.html: http://www.nylxs.com/docs/workshops/cpp/file_1.cc
1 #include 2 3 using namespace std; 4 5 6 void show_chars() 7 { 8 //declare and define char types in a function 9 char letter; 10 unsigned char letterU; 11 //assigning chars means using a single quote mark 12 letter = 'R'; 13 letterU = 'u'; 14 //declare,define and assign 15 char letterNull = 0; 16 char letterZero = '0'; 17 char letterINT = 65; 18 char letterAlert = '\a'; 19 char letterBackspace = '\b'; 20 char letterFormFeed = '\f'; 21 char letterNewLine = '\n'; 22 char letterCarriageReturn = '\r'; 23 char letterVertTab = '\v'; 24 char letterHorzTab = '\t'; 25 char letterBackslash = '\\'; 26 char letterQuestionMark = '\?'; 27 char letterSingleQuote = '\''; 28 char letterDoubleQuote = '\"'; 29 char letterOct = '\103'; 30 char letterHex = '\x44'; 31 char letterOct2 = 0104; 32 char letterHex2 = 0x45; 33 //depreciated and causes a segfault char * letterptr = "\x48"; 34 cout << "signed char ==> " << letter << endl; 35 cout << "unsigned char==> " << letterU << endl; 36 cout << "NULL char ==> " << letterNull << endl; 37 cout << "ZERO char ==> " << letterZero << endl; 38 cout << "INT char ==> " << letterINT << endl; 39 cout << "Alert char ==> " << letterAlert << endl; 40 cout << "Backspace char ==> ::" << letterBackspace << "end" << endl; 41 cout << "FormFeed char ==> " << letterFormFeed << "end" << endl; 42 cout << "New Line char ==> " << letterNewLine << "end" << endl; 43 cout << "Carriage Return char ==> " << letterCarriageReturn << "end" << endl; 44 cout << "Verticle Tab char ==> " << letterVertTab << "end" << endl; 45 cout << "Horizonal Tab char ==> " << letterHorzTab << letterHorzTab << "end" << endl; 46 cout << "Backslash char ==> " << letterBackslash << endl; 47 cout << "Question Mark char ==> " << letterQuestionMark << endl; 48 cout << "Single Quotes char ==> " << letterSingleQuote << endl; 49 cout << "Double Quote char ==> " << letterDoubleQuote << endl; 50 cout << "Octal char ==> " << letterOct << endl; 51 cout << "Hexidecimal char ==> " << letterHex << endl; 52 cout << "Octal char 2 ==> " << letterOct2 << endl; 53 cout << "Hexidecimal char 2 ==> " << letterHex2<< endl; 54 // cout << "NOT REALLY a pointer to char but a string ==> " << *letterptr << endl; 55 56 //can't asign a value to a string literal or const char: *letterptr = 'd'; 57 // cout << "pointer to char ==> " << *letterptr << endl; 58 59 60 61 } 62 63 void show_ints() 64 { 65 } 66 67 void show_floats() 68 { 69 } 70 71 void show_arrays() 72 { 73 } 74 75 void show_cstrings() 76 { 77 } 78 79 void show_strings() 80 { 81 } 82
The Main programing file: http://www.nylxs.com/docs/workshops/cpp/file_1.cc.html http://www.nylxs.com/docs/workshops/cpp/file_1.cc
#include #include "data.h"
int main(int argv, char * argc []){ show_chars(); show_ints(); show_floats(); show_arrays(); show_cstrings(); show_strings(); }
and the Makefile to compile
http://www.nylxs.com/docs/workshops/cpp/makefile.html http://www.nylxs.com/docs/workshops/cpp/makefile
data : data.o data_main.o g++ -o data data.o data_main.o
data.o : file_1.cc data.h g++ -Wall -o data.o -c file_1.cc
data_main.o : file_1_main.cc g++ -Wall -o data_main.o -c file_1_main.cc
Just to say it, there is some differences in the character handling in modern C++ and C, specific to pointers. This syntax, which is almost always wrong
char * ptr = "A"; //Double QUOTES THERE
needs to specify itself as a const
const char * ptr = "A";
and you should be aware that the double quotes is not a character, but a string of the size of 2 chars, a null char is implied, something we will be exploring more closely in the near future.
Furthermore, there is no direct way to assign the address of a literal char to a pointer.
char * ptr = 'A'; // Wrong char * ptr = "A"; // Wrong and depreciated and a string
You can do this:
char letter = 'A';
chat * ptr = &letter; //we will look at this syntax when looking at //pointers in full
Workshop Assignment: Print Out a complete set of ASCII chars and the decimal and numbers associated with them. You can use a for loop
int i = 0; for( i = 0; i < 127; i++){
//you code in here
}
and then using unsigned chars, for fun, extend it to a complete set of 256 chars.
|
|