You are on page 1of 12

Department of Mechatronics and Control Engineering

University of Engineering and Technology Lahore

LAB 5: OPERATORS IN C LANGUAGE


MCT-242L: Computer Programming-I Lab (Fall-2022)

OBJECTIVE:
This lab will introduce different operators available in C Language. At the end of this lab, you should be able
to:

• Recognize and use different types of operators available in C


• Bit masking and its application in bitwise operators
• Understand the operators precedence

APPARATUS:
• Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10

OPERATORS:1
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C
language is rich in built-in operators and provides the following types of operators:
• Arithmetic Operators • Logical Operators • Assignment Operators
• Relational Operators • Bitwise Operators • Miscellaneous Operators

Arithmetic Operators

The following table shows all the arithmetic operators supported by the C language. Assume variable A holds
10 and variable B holds 20 then:

Operator Description Example


+ Adds two operands A + B = 30
- Subtracts second operand from the first A – B = -10
* Multiply both operands A * B = 200
/ Divides numerator by denominator B/A=2
% Modulus operator and remainder of after an integer division B%A=0
++ Increment operator increases the integer value by one A++ = 11
-- Decrement operator decreases the integer value by one B-- = 19

1
https://www.tutorialspoint.com/cprogramming/c_operators.htm

1|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Example 5.1: Arithmetic Operators in C


