Topic3 C Programming
Topic3 C Programming
OPERATORS IN C
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C
language is rich in built-in operators and provides the following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Objectives:
This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other
operators one by one.
Contents
I. ARITHMETIC OPERATORS ......................................................................................... 1
II. RELATIONAL OPERATORS ...................................................................................... 2
III. LOGICAL OPERATORS.............................................................................................. 2
IV. BITWISE OPERATORS ............................................................................................... 3
V. ASSIGNMENT OPERATORS ...................................................................................... 5
VI. OPERATORS PRECEDENCE IN C ............................................................................ 5
EXERCISE 3 .............................................................................................................................. 7
I. ARITHMETIC OPERATORS
[See Activity 3.1] [See Activity 3.6]
Following table shows all the arithmetic operators supported by C language. Assume variable A holds 15 and
variable B holds 20, then:
Try the following example to understand all the arithmetic operators available in C programming language:
#include<stdio.h>
int main()
{
int a =21; int b=10; int c ;
c = a + b; printf("Line 1 - Value of c is %d\n", c );
c = a-b; printf("Line 2 - Value of c is %d\n", c );
c = a * b; printf("Line 3 - Value of c is %d\n", c );
c = a / b; printf("Line 4 - Value of c is %d\n", c );
c = a % b; printf("Line 5 - Value of c is %d\n", c );
c = a++; printf("Line 6 - Value of c is %d\n", c );
c = a--; printf("Line 7 - Value of c is %d\n", c );
return 0;
}
When you compile and execute the above program, it produces the following result:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
Following table shows all the relational operators supported by C language. Assume variable A holds 10 and
variable B holds 20, then:
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then:
Try the following example to understand all the logical operators available in C programming language:
#include<stdio.h>
int main()
{
int a =5;int b =20;int c ;
if( a && b )
printf("Line 1 - Condition is true\n");
if( a || b )
printf("Line 2 - Condition is true\n");
/* lets change the value of a and b */
a =0; b =10;
if( a && b )
printf("Line 3 - Condition is true\n");
else
printf("Line 3 - Condition is not true\n");
if(!(a && b))
printf("Line 4 - Condition is true\n");
}
When you compile and execute the above program, it produces the following result:
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60
and variable B holds 13, then:
Try the following example to understand all the bitwise operators available in C programming language:
#include<stdio.h>
main()
{
unsigned int a =60; /* 60 = 0011 1100 */
unsigned int b =13; /* 13 = 0000 1101 */
int c =0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c =~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a <<2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >>2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
return 0;
}
When you compile and execute the above program, it produces the following result:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
V. ASSIGNMENT OPERATORS
[See Activity 3.3]
There are following assignment operators supported by C language:
There are few other important operators including sizeof and ? : supported by C Language.
Operator precedence determines the grouping of terms in an expression. This affects how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has
higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has 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.
Try the following example to understand the operator precedence available in C programming language:
#include<stdio.h>
main()
{
int a =20;int b =10;int c =15;int d =5;int e;
e =(a + b)* c / d;// ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e =((a + b)* c)/ d;// (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n", e );
e =(a + b)*(c / d);// (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a +(b * c)/ d;// 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n", e );
return0;
}
When you compile and execute the above program, it produces the following result:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
[See Activity 3.4]
EXERCISE 3
Exercise 3.2: Given a time in seconds (integer), print to the screen the corresponding time in hours, minutes
and seconds. The output will be formatted like: “XXXX seconds is equivalent to XX hours, XX minutes and
XX seconds”.
Exercise 3.3: Write a C code that switches the values of two variables A and B and prints the result on the screen.
How many variables do you need?
Exercise 3.4: Write a program that reads in an integer and prints out the given integer in decimal, octal and
hexadecimal formats
Exercise 3.5: Write a program that reads in a temperature expressed in Celsius (Centigrade) and displays the
equivalent temperature in degrees Fahrenheit.
Exercise 3.6: Create a new program that prompts a user for numbers and determines total revenue using the
following formula: Total Revenue = Price * Quantity.
Exercise 3.7: Build a new program that prompts a user for data and determines a commission using the following
formula: Commission = Rate * (Sales Price – Cost).
Exercise 3.8: a) Given a = 5, b = 1, x = 10, and y = 5, create a program that outputs the result of the formula f =
(a b)(x y) using a single printf() function.
b) Create a program that uses the same formula above to output the result; this time, however, prompt the user
for the values a, b, x, and y. Use appropriate variable names and naming conventions.
Exercise 3.9: Create a program that prompts a user for her name. Store the user’s name using the scanf() function
and return a greeting back to the user using her name.
Exercise 3.10: Write a program to read in a three digit number and produce output like
3 hundreds
4 tens
7 units
For an input of 347. There are two ways of doing this. Can you think of both of them? Which do you think is
the better?