Type Casting Operator In Cpp

Explicit type casting operator:

Type casting operators allow you to convert a datum of a given type to another. There are several ways to do this in C++. The simplest one, which has been inherited from the C language, is to precede the expression to be converted by the new type enclosed between parentheses (()):

CODE/PROGRAM/EXAMPLE
int i;
float f = 3.14;
i = (int) f;

The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here, the typecasting operator was (int). Another way to do the same thing in C++ is using the functional notation: preceding the expression to be converted by the type and enclosing the expression between parentheses:

Syntax
i = int ( f );

Both ways of type casting are valid in C++.

sizeof():

This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object:

Syntax
a = sizeof (char);

This will assign the value 1 to a because char is a one-byte long type. The value returned by sizeof is a constant, so it is always determined before program execution.

Other operators:

Later in these tutorials, we will see a few more operators, like the ones referring to pointers or the specifics for object-oriented programming. Each one is treated in its respective section.

Precedence of operators:

When writing complex expressions with several operands, we may have some doubts about which operand is evaluated first and which later. For example, in this expression:

CODE/PROGRAM/EXAMPLE
a = 5 + 7 % 2

we may doubt if it really means:

CODE/PROGRAM/EXAMPLE
a = 5 + (7 % 2) // with a result of 6, or
a = (5 + 7) % 2 // with a result of 0

The correct answer is the first of the two expressions, with a result of 6. There is an established order with the priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but for all the operators which can appear in C++. From greatest to lowest priority, the priority order is as follows:

Level Operator Description Grouping
1 :: scope Left-to-right
2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left-to-right
3 * & / + - indirection and reference (pointers) / unary sign operator Right-to-left
4 (type) type casting Right-to-left
5 .* ->* pointer-to pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <= >= relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 ^ bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left
18 , comma Left-to-right

Grouping defines the precedence order in which operators are evaluated in the case that there are several operators of the same level in an expression.

All these precedence levels for operators can be manipulated or become more legible by removing possible ambiguities using parentheses signs ( and ), as in this example:

CODE/PROGRAM/EXAMPLE
a = 5 + 7 % 2;

might be written either as:

CODE/PROGRAM/EXAMPLE
a = 5 + (7 % 2);
  or
a = (5 + 7) % 2;

depending on the operation that we want to perform.

So if you want to write complicated expressions and you are not completely sure of the precedence levels, always include parentheses. It will also become a code easier to read.

#type_casting_operator_in_c++ #Explicit_type_casting_operator_in_c++ #sizeof()_operator_in_c++ #Precedence_of_operators_in_c++

(New page will open, for Comment)

Not yet commented...