Operators in C

In C programming language operator is a symbol that helps in the calculation, creating a decision, etc.

Arithmetic Operators in c

In this macro group I am going to separate binary operators and unary
operators.

Binary operators work using two operands

OperatorName
Substraction
+Addition
*Multiplication
%Modulus
/Division

Decrement & Increment Operators in c

OperatorNameDescription
DecrementIt’s decremnet one value
++IncrementIt’s increment one value

Logical Operators in c

OperatorNameDescription
!NOTIt’s works’ with negation on an expression
||ORIt’s wok’s with single and multiplication condition
&&ANDIt’s work’s with Multiple condition if both are ture

Relational Operators in c

OperatorNameDescription
<Less thanit works if less than
==is equal toFor check any number & character
<=Less than or equalit work with both less or equal
>Greater thanit work if greater than
>=Greater than or equalit work with both greater or equal
!=Not equal toIf work with is not equal to

Bitwise Operators in c

OperatorName
>>Binary Right shift operator
<<Binary Left shift operator
&Binary AND Operator
~Binary Ones Complement Operator
|Binary OR Operator
^Binary XOR Operator

Miscellaneous Operators in C

The ternary operator
The ternary operator is the only operator in C that works with 3 operands, and
it’s a short way to express conditionals.

This is how it looks:

<condition> ?<expression> :<expression>
a ? b : c

If a is evaluated to true , then the b statement is executed, otherwise c is.

The ternary operator is functionality-wise same as an if/else conditional,
except it is shorter to express and it can be inlined into an expression

OperatorName
&find address of a variable
?:;Conditional statement
,Comma Operator
*pointer variable declaration

sizeof Operator

The sizeof operator returns the size of the operand you pass. You can pass
a variable, or even a type.

Example usage:

include<studio.h>
int main(void) {
int age = 37;
printf("%ldn", sizeof(age));
printf("%ld", sizeof(int));
}

Operator precedence

With all those operators (and more, which I haven’t covered in this post,
including bitwise, structure operators and pointer operators), we must pay
attention when using them together in a single expression.

Suppose we have this operation:

int a = 2;
int b = 4;
int c = b + a * a / b - a;
24

What’s the value of c ? Do we get the addition being executed before the
multiplication and the division?

There is a set of rules that help us solving this puzzle. In order from less precedence to more precedence, we have:

  • the = assignment operator
  • the + and – binary operators
  • the * and / operators
  • the + and – unary operators

Operators also have an associativity rule, which is always left to right except
for the unary operators and the assignment.

int c = b + a * a / b - a;

We first execute a * a / b , which due to being left-to-right we can separate
into a * a and the result / b : 2 * 2 = 4 , 4 / 4 = 1 .

Then we can perform the sum and the subtraction: 4 + 1 – 2. The value of c
is 3 .

In all cases, however, I want to make sure you realize you can use parentheses to make any similar expression easier to read and comprehend.

Parentheses have higher priority over anything else.
The above example expression can be rewritten as:

int c = b + ((a * a) / b) – a;

and we don’t have to think about it that much.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Operators In C Programming Language

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Explore Tutorials By Categories

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password