Showing posts with label Operators. Show all posts
Showing posts with label Operators. Show all posts

## Operators

An operator is a symbol. Compiler identifies Operator and performs specific mathematical or logical operation. C provides following operators :

### Arithmetic Operators

* multiplication
/ division
% remainder after division (modulo arithmetic)
- 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;   converts type to float
(int) fred;   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 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;

### Miscsizeof

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.