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

Removing HTML Tags from string in C#

Hello friends, With this very first post of our new Blog GimiBits, we start our whole new journey contributing to information technology. Our First post is about the logic to remove the HTML tags from the given string with C# (C Sharp) programming language. Logic is very Simple to Implement, we take up a string and for its each index char we find the opening '<' braces and ignores the inside content until the closing '>' braces is not encountered and save to another char[] datatype. Here is a function based Implementation of the logic:  public string RemoveHtmlTags(string Html)         {             string content = Html;             char[] array = new char[content.Length];             int arrayIndex = 0;           ...

List Of Most Searched Topics On Google 2016

List Of Most Searched Topics On Google                                                                                                                               source Global Searches   1) Pokémon Go  2) iPhone 7   3) Donald Trump   4) Prince   5) Powerball   6) David Bowie   7) Deadpool   8) Olympics   9) Slither.io  1 0) Suicide Squad Global News   1) US Election  2) Olympics   3) Brexit   4) Orlando Shooting  5) Zika Virus   6) Panama Papers  7) Nice 8) Brussels  9) Dallas Shooting  10)...

15 BEST PHOTO EDITING APPS FOR ANDROID

There are various photo editing apps available on play store.But there are few apps that can make the most unflattering of images into pieces of art in a matter of few taps.From Amazing Photo Effects to Clicking Best                       Selfies,These Apps are Worth a Shot. 1. Adobe Photoshop Lightroom Bring Beautiful Images into light with adobe lightroom.powered by the magic ofadobe photoshop technology,lightroom for mobile enables you to craft and share professional quality images from smartphone to reality. Adobe has vastly improved their mobile offerings over the last year with Lightroom and Photoshop Express leading the charge in the photo editing apps department.Winner of 2015 Professional photographer hot one awards in the photo category app. They’re both more powerful than average and they both also sync to the desktop variants as long as you use the Adobe Creative Cloud. Individually, Lightroom can be u...