Lecture 3
Arithmetic in C
Most C programs
perform arithmetic calculations. The arithmetic operators used by C are as
follows.
C Operation
|
Arithmetic Operator
|
Algebraic Expression
|
C Expression
|
Addition
|
+
|
f+7
|
f+7
|
Subtraction
|
-
|
x-y
|
x-y
|
Multiplication
|
*
|
bm
|
b*m
|
Division
|
/
|
x/y
|
x/y
|
Modulus
|
%
|
r mod s
|
r%s
|
Table
1: Arithmetic Operators in C
The arithmetic
operators are all binary operators. The expression 3+7 contains binary
operator + and two operands 3 and 7. Integer division yields an integer
result. 17/4 is 4. The modulus operator means the remainder after a division
operation. 17%4 is 1.
Parentheses are used in C expressions in much the same manner as in algebra. a*(b+c)
means firstly b and c will be added and the sum of their addition will be
multiplied by a.
Rules of Operator Precedence
1.
Expressions or portion of
expressions contained in pairs of parentheses are evaluated first. In case of
nested parentheses, the inner most parentheses will be evaluated first.
2.
Multiplication, division and
modulus operations are evaluated next. If an expression contains several *, /
or % operations, evaluations proceed from left to right. These operators are on
the same level of precedence.
3.
Addition and subtraction are
evaluated last. If an expression
contains several + or - operations, evaluations proceed from left to right.
K m=(a+b+c+d+e)/5
K m=a+b+c+d+e/5
What are
the differences here?
K y=mx+c
How do
you write this expression in C? Any parenthesis required?
K z=pr%q+w/x-y
It is in
algebraic format. How will it be written in C? What will be the order of
precedence given by the C compiler while it is written in C format?
Decision Making: Relational Operators
Executable C expressions
either do actions (calculations or input or output) or make a decision.
For example, in a program where the user will put his mark and want to know if
he passed or failed, you will have to do three things-
1. Take the mark
from the user. Action
2. Compare the
mark with a predefined pass mark range. Decision
3. Provide that
decision to user. Action
The decision in
C is done by a control structure called if. It allows a program to make
a decision based on the truth or falsity of some statement of fact called condition. If the condition is true, the body
statement will be executed otherwise not- it will execute the next
statement after the if structure.
Conditions in if
structures are formed by using relational operators. The relational
operators have the same level of precedence and they associate left to right.
Only equality operators have a lower level of precedence than others and
they also associate left to right.
Relational Operators
|
Example of C Condition
|
Meaning of C Condition
|
==
|
x==y
|
x is equal to
y
|
!=
|
x!=y
|
x is not equal
to y
|
>
|
x>y
|
x is greater
than y
|
>=
|
x>=y
|
x is greater
than OR equal to y
|
<
|
x<y
|
x is less than
y
|
<=
|
x<=y
|
x is less than
OR equal to y
|
Table
2: Relational Operators in C
Note- There is a subtle difference between == and =. In case of =, it is
associated from right to left. a=b means the value of b is assigned to a.
Examples evades Clarity
#include
<stdio.h>
#include
<conio.h>
void
main(){
clrscr();
int num1, num2;
printf(“Enter two numbers to compare: ”);
scanf(“%d %d”, &num1, &num2);
if(num1 == num2)
printf(“%d and %d are equal”,
num1, num2);
if(num1 != num2)
printf(“%d and %d are not equal”,
num1, num2);
if(num1 < num2)
printf(“%d is less than %d”, num1,
num2);
if(num1 > num2)
printf(“%d is greater than %d”,
num1, num2);
if(num1 <= num2)
printf(“%d is less than or equal
to %d”, num1, num2);
if(num1 >= num2)
printf(“%d is greater than or
equal to %d”, num1, num2);
getch();
}
Keywords
In the above
program- int and if are keywords or reserved words
in C. You will have to be careful when you are writing program about keywords.
You cannot use them as variable names as C compiler processes keywords in a
different way. The full set of keywords that you cannot use in variable names
or identifiers are given here.
auto
|
break
|
case
|
char
|
const
|
continue
|
default
|
do
|
double
|
else
|
enum
|
extern
|
float
|
for
|
goto
|
if
|
inline
|
int
|
long
|
register
|
restrict
|
return
|
short
|
signed
|
sizeof
|
static
|
struct
|
switch
|
typedef
|
union
|
unsigned
|
void
|
volatile
|
while
|
_Bool
|
_Complex
|
_Imaginary
|
Qs
1. Every C program must have a function. What is its name?
2. What symbols are used to contain the body of the function?
3. With what symbol every statement ends with?
4. Which function displays information on the screen? Which header file
is required for that function?
5. Which function takes input from the user?
6. Which specifier is required to represent integer?
7. Which keyword is used to make decision?
8. What are used to make a program readable?
0 comments:
Post a Comment