LET US STUDY C PROGRAMMING 4 Operators

In the last section a program we are written in the program some function are used that are
clrscr() means clear screen.
getch() means get character.
printf() means printing.
scanf() means scan what we are using in the program.
Format Identifiers
%d decimal integers
%x hex integer
%c character
%f float and double number
%s string
%p pointer
How to specify display space for a variable?
printf(“The student id is %5d \n”, stud_id);
The value of stud_id will occupy 5 characters space in the print-out.
Why “\n”
It introduces a new line on the terminal screen.
\a | alert (bell) character | \\ | backslash |
\b | backspace | \? | question mark |
\f | formfeed | \’ | single quote |
\n | newline | \” | double quote |
\r | carriage return | \000 | octal number |
\t | horizontal tab | \xhh | hexadecimal number |
\v | vertical tab |
What is operator and its duty
The operator is used for what specific activity want to run. may be logical operation or mathematical. An operator is a symbol example + this operator is used for addition and ++ for increment etc.The operators are using in c program are.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment and Decrement Operators
- Misc Operators
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication etc.
Operator | Meaning of Operator |
---|---|
+ | addition or unary plus |
– | subtraction or unary minus |
* | multiplication |
/ | division |
% | remainder after division (modulo division) |
example program using operator for addition.
#include <stdio.h>
int main()
{
int a,b,c;
printf(“Enter two numbers to add\n”);
scanf(“%d%d”, &a, &b);
c = a + b;
printf(“Sum of the numbers = %d\n”, 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 returns 0.
Operator | Meaning of Operator | Example |
---|---|---|
== | Equal to | 4 == 3 is evaluated to 0 |
> | Greater than | 6> 3 is evaluated to 1 |
< | Less than | 6< 5 is evaluated to 0 |
!= | Not equal to | 3!= 4 is evaluated to 1 |
>= | Greater than or equal to | 6>= 4 is evaluated to 1 |
<= | Less than or equal to | 6<= 4 is evaluated to 0 |
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
Operator | Meaning | Example |
---|---|---|
&& | Logical AND. True only if all operands are true | If c = 6and d = 2 then, expression ((c==6) && (d>6) equals to 0. |
|| | Logical OR. True only if either one operand is true | If c = 7and d = 3 then, expression ((c==7) || (d>7)) equals to 1. |
! | Logical NOT. True only if the operand is 0 | If c =7 then, expression !(c==7) equals to 0. |
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc 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 |
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Operator | Example | Same as |
---|---|---|
= | a = b | a = b |
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
%= | a %= b | a = a%b |
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.
Misc Operators ↦ sizeof & ternary
Besides the operators discussed above, there are a few other important operators
Operator | Description | Example |
---|---|---|
sizeof() | Returns the size of a variable. | sizeof(x, where a is integer, will return 6 |
& | Returns the address of a variable. | &a; returns the actual address of the variable. |
* | Pointer to a variable. | *a; |
? : | Conditional Expression. | If Condition is true ? then value X : otherwise value Y |