MESSAGE
DATE | 2010-02-19 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
C++ Integer types - Data Representation
Constant and Literal Integers are represented as numbers without any quotes. They can be represented in decimal, octal, and Hexadecimal forms. They can be assigned to int, short, long and signed and unsigned variables and they are literal, and therefor can not be left values: ie values that go on the left of an assignment operator.
Decimal Forms look like these examples:
int i = 255; short i = 255; long i = 4000; unsigned short = 32; signed int = -273;
Octal examples are similar to the integers that we saw with chars and begin with zeros:
int o = 042; short o = 0101; long o = 0076; unsigned o = 0101; singed long o =011102;
Hexadecimal examples begin with x or X and again are without quotes:
int h = 0xFF; short = 0x12; long = 0xA2E44D; unsigned = 0xA2; signed long = 0xAF23E4;
Integers also have the ability to be expressed as Unsigned or Long literal values, if the need arises to do so.
unsigned regist = 121U; long lightspeed = 123456789L; #define MAXVAL 1234567U
While discussing integer values, the programmer also needs to be aware of the size_t typedef that C and C++ uses for the sizeof() operator. The sizeof() operator returns the size of any data object in size of bytes. Its return value is an integer of type size_t. Because in C and C++ we manage memory directly, the sizeof() operator plays a significant role in your programming. And example of sizeof() is:
size_t i = sizeof(oint);
One of the unusual properties of integers and chars is that both represent real integer values. As a result, many of the mathmatical operators can be used with them and they can be assigned to each other. There are automated rules for "recasting" the data types as they interact with each other. We'll look at this rules in detail later. But for now one should be aware that statements theses are common in C and C++.
char letter, letter2; int number; short num2; long num3;
letter = 'c'; letter2 = 'G'; letter++; //now stores 'd'
cout << letter << " " << endl; //prints 'd' and a line feed
num2 = letter;
cout << num2 << " " << endl; //prints '100' and a line feed
cout << letter + leter2;
Floating Point
Floating Point data essentially can be represented in the two styles already discussed, as decimal and scientific notation. Decimal notation can be followed by an 'F' or 'f' for single precision or a double precision with an 'L' or 'l' (not a 'D' for double). Here are some examples:
double avog = 6.23xE23; float pizza = 0.125; float trip = 102.7F float population = 8,323,456L; float pop_brklyn = 8.32xE6;
Casting: C and C++ are typed languages, but they have some flexibility built into their design in this regard. This can be good and bad, because this also means that the language will give you room to hang yourself if you don't learn the explicit rules for the accommodations that C++ will make for you. For example, one can assign a float into an integer - but then you are left to understand what the resulting outcome is. And these are very difficult bugs to catch because the code looks correct, seems logical, and it is a raw syntax error.
For example, what does this legal code do?
#include #include ;
int a,b,c,z; float d,e,f;
a = 1.25; b = 2.50; c = 5;
d = 0.6125; e = 0.30625; f = 0.153125;
z = 25U + 75;
z = z * a; cout << b/2 << endl; z=((pow(b,2)) + e)/((pow(f,e)) * z);
cout << "I have no clue what this results in and can't be paid enough to debug it " << z << endl;
Rules for Casting:
Implicit Rules:
When the compiler is confronted with two different data types, it tries to work operations by casting one data type to another which is compatable to the expression. The programmer can also manually do such casting. The Implicit casting rules are as follows:
There are 4 events that trigger C and C++ to do implicit type conversion:
A) When the operants in a mathmatic or logic expression are of two different type: example
char a = 'A'; int b = 9; long c; c = a + b; if(c == b){ //do something }
B) When the assignment on the right side of an equation doesn't match the type of the variable or lvaue on the left: example
char a = 'a'; double b; b = c;
C) When the argument to a function or method doesn't match the parameter. We will see this when we look at functions.
D) When the return statement doesn't match the function type. Again, we will see this when we examine functions.
Rules:
Generally the implicit cast rules are designed to loose the least about of precision possible. If an arithmatic operation includes floating point data then the implicit casting of data follows the following rules
float --promotion-->double-->long double
Neither operand in an arithmatic operation has a float:
int-->unsigned int-->long-->unsigned long : Note that these promotions can lose there sign (lose negitivity). I also note that when attempting to verify these promotion rules in C and in C++, that they don't hold. I can not create an example program that will promote the signed data type to an unsigned one, and to then lose the sign.
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium
|
|