Professional Documents
Culture Documents
Unit 1-C OPERATORS
Unit 1-C OPERATORS
C Operators
Increment
#include <stdio.h>
int main() {
int x = 5;
printf("%d", ++x);
return 0;
}
Unary
Syntax
1. int a = 2;
2. int b = -(a);
▪ In the next slide will introduce the #include<conio.h>. It is a header file is a C library that provides functions
for handling console input and output operations.
Unary Minus (-)…
For example:
1. x = 10;
2. A = ++x;
Here the initial value of the x variable is 10 and using the post-increment operator (x++) to assign
increment value of the 'x' to the variable 'A'.
Unary Increment Operator (++)…
Pre Decrement: The pre decrement operator is denoted as (--a) symbol, meaning the operand
value is decreased by 1 before assigning to another variable or expression.
Syntax
1. int pre = --a;
Post Decrement: The Post decrement operator is denoted as (a--) symbol, which means the
original value is decreased by 1 after assigning to another variable or expression.
Syntax
1. int post = a--;
Pre decrement operator and Post Decrement operator
The sizeof is a keyword used to find the size of different data types or operands like int,
float, char, double, etc.
Syntax
1. sizeof(data_variable);
Unary Sizeof() Operator…
#include <stdio.h> // use sizeof() operator and pass the different data type variable to get their size.
#include <conio.h> printf (" The size of the int (x) variable is: %d", sizeof(x));
int main () printf (" \n The size of the float (y) variable is: %d", sizeof(y));
{ printf (" \n The size of the char (ch) variable is: %d", sizeof(ch));
int x; printf (" \n The size of the double (z) variable is: %d", sizeof(z));
float y; return 0;
char ch; }
double z;
Relational Operators
! Logical not Reverse the result, returns !(x < 5 && x < 10)
false if the result is true
Logical Operators…
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int x = 5;
int x = 5;
x += 3;
x *= 3;
printf("%d", x);
printf("%d", x);
return 0;
return 0;
}
}
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int x = 5;
float x = 5;
x &= 3;
x /= 3;
printf("%d", x);
printf("%f", x);
return 0;
return 0;
}
}
#include <stdio.h>
int main() {
int x = 5;
x %= 3;
printf("%d", x);
return 0;
}
#include <stdio.h> #include <stdio.h>
int x = 5; int x = 5;
x |= 3; x >>= 3;
return 0; return 0;
} }
int x = 5; int x = 5;
x ^= 3; x <<= 3;
return 0; return 0;
} }
Assignment operators…
== Equal to x == y
!= Not equal x != y
| Bitwise OR operator
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
Bitwise operators…
1. #include <stdio.h>
1. #include <stdio.h>
2. int main()
2. int main()
3. {
3. {
4. int a=12,b=10;
4. int a=8;
5. printf("The output is: %d",a^b);
5. printf("The output of the Bitwise compl
6. return 0;
ement operator ~a is %d",~a);
7. } 6. return 0;
7. }
Bitwise operators…
The logical not operator is used to reverse the given condition. For example, if the
operand is true, the logical not operator (!) reverses and return false; if the operand is
false, the logical operator returns true.
Syntax
1. bool a = true;
2. bool b = !a; // It reverse the condition of variable b
Logical Not (!) Operator…
#include <stdio.h> printf (" The Boolean value of a is: %d", a);
#include <stdbool.h> printf (" \n The Boolean value of b is: %d", b);
int main ()
{ bool c = 0;
// declare variables bool d = !c;
bool a = true; printf (" \n The Boolean value of c is: %d", c);
bool b; printf (" \n The Boolean value of d is: %d", d);
b = !a; // use logical operator to reverse the condition return 0;
}
AddressOf Operator (&)
The Unary AddressOf Operator is denoted as ampersand (&) symbol, which is used to
find the address of a variable defined in computer memory.
Syntax
1. int a = 5;
2. int b = &a; // variable b hold the address of variable a
AddressOf Operator (&)…