Operators in C/C++ languages
There is also one operator with three operands, which is ? : (explained later).
-
Aritmetics operators
Operator | Using | Description |
---|---|---|
+ | op1+op2 | addition of two numbers |
- | op1-op2 | subtraction two numbers |
* | op1*op2 | Multiple op1 width op2 |
/ | op1/op2 | Division two numbers |
% | op1%op2 | Calculates the rest of division of two numbers |
Solution:
scanf(" %lf%lf ", &R1, &R2); //The user enters R1 and R2
Re=R1*R2 / (R1+R2); // The solution obtained is given by the equation
printf("%f",Re);
The rest of the division of two numbers. Operator "%"
Suppose we want to divide two cell numbers: 7 & 3
If we write 7/3 we get for result 2. The result is an integer because we have divided two integer constants
If we want to get the rest of the division then we would use the operator %
7 % 3 = 1
Example: Load time in minutes and print as mm: ss
scanf(" %d ", &Vr); //The user inputs time in seconds
/*Suppose the user entered during the 132s. Since one minute has 60s,
of these 132s we will convert 2 * 60s in two minutes, and what remains to be put as a second, in this
example it's 132- (2 * 60) = 12s. This calculation can be obtained using the division operator and the remainder of the division, "/" and "%". */
mm=Vr / 60; // mm=132/60=2
ss=Vr % 60; // mm=132%60=12
printf("%2d : %2d",mm,ss);
The data type for the result | Data type for values |
---|---|
long | None of the values is float or double (whole number artifacts), a At least one of the operators is long type |
int | None of the values is float or double (integer arithmetic). No operand is long. |
double | At least one value is double type |
float | At least one value is a float type. None is double type |
Unary operators "++", "-"
Relational and conditional operators
Operator | Using | Return true if |
---|---|---|
> | op1>op2 | op1 greater then op2 |
>= | op1 >= op2 | op2 greater then or equal to op1 |
< | op1 < op2 | op1 Less then op2 |
<= | op1 <= op2 | op1 less then or equal to op2 |
== | op1 == op2 | op1 equal to op2 |
!= | op1 != op2 | op1 different to op2 |
Operator | using | return true if |
---|---|---|
&& | op1 && op2 | both op1 and op2 are true. op2 is calculated only if necessary |
|| | op1 || op2 | either op1 or op2 are true or both. op2 is calculated only if necessary |
! | !op | op is false |
& | op1 & op2 | both op1 and op2 are true. Always calculate op1 and op2 |
| | op1 | op2 | either op1 or op2 are true or both. Always calculate op1 and op2. |
^ | op1 ^ op2 | if op1 and op2 are different, or if one has the value true, but not both |
Example: For the set values of integer variables a, b, c, determine the values of logical expressions:
- a>b
- ! a > b && !(b<c)
- a !=0 || !(a>(b>c))
- a > b, have value 0,
because 4 is no greater than 7 - ! (a > b && !(b<c)), have value 1.
If we replace the expression for a, b and c we get
! ((4 > 7) && !(7<11)) = !((0) && (1)) = !(0)=1The displayed brackets show which operators will be executed in the order. First, those operators with the highest priority (see the last table below) are executed. Therefore, the operators ">" and "<" have a higher priority than the operator "&&", so the phrases 4> 7 whose value is 0 (false) and 7 <11 whose value is 1 (true) are calculated first. Then 0 && 1 is calculated, giving 0 (false). In the end, a negation is performed! which will convert this value to 1 (true). - a !=0 || !(a>(b>c)) have value 1
4 != 0 || (4> (7 > 11)) =
(4 != 0) || (4> (7 > 11)) =
(0) || (4> (0))=
0 || 1=
1
Solution of this example:
using namespace std;
int main()
{
int a,b,c;
bool e,f,g;
a=4;
b=7;
c=11;
//Calculating Logical Expressions
e=a > b;
f = !(a > b && !(b < c));
g = a!=0 || !(a > (b > c));
//Print the expression value
cout << "e=" << e << endl;
cout << "f=" << f << endl;
cout << "g="<< g << endl;
return 0;Operators by bit
- operators to perform logical operations between pairs of bits on the same positions within their operands
- shift operators
- ~ It performs complementing (negating) the value of its operand by bit. Thus, it converts from state 0 to state 1 and vice versa
- & Logical "and" for bit operations.
- | Logic included "or" for bit operations.
- ^ Logic exclusively "or" for bit operations.
- << It moves the left operand for as many binary places as the right-hand value of the right operand
- >> Moving the left operand for as many binary places to the right is the value of the right operand. Moving to the right of the n position is equivalent to dividing the number with 2 ^ n
Examples for operators by bits
- 25 & 5 = 1 0000000000011001 &
0000000000000001
- 25 | 5 = 29 0000000000011001 |
0000000000011101
- 25 ^ 5 = 28 0000000000011001 ^
0000000000011100
- 25 << 2 = 100 0000000000011001 <<2 =
- 25 >> 3 = 3 0000000000011001 &
0000000000000011
Assignment operator
Operator | Using | Equivalent to: |
---|---|---|
+= | op1 += op2 | op1 = op1 + op2 |
-= | op1 -= op2 | op1 = op1 - op2 |
*= | op1 *= op2 | op1 = op1 * op2 |
/= | op1 /= op2 | op1 = op1 / op2 |
%= | op1 %= op2 | op1 = op1 % op2 |
&= | op1 &= op2 | op1 = op1 & op2 |
|= | op1 |= op2 | op1 = op1 | op2 |
^= | op1 ^= op2 | op1 = op1 ^ op2 |
For instance:
int N=28;
N=N<<3;
The starting number 28 will be transformed to number 224 and this value is assigned to the same memory N.
- 28 << 3 = 224 0000000000011100 <<2 =
The other operators in C, C++
Conditional expression: ? :
For entered integers a and b, specify a greater number.
We reserve the memory for 3 data, and b and larger than the two will be placed in the new variable maxAB.
int a,b,maxAB;
The user then enters a and b from the console:
scanf("%d%d", &a, &b);
To assign a value of maxAB, two variants are possible:
maxAB = a; if a >= b or
maxAB = b, if a < b
If you put both variants, the first value of the maxAB would be, and later it would change to b and it would always be b.
It is first necessary to ask whether a> b, and if it is true then maxAB = a is executed, otherwise maxAB = b;
For this, the conditional expression is used:
maxAB=(a>b) ? a : b;
Finally, this value should be displayed:
printf("maxAB=%d", maxAB);
SIZEOF operator
#include < stdlib.h >
int main(int argc, char *argv[])
{
char b;
short c;
long d;
long long e;
unsigned long int f;
printf("\nEnter the integer value a="); scanf("%d",&a);
printf("\nEnter the integer value b="); scanf("%d",&b);
printf("\nEnter the integer value c="); scanf("%d",&c);
printf("\nEnter the integer value d="); scanf("%d",&d);
printf("\nEnter the integer value e="); scanf("%d",&e);
printf("\nEnter the integer value f="); scanf("%d",&f);
printf("\nLength of data int is %d byte and this is equal a=%d bajta",sizeof(int),sizeof(a));
printf("\nLength of data char is %d byte and this is equal b=%d bajta",sizeof(char),sizeof(b));
printf("\nLength of data short is %d byte and this is equal c=%d bajta",sizeof(short),sizeof(c));
printf("\nLength of data long is %d byte and this is equal d=%d bajta",sizeof(long),sizeof(d));
printf("\nLength of data long long is %d byte and this is equal e=%d bajta",sizeof(long long),sizeof(e));
printf("\nLength of data unsigned long int is %d bajta byte and this is equal f=%d bajta",sizeof(unsigned long int),sizeof(d));
return 0;
Comma operator ","
Other operators
Operator | description of operators |
---|---|
[] | Indexing |
() | call function |
. | access to the member (element) of the structure |
-> | access to the member (element) of the structure using pointers |
*(unary) | access to data by pointers (indirect addressing) |
&(unary) | variable address |
Operator Priority Table
The table below describes the order of priorities and association of operators in C / C ++. The advantage of the operator decreases from top to bottom.
Priority | Number of operands | Grouping Direction: | Operators: |
---|---|---|---|
15 | 2 | -> | [] () . -> |
14 | 1 | -> | ! ~ ++ -- + - * & (tip) sizeof |
13 | 2 | -> | * / % |
12 | 2 | -> | + - |
11 | 2 | -> | << >> |
10 | 2 | -> | < <= > >= |
9 | 2 | -> | == != |
8 | 2 | -> | & |
7 | 2 | -> | ^ |
6 | 2 | -> | | |
5 | 2 | -> | && |
4 | 2 | -> | || |
3 | 2 | -> | ?: |
2 | 2 | -> | = += -= *= /= %= &= ^= |= <<= >>= |
1 | 2 | -> | , |
Previous
|< Data in languages C/C++ |
Next
Selection statements >| |