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:
#include <iostream>
int main()
{
using namespace std;
cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
return 0;
}
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:
int x = 0; int y = 2; int 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:
int x = 0; int y = 2; ++x; ++y; int 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:
for (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:
if (x > y)
z = x;
else
z = y;
Or this:
z = (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:
if (x > y)
cout << x;
else
cout << y;
Or we could do this:
cout << ((x > y) ? x : y);
Because the << operator has higher precedence than the ? operator, the statement:
cout << (x > y) ? x : y;
would evaluate as:
(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.
3.5 -- Relational operators (comparisons)
|
Index
|
3.3 -- Increment/decrement operators, and side effects
|
3.5 -- Relational operators (comparisons)
Index
3.3 -- Increment/decrement operators, and side effects
Alex, two things,
1.) In the first example the horizontal tabs are missing their escape characters.
2.) Immediately after the first example author is spelled rather unusually.
[ Thanks for letting me know! -Alex ]
[...] 2007 Prev/Next Posts « 3.2 — Arithmetic operators | Home | 3.4 — Sizeof, comma, and arithmetic if operators » Wednesday, June 13th, 2007 at 8:23 [...]
In your first example, shouldn’t the \t be t only?
It wouldn’t output like that unless I changed it.
Using Geany as IDE and gcc as compiler.
[ You are correct. I've updated the example. Thanks! -Alex ]
Hey, whats the point of the comma operator if all it does is evaluate to the rightmost operand?
The only place I see the comma operator commonly used is inside loops, to increment multiple variables, especially for loops. We cover for loops in more detail in Chapter 5.7 — For statements.
Using the comma operator is a useful way to increment or decrement multiple variables in a single expression. For example:
Due to the particular syntax constraints on for loops, all your variables have to be modified in a single expression — this allows you to do that.
Are you using the comma operator too when initializing multiple variables in one statement?
hi,
using sizeof function
will resolve during
1. runtime
2. compile time 3. pre-compile time
plz answer me
It’s not a function, sizeof is an operator.
Its value is determined in compile time.
Arithmetic if is a construct that takes a number and executes different things depending on
that number beeing less, equal or greater than zero. Such statement can be found in FORTRAN.
But there is no arithmetic if in C++!
A ternary operator employs your usual logical condition (i.e. if not zero).
Is it bad form to place the assignment inside the arithmetric if? eg.:
It compiles fine with Codeblocks and GCC.
This seems more elegant IMHO.
It’s a bit confusing that a lower precedence corresponds to a higher precedence level here. Is that the standard way of saying it?
return x, y;
would this only return y?