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; x++ and ++x difference?
Float/Integer int a=5; float x; x=a/2; printf("x=%f",x); x=2 ? x 2.52
Operator Precedence ( ) * / % + -
#include Void main(void) { int a=10,b=3,c,d,e,f,g; float y,z; //basic operations c=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 Void main(void) { int a=10,b=3,c,d,e,f,g; float y,z; //compound operations c+=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 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 ) Sample functions pow(a,b)a power b (a b ) 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