0% found this document useful (0 votes)
46 views24 pages

ESC101: Introduction To Computing: Operators and Expressions

This document discusses operators and expressions in C programming. It covers binary operations like addition, subtraction, multiplication and division that can be performed on integer, float and double data types. It also discusses unary operators and precedence of operators. The document explains the division and remainder operators in detail and their usage with integer and float operands. It covers type conversions between data types and formatting of output.

Uploaded by

Prawan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views24 pages

ESC101: Introduction To Computing: Operators and Expressions

This document discusses operators and expressions in C programming. It covers binary operations like addition, subtraction, multiplication and division that can be performed on integer, float and double data types. It also discusses unary operators and precedence of operators. The document explains the division and remainder operators in detail and their usage with integer and float operands. It covers type conversions between data types and formatting of output.

Uploaded by

Prawan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

ESC101: Introduction to

Computing
Operators and Expressions
9/19/2014 Esc101, Programming 1
Binary Operations
Operate on int, float, double (and char)

9/19/2014 2 Esc101, Programming
Op Meaning Example Remarks
+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4 Integer div.
9.1/2.0 is 4.55 Real div.
% Remainder 9%2 is 1 Only for int
Unary Operators
Operators that take only one
argument (or operand)
-5
+3.0123
-b
Observe that + and have two
purposes
Meaning depends on context
This is called overloading
9/19/2014 3 Esc101, Programming
The / operator
When both (left and right) operand
of / are of type int
the result is the integral part of the
real division
The result is of type int
Examples
9/4 is 2
1/2 is 0
9/19/2014 4 Esc101, Programming
The / operator
When at least one (left or right or
both) operands of / are of type float
the result is the real division
The result is of type float
Examples
9/4.0 is 2.25
1.0/2 is 0.5, so is 1/2.0 and 1.0/2.0
9/19/2014 5 Esc101, Programming
The % operator
The remainder operator % returns
the integer remainder of the result
of dividing its first operand by its
second.
Both operands must be integers.
Defined only for integers
4%2 is 0
31%4 is 3
9/19/2014 6 Esc101, Programming
Divison(/) and Remainder(%)
Second argument can not be 0
Run time error
For integers a and b (b0), / and
% have the following relation
a = (a/b)*b + (a%b)
If a or b or both are negative, the
result of / and % is system
dependent.
9/19/2014 7 Esc101, Programming
Type of Arithmetic Expr
Type of (result of) arithmetic expr
depends on its arguments
For binary operator
If both operands are int, the result is
int
If one or both operand are float, the
result is float
For unary operator
Type of result is same as operand type
9/19/2014 8 Esc101, Programming
# include <stdio.h>
int main() {
float Centigrades;
float F;
Centigrades=50;
F = ((9*Centigrades)/5) + 32;
printf(The temperature );
printf( %f, Centigrades);
printf(Celsius equals);
printf(%f, F);
printf(Fahrenheit);
return 0;
}

temp_conversion.c
Compile and Run
The temperature Celsius equals 122.000000 50.000000
C F
?? ?? 50.000 ??
Microprocessors represent real
numbers using finite precision,
i.e., using limited number of digits
after decimal point.

Typically uses scientific notation:
12.3456789 represented as
1.23456789E+1. Bit more later.


122.0000
%f signifes that the corresponding
variable is to be printed as a real
number in decimal notation.
Fahrenheit %
Operator Precedence
More than one operator in an
expression
Evaluation is based on precedence
Parenthesis () have the highest
precedence
Precedence order for some
common operators coming next
9/19/2014 10 Esc101, Programming
Operator Precedence
9/19/2014 11 Esc101, Programming
Operators Description Associativity
(unary) + - Unary plus/minus Right to left
* / % Multiply, divide, remainder Left to right
+ - Add, subtract Left to right
< > >= <= less, greater comparison Left to right
== != Equal, not equal Left to right
= Assignment Right to left
I
N
C
R
E
A
S
I
N
G
LOW
HIGH
Operator Precedence
9/19/2014 12 Esc101, Programming
What is the value assigned to x?
x = -5*4/2*3+-1*2;
Always use parenthesis to define
precedence. It is safer and easier
to read.
Avoid relying on operator
precedence. Can give absurd
results if not used correctly.
Consult any textbook to know
more about precedence.
-32
Type Conversion
(Type casting)
Converting values of one type to
another.
Example: int to float and float to int
(also applies to other types)
Can be implicit or explicit
int k =5;
float x = k; // implicit conversion to 5.0
float y = k/10; // y is assigned 0.0
float z = ((float) k)/2; // Explicit conversion

Loss of Information!
Type conversion may result in loss
information.
Larger sized type (e.g. float )
converted to smaller sized type (e.g.
int) is undefined.
Smaller sized type (e.g. int)
converted to larger sized type (e.g.
float) may also give unexpected
results. Take care!

9/19/2014 14 Esc101, Programming
float to int: type conversion
(result ok)
#include<stdio.h>
int main() {
float x; int y; /* define two variables */
x=5.67;
y = (int) x; /* convert float to int */
printf(%d, y);
return 0;
}
Output : 5
float x;

(int) x;

converts the
real value
stored in x
into an
integer. Can
be used
anywhere an
int can.
# include <stdio.h>
int main() {
float x; int y;
x = 1.0E50;
y = (int) x;
printf(%d, y);
return 0;
}
float to int type conversion
(not ok!)
float is a larger box, int is
a smaller box. Assigning a
float to an int may lead to
loss of information and
unexpected values.



Output:
-2184748364
The floating
point number
1E50 is too
large to fit in
an integer
box.

Careful when
converting from
a `larger type to
smaller type.
Undefined.
int to float (take care!)
# include <stdio.h>
int main() {
int y;
y = 1000001;
printf(%f, (float) y);
return 0; }
Output:
1000001.000000
Result is correct
# include <stdio.h>
int main() {
int y;
y = 10000009;
printf(%f\n, (float) y);
printf( %d, y);
return 0; }
Output:
10000008.000000
10000009
Result is not correct.
Information is lost.
float r,h;
scanf("%f", &r);
scanf("%f", &h);
printf("Volume is %f\n",
1/3*3.14*r*r*h);
Program Example
9/19/2014 18 Esc101, Programming
Volume of a cone =
1
3

2

Input:
10
3


Output?
0
Where did
my Icecream
go?
1/3 evaluates to 0
1.0/3.0 evaluates to 0.3333
Remember: use floats for real division

char data type in C
Basic facts
Characters in C are encoded as
numbers using the ASCII encoding
ASCII : American Standard Code for
Information Interchange
Encodings of some of the common
characters:
'A' is 65, 'B' is 66, 'C' is 67 ... 'Z' is 90
'a' is 97, 'b' is 98 ... 'z' is 122
'0' is 48, '1' is 49 ... '9' is 57
9/19/2014 19 Esc101, Programming
char data type in C
Range: 0 to 255
You should NOT try to remember
ASCII values
Encoding/programming languages
provide alternatives to use them
C treats characters as integers
corresponding to their ASCII value.
While displaying with %c
placeholder, the ASCII value is
converted to its corresponding
character.

9/19/2014 20 Esc101, Programming
char data type in C
Interconversion between character
and integer datatypes can be
exploited to write programs.

9/19/2014 21 Esc101, Programming
printf("%d\n", 'A');
printf("%d\n", '7');
printf("%c\n", 70);
printf("%c\n", 321);
Output:
65
55
F
321 is outside range!
What do you think will
be the output of
printf("%c\n",321);
Try it out
char data type in C
Interconversion between character
and integer datatypes can be
exploited to write programs.

9/19/2014 22 Esc101, Programming
printf("%c\n", C+5);
printf("%c\n", D - A + a );
printf("%d\n", 3 + 2);
Output:
H
d
53
Placeholder determines the output.
Use with caution.
Avoid arithmetic operation such as * and / on characters.
Common Mistake: Incorrect data type of placeholder.
Formatting Output of a
Program (int)
When displaying an int value, place a
number between the % and d which will
specify the number of columns to use for
displaying the int value (such as %5d).
9/19/2014 23 Esc101, Programming
int x = 2345, y=123;
printf("%d\n",x); //Usual

printf("%6d\n",x); //Display using 6 columns

printf("%6d\n",y); //Note: Right aligned

printf("%2d\n",x); //Less columns, same as %d
Output
2345
2345
123
2345

Formatting Output of a
Program (float)
Format placeholder id is %n.mf where
n is the total field width (both before and after
the decimal point), and
m is the number of digits to be displayed after
the decimal
point.
9/19/2014 24 Esc101, Programming
float pi = 3.141592;
printf("%f\n",pi); //Usual

printf("%6.2f\n", pi); //2 decimal

printf("%.4f\n",pi); //4 decimal

// Note rounding off!
Output

3.141592
3.14
3.1416

You might also like