/* Example_5_1.c: Arithmetic Operators in C
---------------------------------------------------------------------------
This program demonstrates the use of different arithmetic operators in C.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>

int main()
{
int A = 17, B = 6;

printf("The value of A is %d\n", A);


printf("The value of B is %d\n\n", B);

printf("Value of A+B = %d+%d is %d\n", A, B, A+B );


printf("Value of A-B = %d-%d is %d\n", A, B, A-B );
printf("Value of A*B = %d*%d is %d\n", A, B, A*B );
printf("Value of A/B = %d/%d is %d\n", A, B, A/B );
printf("Value of A%B = %d%%%d is %d\n", A, B, A%B );

return 0;
}

// end of program
The value of A is 17
The value of B is 6

Value of A+B = 17+6 is 23


Program Output
Value of A-B = 17-6 is 11
Value of A*B = 17*6 is 102
Value of A/B = 17/6 is 2
Value of A%B = 17%6 is 5

2|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Relational Operators

The following table shows all the relational operators supported by the C language. Assume variable A holds
10 and variable B holds 20 then:

Operator Description Example


Checks if the values of two operands are equal or not. If yes, then the
== (A == B) is not true.
condition becomes true.
Checks if the values of two operands are equal or not. If the values are
!= (A != B) is true.
not equal, then the condition becomes true.
Checks if the value of left operand is greater than the value of right
> (A > B) is not true
operand. If yes, then the condition becomes true.
Checks if the value of left operand is less than the value of right
< (A < B) is true.
operand. If yes, then the condition becomes true.
Checks if the value of left operand is greater than or equal to the value
>= (A >= B) is not true.
of right operand. If yes, then the condition becomes true.
Checks if the value of left operand is less than or equal to the value of
<= (A <= B) is true.
right operand. If yes, then the condition becomes true.

The relational operators can be used to compare integers and floating-point numbers, with operands of
mixed type allowed. Thus, 1 < 2.5 has the value 1, while 4.3 > 7 has the value 0.
The precedence of the relational operators is lower than that of the arithmetic operators; for example, i+j<k-
1 means (i+j)<(k-1). The relational operators are left associative.
The expression i < j < k is legal in C but doesn’t have the meaning that you might expect. Since < operator is
left-associative, this expression is equivalent to (i<j)<k. In other words, the expression first tests whether i is
less than j; the 1 or 0 produced by this comparison in then compared to k. The expression does not test
whether j lies between i and k. For that we use i<j&&j<k.

Example 5.2: Relational Operators in C


/* Example_5_2.c: Relational Operators in C
---------------------------------------------------------------------------
This program demonstrates the use of different relational operators in C.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>

int main()
{
int A = 10, B =20;

printf("The value of A is %d\n", A);


printf("The value of B is %d\n\n", B);

3|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

printf("Value of A==B = %d==%d is %d\n", A, B, A==B );


printf("Value of A!=B = %d!=%d is %d\n", A, B, A!=B );
printf("Value of A>B = %d>%d is %d\n", A, B, A>B );
printf("Value of A<B = %d<%d is %d\n", A, B, A<B );
printf("Value of A>=B = %d>=%d is %d\n", A, B, A>=B );
printf("Value of A<=B = %d<=%d is %d\n", A, B, A<=B );

return 0;
}

// end of program
The value of A is 10
The value of B is 20

Value of A==B = 10==20 is 0


Program Output Value of A!=B = 10!=20 is 1
Value of A>B = 10>20 is 0
Value of A<B = 10<20 is 1
Value of A>=B = 10>=20 is 0
Value of A<=B = 10<=20 is 1

Logical Operators

The following table shows all the logical operators supported by the C language. Assume variable A holds 1
and variable B holds 0 then:

Operator Description Example


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

Example 5.3: Logical Operators in C


/* Example_5_3.c: Logical Operators in C
---------------------------------------------------------------------------
This program demonstrates the use of different logical operators in C.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>

4|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

int main()
{
int A = 10, B =0;

printf("The value of A is %d\n", A);


printf("The value of B is %d\n", B);

printf("Value of A&&B = %d&%d is %d\n", A, B, A&&B);


printf("Value of A||B = %d|%d is %d\n", A, B, A||B);
printf("Value of !A =!%d is %d\n", A, !A);

return 0;
}

// end of program
The value of A is 10
The value of B is 0
Program Output
Value of A&&B = 10&0 is 0
Value of A||B = 10|0 is 1
Value of !A =!10 is 0

Bitwise Operators

The byte is the lowest level at which we can access data; there is no “bit” type, and we can't ask for an
individual bit. In fact, we cannot even perform operations on a single bit -every bit wise operator will be
applied to, at a minimum, an entire byte at a time. This means we will be considering the whole representation
of a number whenever we talk about applying a bitwise operator. Table below summarizes the bitwise
operators available in C.
The following table lists the bitwise operators supported by C language. Assume variable 'A' holds 0x60 (0011
1100) and variable 'B' holds 0x13 (0000 1101), then:

Operator Description Example


(A & B) = 0x12,
& Binary AND Operator copies a bit to the result if it exists in both operands.
i.e., 0000 1100
(A | B) = 0x61,
| Binary OR Operator copies a bit if it exists in either operand.
i.e., 0011 1101
(A ^ B) = 0x49,
^ Binary XOR Operator copies the bit if it is set in one operand but not both.
i.e., 0011 0001
Binary One's Complement Operator is unary and has the effect of 'flipping' (~A ) = ~(0x60),
~
bits. i.e,. -0111101
Binary Left Shift Operator. The left operands value is moved left by the A << 2 = 0x240
<<
number of bits specified by the right operand. i.e., 1111 0000
Binary Right Shift Operator. The left operands value is moved right by the A >> 2 = 0x15
>>
number of bits specified by the right operand. i.e., 0000 1111

5|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Example 5.4: Bitwise Operators in C


/* Example_5_4.c: Bitwise Operators in C
---------------------------------------------------------------------------
This program demonstrates the use of different bitwise operators in C.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>

int main()
{
int A = 0xC3; // 1100 0011
int B = 0x9F; // 1001 1111

printf("The value of A is 0x%X (1100 0011)\n", A);


printf("The value of B is 0x%X (1001 1111)\n\n", B);

printf("Value of A&B = 0x%X & 0x%X is 0x%X\n", A, B, A&B );


printf("Value of A|B = 0x%X | 0x%X is 0x%X\n", A, B, A|B );
printf("Value of A^B = 0x%X ^ 0x%X is 0x%X\n", A, B, A^B );
printf("Value of ~A = ~0x%X is 0x%X\n", A, ~A );
printf("Value of A>>2 = 0x%X>>2 is 0x%X\n", A, A>>2 );
printf("Value of A<<2 = 0x%X<<2 is 0x%X\n", A, A<<2 );

return 0;
}

// end of program
The value of A is 0xC3 (1100 0011)
The value of B is 0x9F (1001 1111)

Value of A&B = 0xC3 & 0x9F is 0x83


Program Output Value of A|B = 0xC3 | 0x9F is 0xDF
Value of A^B = 0xC3 ^ 0x9F is 0x5C
Value of ~A = ~0xC3 is 0xFFFFFF3C
Value of A>>2 = 0xC3>>2 is 0x30
Value of A<<2 = 0xC3<<2 is 0x30C

Bit Masking

Bitwise operators treat every bit in a word as a Boolean (two-valued) variable, apply a column-wise Boolean
operator, and generate a result. Unlike binary math, there is no carry or borrow and every column is
independent.

Turning Off Bits

6|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Bit-masking is using the bits in one word to “mask off” or select part of the range of bits in another word,
using the bitwise Boolean AND operator. The 1 bit in the “mask” select which bits we want to keep in the
word, and zero bits in the mask turn all the other corresponding bits to zeros. In other words, the one bit are
the “holes” in the mask that let the corresponding bits in the other word flow through to the result.

Turning On Bits

The opposite of masking (turning off bits) is “ORing in” bits, where we use the bitwise Boolean OR to “turn
ON” one or more bits in a word. We select a value that, when ORed with some other value, “turn ON” selected
bits and leaves the other bits unchanged.

Toggling Bits

Sometimes it does not really matter what the value is, but it must be made the opposite of what it currently
is. This can be achieved using the XOR (Exclusive OR) operation. XOR returns 1 if and only if an odd
number of bits are 1. Therefore, if two corresponding bits are one, the result will be a 0, but if only one of
them is 1, the result will be one. Therefore, inversion of the value of bits is done by XORing them with a 1.
If the original bid was 1, it returns 1 XOR 1 = 0. If the original bit was 0 it returns 0 XOR 1 = 1. Also note
that XOR masking is bit-safe, meaning that it will not affect unmasked bit because Y XOR 0 = Y, just like
an OR.

Example 5.5: Application of Bitwise Operators in C


/* Example_5_5.c: Application of Bitwise Operators in C
-----------------------------------------------------------------------
This example program shows the use of C Language Bitwise Operators.
Bitwise AND, OR, & XOR and assignment operators are used to demonstrate
their application. Results are displayed on computer screen.
-----------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include<stdio.h>

int main (){


// Turning Off Bits
printf("Turning Off Bits: Bitwise AND Operator\n");
int byte = 0xC3; // 1100 0011 (single byte register value)
printf("Register Value (1100 0011): 0x%X\n", byte);
int bitMask = 0xFC; // 1111 1100 (bit mask for bit 0 and 1)
byte &= bitMask; // Bitwise AND and Assignment Operator
printf("Bit Mask for AND (1111 1100): 0x%X\n", bitMask);
printf("Bitwise AND Value(1100 0000): 0x%X\n\n", byte);

// Turning On Bits
printf("Turning On Bits: Bitwise OR Operator\n");
byte = 0xC3; // 1100 0011 (single byte register value)

7|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

printf("Register Value (1100 0011): 0x%X\n", byte);


bitMask = 0x0C; // 0000 1100 (bit mask for bit 2 and 3)
byte |= bitMask; // Bitwise OR and Assignment Operator
printf("Bit Mask for OR (0000 1100): 0x0%X\n", bitMask);
printf("Bitwise OR Value (1100 1111): 0x%X\n\n", byte);

// Toggling Bits
printf("Toggling of Bits: Bitwise XOR Operator\n");
byte = 0xC3; // 1100 0011 (single byte register value)
printf("Register Value (1100 0011): 0x%X\n", byte);
bitMask = 0x0F; // 0000 1111 (bit mask for bit 0 through 3)
byte ^= bitMask; // Bitwise XOR and Assignment Operator
printf("Bit Mask for XOR (0000 1111): 0x0%X\n", bitMask);
printf("Bitwise XOR Value(1100 1100): 0x%X\n\n", byte);

return 0;
}

// end of program
Turning Off Bits: Bitwise AND Operator
Register Value (1100 0011): 0xC3
Bit Mask for AND (1111 1100): 0xFC
Bitwise AND Value(1100 0000): 0xC0

Turning On Bits: Bitwise OR Operator


Register Value (1100 0011): 0xC3
Program Output
Bit Mask for OR (0000 1100): 0x0C
Bitwise OR Value (1100 1111): 0xCF

Toggling of Bits: Bitwise XOR Operator


Register Value (1100 0011): 0xC3
Bit Mask for XOR (0000 1111): 0x0F
Bitwise XOR Value(1100 1100): 0xCC

Assignment Operators

The following table shows all the assignment operators supported by the C language.

Operator Description Example


Simple assignment operator. Assigns values from right C = A + B will assign the value of
=
side operands to left side operand A + B to C
Add and assignment operator. It adds the right operand to
+= C += A is equivalent to C = C + A
the left operand and assign the result to the left operand.
Subtract and assignment operator. It subtracts the right
-= operand from the left operand and assigns the result to the C -= A is equivalent to C = C - A
left operand.

8|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Multiply and assignment operator. It multiplies the right


*= operand with the left operand and assigns the result to the C *= A is equivalent to C = C * A
left operand.
Divide and assignment operator. It divides the left operand
/= with the right operand and assigns the result to the left C /= A is equivalent to C = C / A
operand.
Modulus and assignment operator. It takes modulus using
%= C %= A is equivalent to C = C % A
two operands and assigns the result to the left operand.
<<= Left shift and assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift and assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

Example 5.6: Assignment Operators in C


/* Example_5_6.c: Assignment Operators in C
---------------------------------------------------------------------------
This program demonstrate the use of different assignment operators in C.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>

int main()
{
int A = 15, B =10, C = 2;

printf("The value of A is %d\n", A);


printf("The value of B is %d\n", B);
printf("The value of C is %d\n\n", C);

printf("Value of C = A+B => C = %d+%d is %d\n", A, B, C=A+B);


printf("Value of C += A => C += %d is %d\n", A, C+=A);
printf("Value of C -= B => C -= %d is %d\n", B, C-=B);
printf("Value of C *= A => C *= %d is %d\n", A, C*=A);
printf("Value of C /= B => C /= %d is %d\n", B, C/=B);
printf("Value of C %= B => C %= %d is %d\n", B, C%=B);
printf("Value of C <<= 3 is 0x%X\n", C<<=3);
printf("Value of C >>= 2 is 0x%X\n", C>>=2);
printf("Value of C &= 0x6 is 0x%X\n", C&=0x6);
printf("Value of C |= 0xC is 0x%X\n", C|=0xC);
printf("Value of C ^= 0xF is 0x%X\n", C^=0xF);

return 0;

9|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

// end of program
The value of A is 15
The value of B is 10
The value of C is 2

Value of C = A+B => C = 15+10 is 25


Value of C += A => C += 15 is 40
Value of C -= B => C -= 10 is 30
Program Output Value of C *= A => C *= 15 is 450
Value of C /= B => C /= 10 is 45
Value of C %= B => C %= 10 is 5
Value of C <<= 3 is 0x28
Value of C >>= 2 is 0xA
Value of C &= 0x6 is 0x2
Value of C |= 0xC is 0xE
Value of C ^= 0xF is 0x1

Miscellaneous Operators

The following table shows the miscellaneous operators supported by the C language.

Operator Description Example


sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
?: Conditional Expression. If Condition is true ? then value X : otherwise value Y

Example 5.7: Miscellaneous Operators in C


/* Example_5_7.c: Miscellaneous Operators in C
---------------------------------------------------------------------------
This program demonstrate the use of different miscellaneous operators in C.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 29-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>

int main()
{
int A = 10, B =20;

printf("The value of A is %d\n", A);


printf("The value of B is %d\n\n", B);

10 | P a g e Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

printf("Value of sizeof(A) is %d (in bytes)\n", sizeof(A));


printf("Value of &A is 0x%X (32-bit memory location)\n", &A);
printf("Value of A>B?1:0 is %d\n", A>B);
printf("Value of A<B?1:0 is %d\n", A<B);

return 0;
}

// end of program
The value of A is 10
The value of B is 20

Program Output Value of sizeof(A) is 4 (in bytes)


Value of &A is 0x4EBFFBC8 (32-bit memory location)
Value of A>B?1:0 is 0
Value of A<B?1:0 is 1

Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has
a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20
because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the
bottom. Within an expression, higher precedence operators will be evaluated first.
Precedence Category Description Associativity
1 () [] . -> left-to-right
2 ++ -- + - ! ~ (type) * & sizeof Unary Operator right-to-left
3 */% Arithmetic Operator left-to-right
4 +- Arithmetic Operator left-to-right
5 << >> Shift Operator left-to-right
6 < <= > >= Relational Operator left-to-right
7 == != Relational Operator left-to-right
8 & Bitwise AND Operator left-to-right
9 ^ Bitwise XOR Operator left-to-right
10 | Bitwise OR Operator left-to-right
11 && Logical AND Operator left-to-right
12 || Logical OR Operator left-to-right
13 ?: Ternary Conditional Operator right-to-left
14 = += -= *= /= %= &= |= <<= >>= Assignment Operator right-to-left
15 , Comma left-to-right

11 | P a g e Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Task 5.1: Operators in C [6 points]


Write a simplest C Language program (like Example 1.1) and following
program fragments one by one. Run each program and write the output you
get. Also try to justify the results.
int i = 2, j = 3; Output:
1
printf("%d", (i + 12) % j);
Justification:

int i = 7, j = 8, k = 9; Output:
2
printf("%d", (i + 10) % k / j);
Justification:

int i = 7, j = 8; Output:
3 i *= j + 1;
printf("%d %d", i, j);
Justification:

int i = 1, j = 1, k = 1; Output:
4 i += j += k;
printf("%d %d %d", i, j, k);
Justification:

int i = 10, j = 5; Output:


5 printf("%d", i++ - ++j);
printf("\n%d %d", i, j);
Justification:

int i = 5; Output:
int j = i++ * 3 - 2;
6
int k = ++i * 3 - 2;
printf("%d %d %d", i, j, k);
Justification:

12 | P a g e Computer Programming-I Lab

You might also like