INDIAN INSTITUTE OF INFORMATION TECHNOLOGY, DESIGN AND MANUFACTURING, KANCHEEPURAM
Problem Solving and Programming
(CS1000)
Basics of Computers and C
Dr. Jaishree Mayank
Assistant Professor
Department of Computer Sc. and Engg.
Expression
Variables and constants linked with operators
• Arithmetic expressions
Uses arithmetic operators
Can evaluate to any value
• Assignment expression
Uses assignment operators
Evaluates to value depending on assignment
• Logical expressions
Uses relational and logical operators
Evaluates to 1 or 0 (true or false) only
Arithmetic Operators
Binary operators distance = rate * time ;
Addition: + netIncome = income - tax ;
Subtraction: – speed = distance / time ;
Division: /
area = PI * radius * radius;
Multiplication: *
y = a * x * x + b*x + c;
Modulus: %
quotient = dividend / divisor;
Unary operators remain =dividend % divisor;
Plus: +
Minus: – % can be used only with integer operands
Arithmetic Operators
• In decreasing order of priority
1.Parentheses :: ( )
2.Unary minus :: –5
3.Multiplication, Division, and Modulus
4.Addition and Subtraction
• For operators of the same priority, evaluation is from left to
right as they appear
• Parenthesis may be used to change the precedence of
operator evaluation
Examples:Arithmetic Expressions
• a + b * c –d / e a + (b * c) – (d / e)
• a * –b + d % e –f a * (– b) + (d % e) – f
• a –b + c + d (((a – b) + c) + d)
• x*y*z ((x * y) * z)
• a+b+c*d*e (a + b) + ((c * d) * e)
Type of Values
Integer Arithmetic
If all operands of an operator are integer (int variables or integer
constants), the value is always integer
When the operands in an arithmetic expression are integers, the
expression is called integer expression and the operation is called integer
arithmetic
Type of Values
Real Arithmetic
Arithmetic operations involving only real or floating-point operands.
• Since floating-point values are rounded to the number of significant digits
permissible, the final value is an approximation of the final result.
1.0 / 3.0 * 3.0 will have the value 0.99999 and not 1.0
• The modulus operator cannot be used with real operands.
Mixed Arithmetic
If at least one operand is real, the value is real
Assignment Expression
Uses the assignment operator (=)
General syntax:
variable_name = expression
variable_name1=varable_name2
Left of = is called l-value, must be a modifiable variable
Right of = is called r-value, can be any expression
Data types of RHS must be compatible with LHS
Examples:
velocity = 20
b = 15; temp = 12.5
A = A + 10
v=u+f*t
s = u * t + 0.5 * f * t * t
Types of l-value and r-value
Usually should be the same
If not, the type of the r-value will be internally converted to the type of the l-value, and then
assigned to it
Example1:
float a;
a = 2*3;
Type of r-value is int and the value is 6
Type of l-value is float, so stores 6.0
Example2
int a;
a = 2*3.2;
Type of r-value is float/double and the value is 6.4
Type of l-value is int, so internally converted to 6
So a stores 6
Sample code-1 (Compile and modify, uncomment or comment whenever required)
*Performing addition operation on variables and
constants*
#include<stdio.h> printf("After modifying the variables\n\n");
#define PI 10 a=a+2;
void main() b=b+3;
{ c=c+1;
d=d+2;
int a=2, b=3; e=e+3;
const int c=10; f=f+5;
char1='d';
float d=5.4, e=6.8; char2='e';
const float f=7.2; char3='f';
char char1='c', char2='v'; //PI=PI+1;
const char char3='g'; printf("PI %d\n", PI);
printf("integer values %d, %d, and %d\n", a, b, c);
printf("integer values %d, %d, and %d\n\n", a, b, c); printf("float values %f, %f, and %f\n", d, e, f);
printf("float values %f, %f, and %f\n\n", d, e, f); printf("char values %c, %c, and %c\n", char1, char2, char3);
printf("char values %c, %c, and %c\n\n", char1, }
char2, char3);
Sample code-2
/*Use of scanf with different format*/
#include<stdio.h>
void main()
{
int x =5;
float y=4.5;
char z1='c';
printf("Enter one integer, one float and one character using tabspace\n");
scanf("%d\t%f\t%c",&x,&y,&z1);
printf("Entered1 values are: %d,%f and %c\n",x,y,z1);
printf("Enter the one integer, one float and one character using enter\n\n");
scanf("%d\n%f\n%c",&x,&y,&z1);
printf("Entered2 values are: %d,%f and %c\n",x,y,z1);
printf("Enter the one integer, one float and one character\n\n");
scanf("%d%f%c",&x,&y,&z1);
printf("Entered3 values are: %d,%f and %c\n",x,y,z1);
Sample code-2(Contnd)
printf("Enter the one integer, one float and one character with space\n\n");
scanf("%d %f %c",&x,&y,&z1);
printf("Entered4 values are: %d,%f and %c\n",x,y,z1);
printf("Enter the one integer, one float and one character with space\n\n");
scanf("%d%f %c",&x,&y,&z1);
printf("Entered5 values are: %d,%f and %c\n",x,y,z1);
}
Sample code-3
*Use of Macro and use of value with %d and %f*
/*Use other values, complie and analyse*/
#include<stdio.h>
#define U (x+2)
void main()
{
int I =U;
printf("I is %d\n", I);
printf("%8.6d\n%6d ",123, 123);
printf("%.2f\n%20f ",123.5689, 123.5689);
}
Sample code-4
#include<stdio.h>
/*Change the values or expression and analyse the output*/
void main() printf("Evaluated value %d\n", div1);
{ printf("Evaluated value %f\n", div2);
int i1=4, i2=10; printf("Evaluated value %d\n", mul1);
float f1=4.0, f2=10.0; printf("Evaluated value %.2f\n", mul2);
int div1, mul1; }
float div2, mul2;
printf("Evaluated value %.2f\n", (1/3.0*3.0));
div1= f2/f1;
div2=f2/f1;
mul1=3.2*2;
mul2=2*6.2
Sample code-4
Thanks
Any Questions?