lab 3 variables & expressions. basic operations addition + subtraction – multiplication *...
TRANSCRIPT
Lab 3
Variables & Expressions
Basic Operations
Addition + Subtraction – Multiplication * Division / Modulus %
Compound Operations
x += y; x = x + y; x -= y; x = x - y; x *= y; x= x * y; x /= y; x = x / y; x++; x = x + 1; x--; x = x - 1;
xx++++ and and ++++xxdifference?difference?
Float/Integer
int a=5;float x;x=a/2;printf("x=%f",x);
x=2 ??
55------22
xx 2.52.522
Operator Precedence
( ) ++ -- * / % + -
#include <stdio.h>Void main(void){int a=10,b=3,c,d,e,f,g; float y,z;//basic operationsc=a+b;d=a-b;e=a*b;f=a/b;g=a%b;Printf(“C=%d\tD=%d\nE=%d\tF=%d\nG=%d\n\
n”,c,d,e,f,g);}
Example 1
#include <stdio.h>Void main(void){int a=10,b=3,c,d,e,f,g; float y,z;//compound operationsc+=a;d-=b;e*=a;f/=b;g++;Printf(“C=%d\tD=%d\nE=%d\tF=%\ d\nG=%d\n\n”,c,d,e,f,g);}
Example 2
#include <stdio.h>Void main(void){int a=10,b=3,c,d,e,f,g; float y,z;//integer/ float: is the result correct ?y=b/2;Printf(“Y=%f\n”,y);//what is the order of the
operations ?Z=4+5*3;Printf(“z=%f\n\n”,z);}
Example 3
Mathematical Functions Use math.h (#include <math.h>) Sample functions
pow(a,b) a power b (ab) sin(a) sine of a cos(a) cosine of a sqrt(a) square root of a log(a) Natural logarithm of a log10(a) base-10 logarithm of a