









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Programming in c. Data structures in C. operators in c
Typology: Lecture notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Operators in C Operators C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. Operators are used in program to manipulate data and variables.
Operator Meaning of Operator
// C Program to demonstrate the working of arithmetic operators #include <stdio.h> #include<conio.h> void main() { int a = 10,b = 5, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b; printf("a-b = %d \n",c);
c = ab; printf("ab = %d \n",c);
c=a/b; printf("a/b = %d \n",c);
c=a%b; printf("Remainder when a divided by b = %d \n",c);
getch(); } Increment and decrement operators C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand. Example for Increment and Decrement Operators // C Program to demonstrate the working of increment and decrement operators #include <stdio.h> #include<conio.h> void main() { int a = 15, b = 10; float c = 12.5, d = 1.5;
printf("++a = %d \n", ++a);
y-= x; // y = y-x printf(" y= %d \n", y);
y = x; // y = ya printf("y = %d \n", y);
y/= x; // y = y/x printf("y = %d \n", y);
y %= x; // y = y%x printf("y= %d \n", y);
getch(); } C Relational Operators A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and loops. Operator Meaning of Operator^ Example == Equal to 7 == 4 returns 0
Greater than 7 > 4 returns 1 < Less than 7 < 4 returns 0 != Not equal to 7 != 4 returns 1 = Greater than or equal to
7 >= 4 returns 1
<= Less than or equal to 7 <= 4 return 0
Example for Relational Operators // C Program to demonstrate the working of arithmetic operators #include <stdio.h> #include<conio.h>
void main() { int a = 10, b = 10, c = 20;
printf("%d == %d = %d \n", a, b, a == b); // true printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true printf("%d <= %d = %d \n", a, c, a <= c); //true
getch();
} Logical Operators An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
Operator Meaning of Operator^ Example && Logial AND. True only if all operands are true
If c = 10 and d = 4 then, expression ((c == 10) && (d > 3)) equals to 1.
return 0; } Bitwise Operators During computation, mathematical operations like: addition, subtraction, addition and division are converted to bit-level which makes processing faster and saves power. Bitwise operators are used in C programming to perform bit-level operations. Operators Meaning of operators & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left
Shift right
Bitwise AND operator & The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.
Truth Table Bitwise AND a b Result 1 0 0 0 1 0 1 1 1 0 0 0
Let us suppose the bitwise AND operation of two integers 12 and 25. 12 = 00001100 (In Binary) 25 = 00011001 (In Binary)
Bit Operation of 12 and 25 0000 1100 & 0001 1001
0000 1000 = 8 (In decimal) Bitwise OR operator | The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted by |.
Truth Table Bitwise XOR a b Result 1 0 1 0 1 1 1 1 1 0 0 0
12 = 0000 1100 (In Binary) 25 = 0001 1001 (In Binary) Bitwise OR Operation of 12 and 25 0000 1100 | 0001 1001 0001 1101 = 29 (In decimal) Bitwise XOR (exclusive OR) operator ^ The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.
Truth Table Bitwise XOR a b Result 1 0 1 0 1 1 1 1 0 0 0 0
Example 12 = 0000 1100 (In Binary) 25 = 0001 1001 (In Binary)
212<<1 = 110101000 (In binary) [Left shift by one bit] 212<<0 =11010100 (Shift by 0) 212<<4 = 110101000000 (In binary) =3392 (In decimal) Example Program for Bitwise operator: #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the value of a and b \n"); scanf("%d%d",&a,&b); printf("a&b is %d \n",a&b); printf("a|b is %d \n",a|b); printf("~a is %d \n",~a); printf("a>>2 is %d \n",a>>2); printf("a<<s is %d \n",a<<2); printf("a^b is %d \n",a^b); getch(); } Conditional operator A conditional operator is a ternary operator, that is, it works on 3 operands. Conditional Operator Syntax Conditional_Expression? expression1: expression The conditional operator works as follows:
scanf(โ%d%dโ,&a,&b); big = ( a>b?a: b) ; printf("Big value is %d", big); }
Special operators
Operator Description , Comma operator sizeof Size of operator & Address of Operator
Comma Operator
int a, c = 5, d; The sizeof operator The sizeof is an unary operator which returns the size of data (constant, variables, array, structure etc). Example #6: sizeof Operator #include <stdio.h> int main() { int a, e[10]; float b; double c; char d; printf("Size of int=%lu bytes\n",sizeof(a)); printf("Size of float=%lu bytes\n",sizeof(b)); printf("Size of double=%lu bytes\n",sizeof(c)); printf("Size of char=%lu byte\n",sizeof(d)); printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e)); return 0; }
Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left. For example โ*โ and โ/โ have same precedence and their associativity is Left to Right, so the expression โ100 / 10 * 10โ is treated as โ(100 / 10) * 10โ. If two operators of same precedence (priority) is present in an expression, Associativity of operators indicate the order in which they execute.
1 == 2 != 3 Here, operators == and != have same precedence. The associativity of both == and != is left to right, i.e, the expression on the left is executed first and moves towards the right. Thus, the expression above is equivalent to : ((1 == 2) != 3) i.e, (1 == 2) executes first resulting into 0 (false) then, (0 != 3) executes resulting into 1 (true) Output 1
Category Operator Associativity Postfix () [] ->. ++ - - Left to right Unary + -! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |=
Right to left
Comma , Left to right
Example for operator Precedence #include <stdio.h>
main() {
int a = 20; int b = 10; int c = 15; int d = 5; int 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; } Output:
Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50
Program to Compute Quotient and Remainder
#include <stdio.h> int main(){
int dividend, divisor, quotient, remainder;
printf("Enter dividend: "); scanf("%d", ÷nd);
printf("Enter divisor: ");