Professional Documents
Culture Documents
OBJECTIVE:
This lab will introduce different operators available in C Language. At the end of this lab, you should be able
to:
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:
1
https://www.tutorialspoint.com/cprogramming/c_operators.htm
#include <stdio.h>
int main()
{
int A = 17, B = 6;
return 0;
}
// end of program
The value of A is 17
The value of B is 6
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:
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.
#include <stdio.h>
int main()
{
int A = 10, B =20;
return 0;
}
// end of program
The value of A is 10
The value of B is 20
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:
#include <stdio.h>
int main()
{
int A = 10, B =0;
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:
#include <stdio.h>
int main()
{
int A = 0xC3; // 1100 0011
int B = 0x9F; // 1001 1111
return 0;
}
// end of program
The value of A is 0xC3 (1100 0011)
The value of B is 0x9F (1001 1111)
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.
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.
#include<stdio.h>
// Turning On Bits
printf("Turning On Bits: Bitwise OR Operator\n");
byte = 0xC3; // 1100 0011 (single byte register value)
// 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
Assignment Operators
The following table shows all the assignment operators supported by the C language.
#include <stdio.h>
int main()
{
int A = 15, B =10, C = 2;
return 0;
// end of program
The value of A is 15
The value of B is 10
The value of C is 2
Miscellaneous Operators
The following table shows the miscellaneous operators supported by the C language.
#include <stdio.h>
int main()
{
int A = 10, B =20;
return 0;
}
// end of program
The value of A is 10
The value of B is 20
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
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 = 5; Output:
int j = i++ * 3 - 2;
6
int k = ++i * 3 - 2;
printf("%d %d %d", i, j, k);
Justification: