0% found this document useful (0 votes)
8 views

MT Module

The document discusses various operators in the C programming language. It covers arithmetic, assignment, comparison, logical, and bitwise operators. For each type of operator, it provides examples of common operators like addition, subtraction, assignment, equality, AND, and OR. It also provides the syntax and output for short code examples using each operator.

Uploaded by

Kalabet peanut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

MT Module

The document discusses various operators in the C programming language. It covers arithmetic, assignment, comparison, logical, and bitwise operators. For each type of operator, it provides examples of common operators like addition, subtraction, assignment, equality, AND, and OR. It also provides the syntax and output for short code examples using each operator.

Uploaded by

Kalabet peanut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

School of Information and Technology

C divides the operators into the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Bitwise operators

Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example

+ Addition Adds together two x+y


values

- Subtraction Subtracts one value x-y


from another

* Multiplication Multiplies two values x*y

/ Division Divides one value by x/y


another

% Modulus Returns the division x%y


remainder

++ Increment Increases the value of a ++x


variable by 1

-- Decrement Decreases the value of --x


a variable by 1
School of Information and Technology
Addition Example

#include <stdio.h>

int main() {
int x = 5;
int y = 3;
printf("%d", x + y);
return 0;
}

Subtraction Example

#include <stdio.h>

int main() {
int x = 5;
int y = 3;
printf("%d", x - y);
return 0;
}

Multiplication Example

#include <stdio.h>

int main() {
int x = 5;
int y = 3;
printf("%d", x * y);
return 0;
}

Division Example

#include <stdio.h>

int main() {
int x = 12;
int y = 3;
printf("%d", x / y);
return 0;
}
School of Information and Technology
Modulus Example

#include <stdio.h>

int main() {
int x = 5;
int y = 2;
printf("%d", x % y);
return 0;
}

Increment Example

#include <stdio.h>

int main() {
int x = 5;
printf("%d", ++x);
return 0;
}

Decrement Example

#include <stdio.h>

int main() {
int x = 5;
printf("%d", --x);
return 0;
}
School of Information and Technology

Assignment Operators
Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign


the value 10 to a variable called x:

int x = 10;

Ex.

#include <stdio.h>

int main() {

int x = 10;

printf("%d", x);

return 0;

The addition assignment operator (+=) adds a value to a variable:

#include <stdio.h>

int main() {

int x = 10;

x += 5;

printf("%d", x);

return 0;

}
School of Information and Technology

A list of all assignment operators:

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


School of Information and Technology

Example : =
#include <stdio.h>

int main() {
int x = 5;
printf("%d", x);
return 0;
}

Output :5

Example : +=
#include <stdio.h>

int main() {
int x = 5;
x += 3;
printf("%d", x);
return 0;
}
Output:8

Example : - =
#include <stdio.h>

int main() {
int x = 5;
x -= 3;
printf("%d", x);
return 0;
}
Output:2

Example: *=
#include <stdio.h>

int main() {
int x = 5;
x *= 3;
printf("%d", x);
return 0;
School of Information and Technology
}
Output:15

Example: /=
#include <stdio.h>

int main() {
float x = 5;
x /= 3;
printf("%f", x);
return 0;
}
Output:1.66667

Example: %=
#include <stdio.h>

int main() {
int x = 5;
x %= 3;
printf("%d", x);
return 0;
}
Output:2

Example : &=
#include <stdio.h>

int main() {
int x = 5;
x &= 3;
printf("%d", x);
return 0;
}
Output:1

Example: |=
#include <stdio.h>

int main() {
int x = 5;
x |= 3;
printf("%d", x);
return 0;
School of Information and Technology
}
Output: 7

Example: ^=
#include <stdio.h>

int main() {
int x = 5;
x ^= 3;
printf("%d", x);
return 0;
}
Output:6

Example: >>=

#include <stdio.h>

int main() {
int x = 5;
x >>= 3;
printf("%d", x);
return 0;
}
0utput: 0

Example: <<=

#include <stdio.h>
int main() {
int x = 5;
x <<= 3;
printf("%d", x);
return 0;
}
Output: 40
School of Information and Technology

Comparison Operators
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.

The return value of a comparison is either 1 or 0, which means true (1) or false
(0). These values are known as Boolean values, and you will learn more about
them in the Booleans and If..Else chapter.

In the following example, we use the greater than operator (>) to find out if 5 is
greater than 3:

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

printf("%d", x > y); // returns 1 (true) because 5 is greater than 3

return 0;

Output: 1
School of Information and Technology

A list of all comparison operators:

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

Example: ==

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

printf("%d", x == y); // returns 0 (false) because 5 is not equal to 3

return 0;

Output: 0
School of Information and Technology

Example: !=

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

printf("%d", x != y); // returns 1 (true) because 5 is not equal to 3

return 0;

Output: 1

Example: >

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

printf("%d", x > y); // returns 1 (true) because 5 is greater than 3

return 0;

Output:1
School of Information and Technology

Example: <

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

printf("%d", x < y); // returns 0 (false) because 5 is not less than 3

return 0;

Output:0

Example:>=

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

// Returns 1 (true) because five is greater than, or equal, to 3

printf("%d", x >= y);

return 0;

Output:1
School of Information and Technology

Example:<=

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

// Returns 0 (false) because 5 is neither less than or equal to 3

printf("%d", x <= y);

return 0;

Output: 1
School of Information and Technology

Logical Operators
You can also test for true or false values with logical operators.

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example

&& Logical Returns true if both x < 5 && x <


and statements are true 10

|| Logical or Returns true if one of the x < 5 || x < 4


statements is true

! Logical Reverse the result, !(x < 5 && x <


not returns false if the result 10)
is true

Example : &&

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

// Returns 1 (true) because 5 is greater than 3 AND 5 is less than 10

printf("%d", x > 3 && x < 10);

return 0;

Output: 1
School of Information and Technology

Example : ||

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

// Returns 1 (true) because one of the conditions are true (5 is greater than 3, but 5 is not
less than 4)

printf("%d", x > 3 || x < 4);

return 0;

Output:1

Example : !

#include <stdio.h>

int main() {

int x = 5;

int y = 3;

// Returns false (0) because ! (not) is used to reverse the result

printf("%d", !(x > 3 && x < 10));

return 0;

}
School of Information and Technology
Output:0

Sizeof Operator
The memory size (in bytes) of a data type or a variable can be found with the
sizeof operator:

#include <stdio.h>

int main() {

int myInt;

float myFloat;

double myDouble;

char myChar;

printf("%lu\n", sizeof(myInt));

printf("%lu\n", sizeof(myFloat));

printf("%lu\n", sizeof(myDouble));

printf("%lu\n", sizeof(myChar));

return 0;

Output:

Note that we use the %lu format specifier to print the result, instead of %d. It is
because the compiler expects the sizeof operator to return a long unsigned int
School of Information and Technology
(%lu), instead of int (%d). On some computers it might work with %d, but it is safer to
use %lu.

Booleans
Very often, in programming, you will need a data type that can only have one of
two values, like:

● YES / NO
● ON / OFF
● TRUE / FALSE

For this, C has a bool data type, which is known as booleans.

Booleans represent values that are either true or false.

Boolean Variables
In C, the bool type is not a built-in data type, like int or char.

It was introduced in C99, and you must import the following header file to use
it:

#include <stdbool.h>

A boolean variable is declared with the bool keyword and can only take the
values true or false:

bool isProgrammingFun = true;

bool isFishTasty = false;

Before trying to print the boolean variables, you should know that boolean
values are returned as integers:

● 1 (or any other number that is not 0) represents true


School of Information and Technology
● 0 represents false

Therefore, you must use the %d format specifier to print a boolean value:

#include <stdio.h>

#include <stdbool.h> // Import the boolean header file

int main() {

bool isProgrammingFun = true;

bool isFishTasty = false;

printf("%d\n", isProgrammingFun); // Returns 1 (true)

printf("%d", isFishTasty); // Returns 0 (false)

return 0;

Output:

However, it is more common to return a boolean value by comparing values and


variables.

Comparing Values and Variables


Comparing values is useful in programming, because it helps us to find answers
and make decisions.

For example, you can use a comparison operator, such as the greater than (>)
operator, to compare two values:
School of Information and Technology

#include <stdio.h>

int main() {

printf("%d", 10 > 9); // Returns 1 (true) because 10 is greater than 9

return 0;

Output:

You are not limited to only comparing numbers. You can also compare
boolean variables, or even special structures, like arrays (which you
will learn more about in a later chapter):

#include <stdio.h>

#include <stdbool.h> // Import the boolean header file

int main() {

bool isHamburgerTasty = true;

bool isPizzaTasty = true;

printf("%d", isHamburgerTasty == isPizzaTasty);

return 0;

Output:

1
School of Information and Technology
Remember to include the <stdbool.h> header file when working with
bool variables.

Real Life Example


Let's think of a "real life example" where we need to find out if a person is old
enough to vote.

In the example below, we use the >= comparison operator to find out if the age
(25) is greater than OR equal to the voting age limit, which is set to 18:

#include <stdio.h>

int main() {

int myAge = 25;

int votingAge = 18;

printf("%d", myAge >= votingAge); // Returns 1 (true), meaning 25 year olds


are allowed to vote!

return 0;

Output:

1
School of Information and Technology

Ex. 2

Output "Old enough to vote!" if myAge is greater than or equal to 18. Otherwise
output "Not old enough to vote.":

#include <stdio.h>

int main() {

int myAge = 25;

int votingAge = 18;

if (myAge >= votingAge) {

printf("Old enough to vote!");

} else {

printf("Not old enough to vote.");

return 0;

Output:

Old enough to vote.


School of Information and Technology

C If ... Else
Conditions and If Statements

You have already learned that C supports the usual logical conditions from

mathematics:

● Less than: a < b

● Less than or equal to: a <= b

● Greater than: a > b

● Greater than or equal to: a >= b

● Equal to a == b

● Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C has the following conditional statements:

● Use if to specify a block of code to be executed, if a specified condition is

true

● Use else to specify a block of code to be executed, if the same condition

is false

● Use else if to specify a new condition to test, if the first condition is

false

● Use switch to specify many alternative blocks of code to be executed


School of Information and Technology

The if Statement

Use the if statement to specify a block of code to be executed if a condition is

true.

Syntax

if (condition) {

// block of code to be executed if the condition is true

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the

condition is true, print some text:

#include <stdio.h>

int main() {

if (20 > 18) {

printf("20 is greater than 18");

return 0;

Output: 20 is greater than 18


School of Information and Technology
We can also test variables:

#include <stdio.h>

int main() {

int x = 20;

int y = 18;

if (x > y) {

printf("x is greater than y");

return 0;

Output: x is greater than y

In the example above we use two variables, x and y, to test whether x is greater than y
(using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we
print to the screen that "x is greater than y".
School of Information and Technology

The else Statement


Use the else statement to specify a block of code to be executed if the condition is false.

Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example

#include <stdio.h>

int main() {

int time = 20;

if (time < 18)


{
printf("Good day.");
}
else
{
printf("Good evening.");
}
return 0;
}

Output: Good evening.

In the example above, time (20) is greater than 18, so the condition is false.
Because of this, we move on to the else condition and print to the screen
"Good evening". If the time was less than 18, the program would print "Good
day".
School of Information and Technology

The else if Statement


Use the else if statement to specify a new condition if the first condition is
false.

Syntax

if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}

Example

#include <stdio.h>

int main() {
int time = 22;
if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day.");
} else {
printf("Good evening.");
}
return 0;
}

Output: Good evening.

In the example above, time (22) is greater than 10, so the first condition is
false. The next condition, in the else if statement, is also false, so we
move on to the else condition since condition1 and condition2 are both false -
and print to the screen "Good evening".

However, if the time was 14, our program would print "Good day."
School of Information and Technology
Another Example:

This example shows how you can use if..else to find out if a number is
positive or negative:

#include <stdio.h>

int main() {

int myNum = 10;

if (myNum > 0) {

printf("The value is a positive number.");

} else if (myNum < 0) {

printf("The value is a negative number.");

} else {

printf("The value is 0.");

return 0;

Output:The value is a positive number.


School of Information and Technology

Switch Statement
Instead of writing many if..else statements, you can use the switch
statement.

The switch statement selects one of many code blocks to be executed:

Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

This is how it works:

● The switch expression is evaluated once


● The value of the expression is compared with the values of each case
● If there is a match, the associated block of code is executed
● The break statement breaks out of the switch block and stops the
execution
● The default statement is optional, and specifies some code to run if
there is no case match

The example below uses the weekday number to calculate the weekday name:

#include <stdio.h>

int main() {

int day = 4;

switch (day) {

case 1:

printf("Monday");

break;
School of Information and Technology
case 2:

printf("Tuesday");

break;

case 3:

printf("Wednesday");

break;

case 4:

printf("Thursday");

break;

case 5:

printf("Friday");

break;

case 6:

printf("Saturday");

break;

case 7:

printf("Sunday");

break;

return 0;

Output: Thursday
School of Information and Technology

The break Keyword


When C reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

When a match is found, and the job is done, it's time for a break. There is no
need for more testing.

A break can save a lot of execution time because it "ignores" the execution of all
the rest of the code in the switch block.

The default Keyword


The default keyword specifies some code to run if there is no case match:

Note: The default keyword must be used as the last statement in the switch,
and it does not need a break.

#include <stdio.h>

int main()
{
int day = 4;

switch (day) {

case 6:
printf("Today is Saturday");
break;

case 7:
printf("Today is Sunday");
break;

default:
printf("Looking forward to the Weekend");
}

return 0;
}

Output: Looking forward to the Weekend


School of Information and Technology

Loops
Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more
readable.

While Loop
The while loop loops through a block of code as long as a specified condition is true:

Syntax

while (condition) {

// code block to be executed

In the example below, the code in the loop will run, over and over again, as
long as a variable (i) is less than 5:

#include <stdio.h>

int main() {

int i = 0;

while (i < 5) {

printf("%d\n", i);

i++;

return 0;

}
School of Information and Technology
Output:

Note: Do not forget to increase the variable used in the condition (i++),
otherwise the loop will never end!

The Do/While Loop


The do/while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat the
loop as long as the condition is true.

Syntax

do {

// code block to be executed

while (condition);
School of Information and Technology
The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:

#include <stdio.h>

int main() {

int i = 0;

do {

printf("%d\n", i);

i++;

while (i < 5);

return 0;

Output:

4
School of Information and Technology

For Loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) {

// code block to be executed

Statement 1 is executed (one time) before the execution of the code


block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been
executed.
School of Information and Technology
The example below will print the numbers 0 to 4:

#include <stdio.h>

int main() {

int i;

for (i = 0; i < 5; i++) {

printf("%d\n", i);

return 0;

Output:

4
School of Information and Technology
This example will only print even values between 0 and 10:

#include <stdio.h>

int main() {

int i;

for (i = 0; i <= 10; i = i + 2) {

printf("%d\n", i);

return 0;

Output:

10
School of Information and Technology

Nested Loops
It is also possible to place a loop inside another loop. This is called a nested
loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

#include <stdio.h>

int main() {
int i, j;

// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times

// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}

return 0;
}

Output:
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3
School of Information and Technology

C Break and Continue


You have already seen the break statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

This example jumps out of the for loop when i is equal to 4:

#include <stdio.h>

int main() {

int i;

for (i = 0; i < 10; i++) {

if (i == 4) {

break;

printf("%d\n", i);

return 0;

Output:

3
School of Information and Technology

Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.

This example skips the value of 4:

#include <stdio.h>

int main() {

int i;

for (i = 0; i < 10; i++) {

if (i == 4) {

continue;

printf("%d\n", i);

return 0;

Output:

0
1
2
3
4
5
6
7
8
9
School of Information and Technology
Break and Continue in While Loop
You can also use break and continue in while loops:
Break Ex.
#include <stdio.h>

int main() {
int i = 0;

while (i < 10) {


if (i == 4) {
break;
}
printf("%d\n", i);
i++;
}

return 0;
}
Output:
0
1
2
3
School of Information and Technology
Continue Ex.

#include <stdio.h>

int main() {

int i = 0;

while (i < 10) {

if (i == 4) {

break;

printf("%d\n", i);

i++;

return 0;

Output:

References:

https://www.w3schools.com/c/c_operators.php

https://www.w3schools.com/c/c_booleans.php

https://www.w3schools.com/c/c_conditions.php

https://www.w3schools.com/c/c_switch.php

https://www.w3schools.com/c/c_while_loop.php

https://www.w3schools.com/c/c_for_loop.php

https://www.w3schools.com/c/c_break_continue.php

You might also like