Skip to main content

C-Operators

An operator is a symbol that operates on a certain data types and produce the output as the result of the operation.

Category of Operators

• Unary Operator

An unary operator is an operator which operates on one operand that is it operates on itself.
     
     Operand1 operator
Here Operand1 may be a variable , a constant or an expression etc.
   For example: -b , ~a , x+1 etc.

Binary Operator
A binary operator is an operator which operates on two operands.

Operand1 operator Operand2

Here Operand1 and Operand2 maybe a variable, a constant or an expression etc. and Operator must be either arithmetic type or relational type or logical type etc.
   For example: a+b , a && b , etc.

Ternary Operator

A ternary operator is an operator which operates on 3 operands.

Operand1 operator Operand2 operator Operand3
  Here Operand1 , Operand 2 , Operand3 may be a variable , a constant or an expression etc. and Operator must be either arithmetic type or relational type or logical type etc.
    For example: a+b+c , a && b || c , a>b>c




TYPES OF OPERATOR

There are various operators used in C language.There are mainly eight operators used in C language.

Arithmetic Operators

Suppose A=5 & B=10 are two variables, then Arithmetic operators used for different operations as :
Operator Description example
+ Adds two operands A+B gives 15
- subtracts second operand from first A-B gives -5
* multiply the two operands A*B gives 50
/ divide numerator by denominator B/A gives 2
% modulus operator B%A gives 0

Relational Operators

It is required to compare the relationship between operands and bring out at decisions and program accordingly for which relational operators are used.
Mainly relational operators are categorised into two sections one is comparison section and second one is equality section.There are mainly 6 relational operators used in the C language , first two operators are in the equality section and last four are in the comparison section.
Suppose A=5 & B=20 are two variables,then arithmetic operators used for different operations as:
Operator Description example
== Checks if the value of two operands is equal or not, if yes then condition becomes true . (A==B) is not true.
!= Checks if the value of two operands is equal or not, if values are not equal then condition becomes true . (A!=B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A>B) is not true.
< Checks if the value of left operand is less than the value of a right operand, if yes then condition becomes true . (A
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true . (A>=B)< is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true . (A<=B) is true.

These operators are used in if statement, switch statement, conditional operator statement, while statement, do-while statement and for statement.These operators are mainly used for decision making statement .These operators give the results in two branches ,one is of true type and other is of false type.


Logical Operators

A logical operator is used to compare or evaluate logical and relational expressions. These operations are used for compound relational expression or logical expressions .These are used in decision making statement and some looping statements.

Suppose A=5 & B=10 are two variables,then arithmetic operators used for different operations as:

Operator Description example
&& Called logical AND operator.If both the operands are non zero then condition becomes true. (A&&B) is true.
|| Called logical OR operator.If any of the two operands is non zero then condition becomes true. (A||B) is true.
! Called logical NOT operator.Use to reverse the logical state of its operand. If a condition is true then logical NOT operator will make false. !(A&&B) is false.

Assignment Operators

The assignment operator evaluates an expression on the right of the expression and substitute it to the value of a variable on the left of the expression.

Operator Description example
= Simple assignment operator, assigns values from right side operands to left side operand. C=A+B will assign value of A+B into C.
+= Add AND assignment operator, it adds right operand to the left operand and assign the result to left operand. C+=A is equivalent to C=C+A.
-= Subtract AND assignment operator ,it subtracts right operand from the left operand and assign the result to left operand. C-=A is equivalent to C=C-A
*= multiply AND assignment operator ,it multiplies right operand with the left operand and assign the result to left operand. C*=A is equivalent to C=C*A
/= divide AND assignment operator ,it divides left operand with the right operand and assign the result to left operand. C/=A is equivalent to C=C/A.
%= modulus AND assignment operator ,it takes modulus using two operands and assign the result to left operand. C%=A is equivalent to C=C%A
<<= left shift AND assignment operator. C<<=2 is equivalent to C=C<<2
>>= right shift AND assignment operator. C>>=2 is equivalent to C=C>>2
&= BitwiseAND AND assignment operator. C &=2 is equivalent to C=C&2
^= Bitwise exclusive OR and assignment operator. C ^=2 is equivalent to C=C^2
|= Bitwise inclusive OR and assignment operator. C |=2 is equivalent to C=C|2

Conditional or Ternary Operator


The conditional operator consists of two symbols the question mark(?) and the colon (:).The syntax for a ternary operator is as follows:

(exp1 ? exp2 : exp3;)
Operator Description example
? : Conditional Exlression If condition is true ? Then value X:Otherwise value Y

Increment & Decrement Operators


These operators also fall under unary operator but are quite distinct than unary minus(-).The increment(++) and decrement(--) operators are very useful in C language.Increment operators are used to increment value one by one and decrement operators are used to decrement the value one by one. They are extensively used in for and while loops .These operators can be used in either the prefix for postfix notations.

Suppose A=5 a variable,then arithmetic operators used for different operations as:
Operator Description example
++ Increment operator,increases integer value by one A++ will give 6
-- Decrement operator,decreases integer value by one A-- will give 4

Bitwise Operators

As programmers will occasionally need to work with streams of ones and zeros.To facilitate this requirement, C provides a range ofbit operators.

Suppose A=171 & B=3 are two variables:
Operator Description example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A&B) will give 3
| Binary OR Operator copies a bit if it exists in either operand. (A|B) will give 171
^ Binary XOR Operator copies a bit if it is set in one operand but not both. (A^B) will give 168
~ Binary ONES Complement Operator is unary and has the effect of 'flipping' bits. (~A) will give 84
<< Binary LEFT SHIFT. The left operands value is moved left by the number of bits specified by the right operand. (A<<1) will give 342
>> Binary RIGHT SHIFT Operator. The left operands value is moved right by the number of bits specified by the right operand. (A>>1) will give 85

Special Operators

C supports some special operators of interest such as comma operator, size of operator, pointer operators etc.

Operator Description example
sizeof() Returns the size of an variable. sizeof(a) will give 4, where a is integer.
& Returns the address of an variable. &a, will give actual address of the variable.
* pointer to variable. *a, will point to the variable.

Prev
next

Comments

Popular posts from this blog

THE BEST BRAIN TRAINING APPS FOR ANDROID

THE BEST BRAIN TRAINING APPS FOR ANDROID Elevate - Brain Training Elevate is a brain training program designed to improve attention, speaking skills, processing speed, memory, math skills, and more. Each person is provided with his or her own personalized training program that adjusts over time to maximize results. The more you train with Elevate, the more you’ll improve critical cognitive skills that are designed to boost productivity, earning power, and self-confidence. Users who train at least 3 times per week have reported dramatic gains and increased confidence. FEATURES * 35+ brain games for critical cognitive skills like focus, memory, processing, math, precision, and comprehension * Detailed performance tracking * Personalized daily workouts that include the skills you need most * Adaptive difficulty progression to ensure your experience is challenging * Workout calendar to help you track your streaks and stay motivated Peak - Brain Training P

Adding Dialogue Boxes IN HTML5 AND CSS3.

This post is about adding Dialogue Boxes in html. Here is the implementation of the logic. <!DOCTYPE html> <html> <head> <title>HTML dialog Tag</title> </head> <body> <!-- Simple pop-up dialog box, containing a form --> <dialog id="dialog"> <form method="dialog"> <h3> Hello</h3> <menu> <button id="close" type="reset">Close</button> </menu> </form> </dialog> <menu> <button id="click">Click Me</button> </menu> <script> (function() { var clickButton = document.getElementById('click'); var closeButton = document.getElementById('close'); var Dialog = document.getElementById('dialog'); // Update button opens a modal dialog clickButton.addEventListener('click', function() { Dialog.showModal(); }); // Form close button closes the dialog

Describing ordered and unordered lists

<!DOCTYPE html> <html> <body>     <p>Unordered List</p>   <ul>     <li>lenovo</li>     <li>Oppo</li>     <li>Microsoft</li>   </ul>     <p>Ordered List</p>   <ol type= "a">     <li>lenovo</li>     <li>Oppo</li>     <li>Microsoft</li>   </ol>     </body> </html>      Unordered list - Item Markers • disk • square • circle • none The  list-style-type property is used to describe the type of list item Markers. Syntax : <ul style =" list-style-type :none ">                         -------------    Ordered list The type attribute of <ol> tag is used to select type of list item marker.