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

Chapter 3 C Programming

Chapter 3 C Programming

Uploaded by

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

Chapter 3 C Programming

Chapter 3 C Programming

Uploaded by

raja hello
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 3

Operators and Expressions


EXPRESSIONS

► The combination of operators and operands is said


to be an expression.
► ARITHMETIC EXPRESSIONS
► An arithmetic expression is a combination of variables,
constants, and operators arranged as per the syntax of the
language.
▪ Eg. 1) a = x + y;
► EVALUATION OF EXPRESSIONS
► Expressions are evaluated using an assignment statement of
the form
► variable = expression;
► Eg:1) x = a * b – c;
▪ 2) y = b / c * a;

Evaluation
Program
of expressions
main()
{
float a, b, c, y, x, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – (b / (3 + c) * 2) – 1;
printf(“x = %f \n”,x);
printf(“y = %f \n”,y);
printf(“z = %f \n”,z);
}
OUTPUT
x = 10.000000
y = 7.000000
z = 4.000000
► PRECEDENCE OF ARITHMETIC OPERATORS
▪ An arithmetic expression without parenthesis
will be evaluated from left to right using the
rule of precedence of operators. There are two
distinct priority levels of arithmetic operators in
C.
▪ High priority * / %
▪ Low priority + -
EXAMPLE
9 – 12 / 3 + 3 * 2 - 1

(1) (2)

6
(3)
5
(4)

11

(5)

10
TYPE CONVERSION IN
EXPRESSIONS
►C permits the mixing of constants and
variables of different types in an expression.
► If the operands are of different types, the
lower type is automatically converted to
higher type before the operation proceeds.
The result is of the higher type. This
automatic conversion is known as implicit
type conversion.
Rules for Conversion
► All short and char are automatically converted to
int;
► If one of the operands is long double, the other
will be converted to long double and the result will
be in long double.
► Else, If one of the operands is double, the other
will be converted to double and the result will be
in double.
► Else, If one of the operands is float, the other will
be converted to float and the result will be in float.
Contd..
► Else, If one of the operands is unsigned long int,
the other will be converted to unsigned long int
and the result will be in unsigned long int.
► Else, if one of the operands is long int and other is
unsigned int, then:
▪ (a) if unsigned int can be converted to long int, the
unsigned int operand will be converted as such and the
result will be long int.
▪ (b) else, both the operands will be converted to
unsigned long int and the result will be unsigned long
int.
Contd..
► Else,If one of the operands is long int, the
other will be converted to long int and the
result will be in long int.
► Else, If one of the operands is unsigned int,
the other will be converted to unsigned int
and the result will be in unsigned int.
Casting a Value
► C performs type conversion automatically. However, there
are instances when we want to force a type conversion in
a way that is different from the automatic conversion.
▪ Eg: 1) ratio = female_number / male_number

► Since female_number and male_number are declared as


integers the ratio would represent a wrong figure. Hence it
should be converted to float. This type of conversion is
called as casting a value or explicit conversion.
▪ ratio = (float) female_number / male_number

► The general form of a cast is:


► (type-name) expression
► Eg. 1) x=(int) 7.5; 2) a =(int) 21.3/(int) 4.5;
► 3) x = (int) (y+0.5);
MATHEMATICAL FUNCTIONS
► Mathematical functions such as sqrt, cos,
log etc., are the most frequently used ones.
► To use the mathematical functions in a C
program, we should include the line
►#include<math.h>

► in the beginning of the program.


Contd..
Function Meaning
► Trignometric
▪ acos(x) Arc cosine of x
▪ asin(x) Arc sine of x
▪ atan(x)
▪ atan2(x,y)
▪ cos(x)
▪ sin(x)
▪ tan(x) Other functions refer notes.
Example for mathematicals
functions
#include <stdio.h>
#include <math.h>
int main(void) {
double age = 34.38;
int floor_age = floor(age);
printf("FLOOR = %d", floor_age);
printf("\n");
int ceil_age = ceil(age);
printf("CEILING = %d", ceil_age);
}
Mathematical functions
► pow(age, 2)
► sqrt(age)
► fabs(a)

You might also like