Tuesday, March 5, 2024

INTRODUCTION TO OPERATOR IN C


Introduction to operators


  • DIFFERENT TYPES OF OPERATORS LIKE BITWISE, ARITHMETIC, RELATIONAL OPERATORS ETC
  • OPERATOR PRECEDENCE AND ASSOCIATIVITY IN C
  • OPERATOR PRECEDENCE AND ASSOCIATIVITY IN THE TABLE 

Symbols associated with C operators can be utilized to perform logical or mathematical operations. The C programming language has a large number of operators built in. Programs that work with data and variables make use of operators. They are a component of mathematical or logical statements. There are many different kinds of operators in C programming.

Arithmetic Operator

This operator is used in mathematical operators on operands.

Suppose variable A=10 and variable B= 20 then 


                                                          

Relational operator

This operator is used to compare two quantities or values


                                                                                                                                                  

Bitwise Operator

This operator is used in C for bit operations between two variables

For-ex: - A =60 and B= 13


Logical Operator

The basic three logical operators used in C language to decide on one condition whether it is true or false.

For ex :

 

 Miscellaneous operator


Assignment operator

Applying this operator to a variable allows you to assign the outcome of an expression to it. There are several shorthand assignment operators in C.





Operator Precedence and Associativity in C

C's concepts of operator precedence and associativity are useful for setting the order of precedence for the operators in an expression that has several operators. When multiple operators are present in a C code, the compiler often assigns greater weight to the operator with the highest precedence. It helps to keep the expression's ambiguity while preventing the unneeded use of parenthesis.

The precedence table, operator associativity, and operator precedence are the techniques used in this article to determine the order in which operators are evaluated in C code.

 Operator Precedence in C

Operator precedence means which operation the programming language will perform first in an expression that may have one or many operators with different precedence.

An example of the importance of an operator

Let's try to evaluate the following expression:

10 + 20 * 30

The expression contains two operators + (plus) and * (multiplication). According to the given table, * has higher priority than +, so the first evaluation is

10 + (20 * 30)

After evaluating the higher priority operator, the expression is 10 + 600

Now the + operator is estimated.

610

We can also understand this by a C program 


Operator associativity

Operator associativity is used when there are two operators with the same order of importance in the expression. The relationship can be either left to right or right to left.

Example of operator associativity

We evaluate the following expression,

100 / 5% 2

Both / (division) and % (modulo) operators have the same level of precedence, so associativity determines the order of evaluation.

According to the given table, multiplication operators are associativity from left to right. Thus,

(100 / 5) % 2

After evaluation, the expression is

20 % 2

Now the % operator is evaluated.

0.



 Operator Precedence and Associativity Table

 The following tables list the C operator precedence from highest to lowest and the associativity for each of the operators:




No comments:

Post a Comment

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular