Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

3.5 Operators

2 min read
 Image result for Operators C++
An operator is a symbol. Compiler identifies Operator and performs specific mathematical or logical operation. C provides following operators :
# Arithmetic Operators
# Logical Operators
# Increment and Decrement Operators
# Relational Operators
# Cast Operators
# Bitwise Operators
# Assignment Operators
# Misc

Arithmetic Operators
* multiplication
/ division
% remainder after division (modulo arithmetic)
+ addition
- subtraction and unary minus
Logical Operators
&&  Called Logical AND operator. If both the operands are non-zero, then condition becomes true.

|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.

!  Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.

Increment and Decrement Operators
Increment and decrement operators are used to add or subtract 1 from the current value of oprand.

++  increment
--  decrement
Increment and Decrement operators can be prefix or postfix. In the prefix style the value of oprand is changed before the result of expression and in the postfix style the variable is modified after result.

For eg.
a = 9;
b = a++ + 5; /* a=10 b=14 */
a = 9;
b = ++a + 5; /* a=10 b=15 */

Relational Operators
== equal.
!= Not equal.
> < Greater than/less than
>= greater than or equal to
<= less than or equal to
Cast Operators
Cast operators are used to convert a value from one to another type.
(float) sum; &nbsp converts type to float
(int) fred; &nbsp converts type to int

Bitwise Operators
Bitwise operators performs operation on actual bits present in each byte of a variable. Each byte contain 8 bits, each bit can store the value 0 or 1

~ one's complement
& bitwise AND
^ bitwise XOR
| bitwise OR
<< left shift (binary multiply by 2)
>> right shift (binary divide by 2)


Assignment Operators
= assign
+= assign with add
-= assign with subtract
*= assign with multiply
/= assign with divide
%= assign with remainder
>>= assign with right shift
<<= assign with left shift
&= assign with bitwise AND
^= assign with bitwise XOR
|= assign with bitwise OR
For example,
a = a + 64; is same as
a += 64;

Misc
sizeof
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:
cout << "bool:\t\t" << sizeof(bool) << " bytes";
bool: 1 bytes

Condition ? X : Y
Condition operator: If Condition is true ? then it returns value X : otherwise value Y

,
Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list

. (dot) and -> (arrow)
Member operators are used to reference individual members of classes, structures, and unions.

& Pointer operator: & returns the address of an variable. For example &a; will give actual address of the variable.

* Pointer operator: * is pointer to a variable. For example *var; will pointer to a variable var.

                      4.1 Arrays          NEXT >>

You may like these posts

  • Object Oriented Programming has a special feature called data abstraction. Data abstraction allows ignoring the details of how a data type is represented. While defining a clas…
  • A file is collection of related records, a record is composed of several fields and field is a group of character. This requires another standard C++ library called fstream which…
  •   Encapsulation is the method of combining the data and functions inside a class. This hides the data from being accessed from outside a class directly, only through th…
  • Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers. C++ exception h…
  • You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of argu…
  • Polymorphism "Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of…

Post a Comment