Monday, January 17, 2011

Sizeof, comma, and arithmetic if operators

Sizeof
Operator Symbol Form Operation
Sizeof sizeof sizeof(type)
sizeof(variable)
Returns size of type or variable in bytes
Even though we’ve already talked about the sizeof operator, we’ll cover it again briefly for completeness. The sizeof operator returns the size, in bytes, of a type or a variable.
You can compile and run the following program to find out how large your data types are:

01#include <iostream>
02
03int main()
04{
05    using namespace std;
06    cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
07    cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
08    cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
09    cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
10    cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
11    cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
12    cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
13    cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
14    cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
15    return 0;
16}
Here is the output from the author’s Pentium 4 machine, using Visual Studio 2005 Express:
bool:           1 bytes
char:           1 bytes
wchar_t:        2 bytes
short:          2 bytes
int:            4 bytes
long:           4 bytes
float:          4 bytes
double:         8 bytes
long double:    8 bytes
Your results may vary if you are using a different type of machine, or a different compiler.
Comma
Operator Symbol Form Operation
Comma , x, y Evaluate x then y, return value of y
The comma operator allows you to evaluate multiple expressions wherever a single expression is allowed. The comma operator evaluates to it’s rightmost operand.
For example:

1int x = 0;
2int y = 2;
3int z = (++x, ++y); // increment x and y
z would be assigned the result of evaluating ++y, which equals 3.
However, in almost every case, a statement written using a comma would be better written as separate statements. For example, the above code should be written as:

1int x = 0;
2int y = 2;
3++x;
4++y;
5int z = y;
Most programmers do not use the comma operator at all, with the single exception of inside for loops, where it’s use is fairly common. For example:

1for (int iii = 1, jjj = 10; iii <= 10; iii++, jjj--)
The iii variable will count up from 1 to 10, while the jjj variable will count down from 10 to 1. Each iteration of the loop, iii is incremented and jjj is decremented. We will discuss for loops in more detail in the section on flow control statements.
Arithmetic if
Operator Symbol Form Operation
Arithmetic if ?: c ? x : y If c is nonzero (true) then evaluate x, otherwise evaluate y
The arithmetic if operator (?:) is also known as the conditional operator, and it is C++’s only ternary operator (it takes 3 operands). The ?: operator provides a shorthand method for doing a particular type of if/else statement.
If/else statements in the following form:
if (condition)
    x = some value
else
    x = some other value
can be rewritten as:
x = (condition) ? some value : some other value;
For example, to put the larger of values x and y in variable z, we could write this:
1if (x > y)
2    z = x;
3else
4    z = y;
Or this:

1z = (x > y) ? x : y;
It is common to put the conditional part of the expression inside of parenthesis, both to make it easier to read, and also to make sure the precedence is correct.
Keep in mind that the ?: operator has a very low precedence. If doing anything other than using the result in an assignment statement (which has even lower precedence), the ?: statement needs to be wrapped in parenthesis.
For example to print the larger of values x and y to the screen, we could do this:

1if (x > y)
2    cout << x;
3else
4    cout << y;
Or we could do this:

1cout << ((x > y) ? x : y);
Because the << operator has higher precedence than the ? operator, the statement:

1cout << (x > y) ? x : y;
would evaluate as:

1(cout << (x > y)) ? x : y;
That would print 1 (true) if x > y, or 0 (false) otherwise!
The arithmetic if gives us a convenient way to simplify simple if/else statements. It should not be used for complex if/else statements, as it quickly becomes both unreadable and error prone.

No comments: