0% found this document useful (0 votes)
2 views6 pages

Arithmetic Operators Relational, Logical Bitwise

The document provides an overview of built-in operators in C, focusing on arithmetic, relational, and logical operators. It explains the syntax and usage of these operators, including examples of basic arithmetic operations and the concept of operator precedence and associativity. Additionally, it discusses shorthand assignment operators, increment and decrement operators, and their implications in expressions.

Uploaded by

Rhean Jane Asur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Arithmetic Operators Relational, Logical Bitwise

The document provides an overview of built-in operators in C, focusing on arithmetic, relational, and logical operators. It explains the syntax and usage of these operators, including examples of basic arithmetic operations and the concept of operator precedence and associativity. Additionally, it discusses shorthand assignment operators, increment and decrement operators, and their implications in expressions.

Uploaded by

Rhean Jane Asur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

G.

Operators
C is very rich in built-in operators. An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. Aside from arithmetic operators, other general classes of C operators
are relational, logical and bitwise operators.

G.l Arithmetic Operators

Table G. 1 lists the arithmetic operators allowed in C. The operators +, - , *, and / all work the same
way in C as they do in most other computer languages.

Table G.1 Arithmetic Operators


Operator Action Example
- Subtraction, also unary a-b; -c
minus
+ Addition a+b

* Multiplication a*b

/ Division a/b
% Modulus division a%b

All five arithmetic operators are binary operators (except unary minus), and follow the general
form (expression1 arithmetic_operator expression2). This can be applied to almost any built-in data type
allowed by C.
When the division operator is applied to an integer, any remainder is truncated. This is called
integer division. For example, 10/3 equals 3 in integer division. The modulus operator, %, yields the
remainder of an integer division. However, % cannot be used on type float or double. Program G. I shows
how arithmetic operators are used.

Program G.1 Basic Arithmetic Operators


#include <stdio.h>
#include <conio.h>
main ()
{

int num1, num2 , sum, diff, product, quotient, remainder;

printf("Enter two numbers, separated by a space:" );


scanf("%d %d",&num1,&num2);
sum = num1 + num2;
diff = num1 - num2;
product = num1 * num2 ;
quotient = num1 / num2;
remainder = num1%num2;

printf("Their sum is %d",sum);


printf("\n%d minus %d is %d", num1, num2, diff);
printf("\nTheir product is %d",product);
printf("\n%d divided by %d is %d with a remainder of %d", num1, num2 , quotient,
remainder) ;
getch() ;
} // end of Program G. 1
Sample Dialogue:

Enter two numbers, separated by a space: 10 3

Their sum is 13
10 minus 3 is 7
Their product is 30
10 divided by 3 is 3 with a remainder of 1

G.2 Precedence and Associativity of Arithmetic Operators

Table G.2 shows the precedence of arithmetic operators. Operators on the same precedence level
are evaluated by the compiler fr0ffi left to right. Of course, parentheses may be used to alter the order of
evaluation. Parentheses are treated by C in the same way they are by virtually all other computer languages:
They give an operation, or set of operations, a higher precedence level.

Table G.2 Precedence of Arithmetic Operators

Highest ++ --
-(unary minus)
*/ %
Lowest +-

In most mathematical equations, different arithmetic operators are used at the same time in a single
expression. The question now arises: Which part of the expression will be evaluated first? An example is
shown in Program G.2.

Program G.2 Precedence of Arithmetic Operators


# include <stdio.h>
# include <math.h>
#include <conio.h>
main()
{float a,b,c,x1,x2;
printf("This program will derive the real roots of a quadratic equation." ) ;
printf("\nType the coefficients (a, b, c) , separating each with a space:" ) ;
scanf ("%f %f %f",&a, &b, &c);
x1 = -b + sqrt(b*b-4*a*c)/(2*a);
x2 = -b - sqrt(b*b-4*a*c)/(2*a);
printf("\nThe roots of the equation are %f and %f", x1,x2);
getch();
} // end of Program G. 2

Sample Dialogue

This program will derive the real roots of a quadratic equation


Type the coefficients (a, b, c) , separating each with a space: 1 4 1

The roots of the equation are —2.267949 and —5.732051

As can be seen from Program G.2, the assignment statement for the variable x1 and x2 uses the function
sqrt( ), which is used for determining the square root of a positive value. As an argument for sqrt( ), an
expression made up of a combination of the * and - operators were used. C evaluates the expression using order
of precedence of arithmetic operators, as shown in Table G.2. Thus, for the argument of the sqrt( ) function
(b*b – 4*a*c), the variable b is squared first (b*b), followed by 4*a*c, before taking their difference. Variable
b is multiplied by -1(unary minus) before the addition/subtraction to the derived square root of (b*b – 4*a*c).
This result will then be divided by (2*a). However, Program G.2 does not, of course, check for imaginary root.
If the equation within the sqrt( ) function evaluates to a negative value, a domain error will be generated by
the compiler. The use of sqrt( ) function requires the math.h header file.
Whenever a part of an expression is to be evaluated first even if the operator has a lower precedence than the
other operators, that part should be enclosed in parentheses. For example, in the expression (a + b) * (x- y), the
addition and subtraction are evaluated first before their results are multiplied together.

If the operators in an expression have the same precedence, such as i + j – k, which part of the expression
will be evaluated first? The answer lies in the associativity of an operator, which determines the order in which
expressions of equal precedence are evaluated. All arithmetic operators are left-associative, that is, the
expressions are evaluated from left to right. So for the given expression i + j - k, i and j will be added first
before k is subtracted from the sum.

Program G.2 does not, of course check for imaginary roots. If the equation within the sqrt( ) function
evaluates to a negative value, a domain error will be generated by the compiler.

G.3 Shorthand Assignment Operators

C has special shorthand operators that simplify the coding of a certain type of assignment statement. For
example:

a = a + 10; can be written, in C shorthand, as a += 10;

The operator pair += tells the compiler to assign to a the value of a plus 10. This shorthand works for all the
binary operators in C (those that require two öperands). The general form of the shorthand operator:
expressionl shorthand operator expression2. However, for the operation to be valid, expression2 must not
be equal to zero. Table G.3 lists the shorthand operators in C. The precedence level are the same for all
shorthand operators.

Table G.3 Shorthand Assignment Operators

Operator Type Expression Replaces


+= Addition a += b a=a+b
-= Subtraction a -= b a=a-b
*= Multiplication a *= b a=a*b
/= Division a /= b a=a/b
%= Modulus a %= b a=a%b

G.4 Increment and Decrement

C allows two very useful operators not generally found in other computer languages. These
are the increment and decrement operators, ++ and --. The operation ++ adds 1 to its operand, and --
subtracts l. Therefore, the following are equivalent operations:

a = a+1; is the same as ++a; Also a = a-1; is the same as --a;

Both the increment and the decrement operators may either precede or follow the operand.
For example,

a = a+1; can be written as ++a; or a++;

However, there is a difference when they are used in an expression. When an increment or
decrement operator precedes its operand, C performs the increment or decrement operation prior to
using the operands value. If the operator follows its operand, C uses the operand's value before
incrementing or decrementing it. Consider Programs G.3 and G.4.
Program G. 3 Using the Post-Increment/Post Decrement Operator
#include <stdio.h>
#include <conio.h>
main ()
{

int a,b,c;
a = 10;
b = a++;
c = b--*2;

printf("The value of a is %d", a );


printf("\nThe value of b is %d", b );
printf("\nThe value of c is %d", c );
getch() ;
} // end of Program G. 1

Sample Output:
The value of a is 11
The value of b is 9
The value of c is 20

In the assignment statement for the variables b and c, the increment and decrement operators are
after the variable operators. As such, the operators are called post-increment and post-decrement operators,
respectively. This means that the variable operands will be incremented/decremented only after the
expression has been fully evaluated. In the statement b = a++; , the value of the variable a is assigned first to
the variable b before it is incremented. Thus, after the statement is executed a = 11 and b = 10.

In the statement c=b--*2; , the value of b is multiplied with 2 before it is decremented. The value is
then assigned to c. Therefore, c=20 and b = 9 after the statement is executed.

When the increment/decrement operator is placed ahead of the variable operand, it is considered a
pre-increment/pre-decrement, and the increment/decrement is executed before anything else is done with the
variable. Program G.4.

Program G. 4 Using the Pre-Increment/Pre-Decrement Operator


#include <stdio.h>
#include <conio.h>
main ()
{

int a,b,c;
a = 10;
b = ++a;
c = --b*2;

printf("The value of a is %d", a );


printf("\nThe value of b is %d", b );
printf("\nThe value of c is %d", c );
getch() ;
} // end of Program G. 1

Sample Output:
The value of a is 11
The value of b is 10
The value of c is 20

G.5 Relational and Logical Operators

In the term relational operator the word relational refers to the relationships values can have with one
another. In the term logical operator the word logical refers to the ways these relationships can be connected
together using the rules of formal logic. Because the relational and logical operators often work together, they
will be discussed together here.
The key to the concepts of relational and logical operators is the idea of true and false. In C, true is any
value other than 0 (zero). False is 0. Expressions that use relational or logical operators will return 0 for false
and 1(one) for true.

Tables G.4 and G.5 show the relational and logical operators, while Table G.6 gives the truth table for
the logical operators using 1's(True) and 0's(False).

Table G.4 Relational Operators

Operator Action
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
== Equal
!= Not equal

Table G.5 Logical Operators

Operator Action
&& AND
|| OR
! NOT

Table G6 Truth Table for Logical Operators

a b a&&b a||b !a
0(False) 0(False) 0(False) 0(True) 1(True)
0(False) 1 (True) 0(False) 1(True) 1(True)
1(True) 0(False) 0(False) 1(True) 0(False)
1 True) 1 True) 1 True) 1(False) 0(False)

Both relational and logical operators are lower in precedence than the arithmetic operators. This
means that an expression like 10>1+12 is evaluated as if it were written 10>(1+12). The result is, of course,
false. The table G.7 lists the relative precedence of the relational and logical operators.

Table G7 Precedence of Relational and Logical

Highest !
>>= < <=
= = !=
&&
Lowest ||

Several operations can be combined in one expression as shown in Program G.5. In the expression
a = 10>5 &&!(10<9) || 3<=4, the logical expression (10<9) will be evaluated first and will result to 0(False)
then !(0) will yield a value of 1(True). Next, expressions 10>5, 3<=4 will evaluate true(1).The expression
will now be a = 1 && 1 || 1 which will clearly to logical 1(True). While in the statement b = !1 && 1, the
result is 0(False) because the ! is evaluated first before the && operator.
Program G.5 Precedence of Relational and Logical Operators
#include <stdio.h>
#include <conio.h>
main()
{

int a,b;
a = 10>5&&!(10<9)||3<=4;
b = !1&&1;

printf("The value of variable a is logic %d", a );


printf("\nThe value of variable b is logic %d", b );
getch() ;
} // end of Program G. 1

Sample Output:
The value of variable a is logic 1
The value of variable b is logic 0

You might also like