You are on page 1of 31

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Expressions and Operators

Dr. Muhammad Yousaf Hamza


Expressions and Operators
• In the most general sense, a statement is a part of your
program that can be executed.
• An expression is a statement.
• Examples:
x = 4;
x = x + 1;
printf("%d",x);

• The expressions are formed by data and operators


• An expression in C usually has a value
– except for the function call that returns void. (later)

Dr. Muhammad Yousaf Hamza


Arithmetic Operators
Operator Symbol Action

Addition + Adds operands x+y


Subtraction - Subtracts from first x-y
Negation - Negates operand -x
Multiplication * Multiplies operands x*y
Division / Divides first by second x/y
(integer quotient)
Modulus % Remainder of divide op x%y

• (x % y) gives the remainder when x is divided by y


• remainder= x%y; (ints only)
Dr. Muhammad Yousaf Hamza
The Use of Modulus
• int x;
• // Various cases
• x = 6%2 // x =
• x = 7%2 // x =
• Suppose num is any even number then
• x = num%2 // x =
• Suppose num is any odd number then
• x = num%2 // x =
• // Some other examples
• x = 63%10 // x =
• x = 100 %7 // x =

Dr. Muhammad Yousaf Hamza


The Use of Modulus
• int x;
• // Various cases
• x = 6%2 // x = 0
• x = 7%2 // x = 1
• Suppose num is any even number then
• x = num%2 // x = 0
• Suppose num is any odd number then
• x = num%2 // x = 1
• // Some other examples
• x = 63%10 // x = 3
• x = 100 %7 // x = 2

Dr. Muhammad Yousaf Hamza


The Use of Modulus
include<stdio.h>
int main()
{
int num=12;
int digit1,digit2;
digit1=num%10; // digit1 = 2
digit2=num/10; // digit2 = 1
printf(“First digit is = %d ”,digit1);
printf(“\nSecond digit is =%d”,digit2);
getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


Assignment Operator
• The assignment operator =
x=3
– It assigns the value of the right hand side (rhs) to
the left hand side (lhs).
– The value is the value of the rhs.
• For example:
x = ( y = 3 ) +1; /* y is assigned 3 */
/* the value of (y=3) is 3 */
/* x is assigned 4 */

Dr. Muhammad Yousaf Hamza


Compound Assignment Operator
• Often we use “update” forms of operators
x=x+1, x=x*2, ...
• C offers a short form for this:
Operator Equivalent to:
x+=y x=x+y
x *= y x=x*y
y -= z + 1 y = y - (z + 1)
a /= b a=a/b
x += y / 8 x = x + (y / 8)
y %= 3 y=y%3
Dr. Muhammad Yousaf Hamza
// demonstrates arithmetic assignement operators
#include <stdio.h>
int main()
{
int ans = 27;
ans += 10; //same as: ans = ans + 10;
printf(" %d, ",ans);
ans -= 7; //same as: ans = ans - 7;
printf(" %d, ",ans);
ans *= 2; //same as: ans = ans * 2;
printf(" %d, ",ans);
ans /= 3; //same as: ans = ans / 3;
printf(" %d, ",ans);
ans %= 3; //same as: ans = ans % 3;
printf(" %d, \n",ans);
getchar(); return 0;
} Dr. Muhammad Yousaf Hamza
Increment and Decrement Operators

Dr. Muhammad Yousaf Hamza


Increment and Decrement
• Increment and decrement operators.
– Increment: ++ It increases the value by 1
i = 7; // i is a variable name
++i; // (i = i + 1 or i + = 1). It increases the value of i by 1
i = 7;
i++;

–Decrement: -- (similar to ++) It decreases the value by 1


i = 8;
--i; // --i is the same as : (i = i – 1 or i - = 1).
i = 8;
i--;

Dr. Muhammad Yousaf Hamza


Increment and Decrement
Pre-fix and Post-fix
• ++i means increment i then use it
• i++ means use i then increment it
int i= 6;
printf ("%d\n",i++);

Note this important difference


int i= 6;
printf ("%d\n",++i);

int i= 6;
printf ("%d\n",i = i+1);
int i= 6;
printf ("%d\n",i+1);

All of the above also applies to --


Dr. Muhammad Yousaf Hamza
Increment and Decrement
Pre-fix and Post-fix
• ++i means increment i then use it
• i++ means use i then increment it
int i= 6;
printf ("%d\n",i++); /* Prints 6 sets i to 7 */

Note this important difference


int i= 6;
printf ("%d\n",++i); /* prints 7 and sets i to 7 */

int i= 6;
printf ("%d\n",i = i+1); /* prints 7 and sets i to 7 */
int i= 6;
printf ("%d\n",i+1); /* prints 7 and i remains 6 */

All of the above also applies to --


Dr. Muhammad Yousaf Hamza
Increment and Decrement
#include<stdio.h>
Pre-fix and Post-fix
int main()
{
int a = 7, b = 20, c, d;
c = a++;
printf("%d", c);
printf("\n%d",a);

d = ++b;
printf("\n%d", d);
printf("\n%d",b);

getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Type Conversion
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b; // 5.00
printf("\n%f",c); // 5.00
getchar();
return 0;
}
Actually 23/4 = 5.75, but here output is 5.00 In order to have
correct answer (with decimal value), we need type casting.
Dr. Muhammad Yousaf Hamza
Type Conversion
• C allows for conversions between the basic types, implicitly or
explicitly. It is also called casting.
• A cast is a way of telling one variable type to temporarily look
like another.
• Explicit conversion uses the cast operator.
• Example :
int x=10;
float y, z=3.14;

y=(float) x; /* y=10.0 */

x=(int) z; /* x=3 */ OR x = z, both are OK


Dr. Muhammad Yousaf Hamza
Casting of Variables
By using (type) in front of a variable we tell
the variable to act like another type of variable.
We can cast between any type usually.

Dr. Muhammad Yousaf Hamza


Casting of Variables
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b;
printf("\n%f",c); // 5.00
Cast ints a and b to be float
c= (float)a/(float)b;
// c = (float)a/b; or c= a/(float)b; are also same.

printf("\n%f",c); // 5.75
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
More types: const
const means a variable which doesn't vary – useful for
physical constants or things like pi or e
– You can also declare variables as being constants
– Use the const qualifier:
const double pi=3.1415926;
const int maxlength=2356;
const int val=(3*7+6)*5; #include<stdio.h>
int main()
•(scientific) notation {
(mantissa/exponent) const float n = 6.18e2;
const double n = 6.18e2; printf("%f",n); 618.00
getchar();
return 0;
• 6.18e2 = 6.18x10^2 }

Dr. Muhammad Yousaf Hamza


More types: const

Constants
– Constants are useful for a number of reasons
• Tells the reader of the code that a value does not
change
• Tells the compiler that a value does not change
– The compiler can potentially compile faster code

• Use constants where appropriate

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{

const double pi=3.1415926;


double radius = 4.5, circum ;

circum = 2*pi*radius;

printf("\nCircumference is= %lf", circum); // 28.274334

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("\n%f", circum);

radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Mathematical Functions

Dr. Yousaf, PIEAS


#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;

y = sin(PI/2.0); // argument is in radian

printf("%f", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%f", y);
getchar(); return 0; }
Output: 1.000000

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS
• log() ……..> natural log
• log10() …………> base-10 logarithm
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
#include<stdio.h> k = floor(15.2);
#include<math.h>
printf(" k = %d\n",k);
#define PI 3.14 // output: 15
j = ceil (15.2);
int main()
{ printf(" j = %d\n",j);
// output: 16
int k, j, r;
double x = 25.0, y, z; r = abs (-67);
printf(" r = %d\n",r);
y = sqrt(x); // output: 67
printf("y = %lf\n", y); // y = 5.000000 getchar();
return 0;
z = pow(y,3); // y^3, z = 125.000000
}
printf(" z = %lf\n", z);

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS

You might also like