0% found this document useful (0 votes)
24 views20 pages

C Programming: Operators and Expressions

Uploaded by

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

C Programming: Operators and Expressions

Uploaded by

tqhien614
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INS2020 Programming 1

Lecture 04: Types, Operators and Expressions

(part 2)

Dr. Ngoc Thanh Pham - Faculty of Applied Sciences,


International School, Vietnam National University, Hanoi

1
Plan For Today

Declarations
Operators
Expressions
2
Declarations
<qualifier> <type> <identifier1> = <value1>, <identifier2> = <value2>, ... ;

int lower, upper, step; /* 3 uninitialised ints */


char tab = '\t'; /* a char initialised with ‘\t’ */
char buf[10]; /* an uninitialised array of chars */
int m = 2+3+4; /* constant expression: 9 */
int n = m + 5; /* initialised with 9+5 = 14 */
float limit = 9.34f;
char name[10] = {'c','o','d','e','\0'};
const double PI = 3.1416;

• All variables must be declared before they are used


• A variable may also be initialized in its declaration (optional)
• Qualifier const is added to a declaration → value will not change.
3
Plan For Today
• Declarations
• Operators
• Expressions
• …..

4
Arithmetic Operations
#include <stdio.h>
int main( )
{
float num1 = 10;
float num2 = 7;

printf( "Add %f\n", num1 + num2 );


printf( "Subtract %f\n", num1 - num2 );
printf( "Multiply %f\n", num1 * num2 );
printf( "Divider %f\n", num1 / num2 );
printf( "Modulus %d\n", 10 % 7 ); // Remainder operator

float x = 3.2;
float y = ++x; //preincrement = x is increased to 4.2, and then assigned to y
float z = x++; //postincrement = x is assigned to z, and then increased by 1
printf("x = %f; y = %f; z = %f", x,y,z);
return 0;
}
5
Arithmetic Operations
#include <stdio.h> • The % operator cannot be applied to a float or double
int main( )
• x % y produces the remainder from the division x / y
{
float num1 = 10;
float num2 = 7;

printf( "Add %f\n", num1 + num2 );


printf( "Subtract %f\n", num1 - num2 );
printf( "Multiply %f\n", num1 * num2 );
printf( "Divider %f\n", num1 / num2 );
printf( "Modulus %d\n", 10 % 7 ); // Remainder operator

float x = 3.2;
float y = ++x; //preincrement = x is increased to 4.2, and then assigned to y
float z = x++; //postincrement = x is assigned to z, and then increased by 1
printf("x = %f; y = %f; z = %f", x,y,z);
return 0;
}
6
Arithmetic Operations
#include <stdio.h>
int main( )
{
float num1 = 10;
float num2 = 7;

printf( "Add %f\n", num1 + num2 );


printf( "Subtract %f\n", num1 - num2 );
printf( "Multiply %f\n", num1 * num2 );
printf( "Divider %f\n", num1 / num2 );
printf( "Modulus %d\n", 10 % 7 ); // Remainder operator

float x = 3.2;
float y = ++x; //preincrement = x is increased to 4.2, and then assigned to y
float z = x++; //postincrement = x is assigned to z, and then increased by 1
printf("x = %f; y = %f; z = %f", x,y,z);
return 0;
}
7
Relational Operation
#include <stdio.h>
int main()
{
int a = 6, b = 4;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d <= %d is %d \n", a, b, a <= b);
return 0;
}

• Relational operators are used to compare two values in C.


• If relation is true, it returns 1; If the relation is false, it returns 0.
8
Logical Operation

• Logical operators are mostly used for decision making.


• A logical operator returns either 0 or 1 whether the condition is true or false.
9
Logical Operation
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
printf("&& Operator : Both conditions are true\n");
if (o>p || p!=20)
printf("|| Operator : Only one condition is true\n");
if (!(m>n && m !=0))
printf("! Operator : Both conditions are true\n");
else
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}

10
Bitwise Operators
#include <stdio.h>
int main()
{
int m = 40,n = 80;
int AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
return 0;
} • These operators are used to perform bit operations.
• Decimal values are converted into binary values which are the
sequence of bits and bitwise operators work on these bits. 11
Bitwise Operators
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
return 0;
} • These operators are used to perform bit operations.
• Decimal values are converted into binary values which are the
sequence of bits and bitwise operators work on these bits.
12
Bitwise Operators
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m); x = 00101000 = 40 (decimal)
XOR_opr = (m^n); x << 1 = 01010000 (binary) = 80 (decimal)
printf("AND_opr value = %d\n",AND_opr ); x >> 1 = 00010100 (binary) = 20 (decimal)
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
return 0;
}
• m << k: k means that the bits will be left shifted by k place.
• m >> k: k means that the bits will be right shifted by k place
13
Assignment Operators

sum += 10; //sum = sum + 10


sum -= 10; //sum = sum - 10
sum *= 10; //sum = sum * 10
sum /= 10; //sum = sum / 10
sum %= 10; //sum = sum % 10
sum ^= 10; //sum = sum ^ 10

14
Assignment Operators
#include<stdio.h>
int main ()
{
int x = 10, y = 12;
/* x += y is same as x = x + y */
printf ( " x += y : %d \n", x += y );
/* x -= y is same as x = x - y */ x *= y + 1 means
x = 10; y = 12;
printf ( " x -= y : %d \n", x -= y );
x = x * (y + 1)?
/* x *= y is same as x = x * y */ OR x = x * y + 1?
x = 10; y = 12;
printf ( " x *= y : %d \n", x *= y );
/* x /= y is same as x = x / y */
x = 10; y = 12;
printf ( " x /= y : %d \n", x /= y );
/* x %= y is same as x = x % y */
printf ( " x %= y : %d \n", x %= y );
return 0;
}
15
Conditional Expressions
#include <stdio.h>
int main()
if (x == 1)
{
y = 2;
int x=1, y ;
else
y = (x ==1? 3 : 0) ; y = 0;
printf("x = %d\n", x);
printf("y = (x ==1? 3 : 0) = %d\n", y);
int a=3, b=2;
int z = (a > b) ? a : b; /* z = max(a, b) */
printf("z = max(%d, %d) = %d", a,b,z);
return 0;
}

• Conditional operators return true_value if condition is true and


returns false_value is condition is false.
• Syntax: (Condition? true_value: false_value);
16
Conditional Expressions
#include <stdio.h>
int main()
{
int n=53;
for (int i = 0; i < n; i++)
printf("%6d%c", i, (i%10==9 || i==n-1) ? '\n' : ' ');
}

• Conditional operators return true_value if condition is true and


returns false_value is condition is false.
• Syntax: (Condition? true_value: false_value);
17
Order of Evaluation [1]
• The arithmetic operators hold higher precedence
than the logical and relational operators.
• E.g.: 10 > 1 + 9 means 10 > (1+9)
• Order of Precedence in Arithmetic Operators:
1. ++ and -- (increment and decrement)
operators hold the highest precedence.
2. Then comes - (unary minus) operator
3. Then comes *, / and % holding equal
precedence.
4. At last, + and - operators hold the lowest
precedence.
• Order of precedence in Relational/Logical
Operators:
1. ! (logical NOT) operator holds the highest
precedence
2. >, >=, <, <= hold the same precedence.
3. == and != operators
4. && (logical AND) operator
5. || (logical OR) hold the lowest precedence
18
Order of Evaluation [1]
#include <stdio.h>
int main() {
// arithmetic operator precedence
int a = 10, b = 20, c = 30, result;
result = a * b + ++c;
printf("The result is: %d", result);
return 0;
}

• Order of Precedence in Arithmetic Operators:


1. ++ and -- (increment and decrement) operators
hold the highest precedence.
2. Then comes - (unary minus) operator
3. Then comes *, / and % holding equal precedence.
4. At last, + and - operators hold the lowest
precedence.

19
Order of Evaluation [1]
#include <stdio.h>
int main() {
int a = 20,b = 10,c = 15,d = 5,e;
e = (a+b)*c/d; // ( 30*15)/5
printf("Value of (a+b)*c/d is : %d\n", e );
e = ((a+b)*c)/d; // (30*15)/5
printf("Value of ((a+b)*c)/d is : %d\n" , e );
e = (a+b)*(c/d); // (30)*(15/5)
printf("Value of (a + b)*(c/d) is : %d\n", e );
e = a+(b*c)/d; // 20 + (150/5)
printf("Value of a+(b*c)/d is : %d\n" , e );
return 0;
}

20

You might also like