c operators

27
C Operators An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc. There are following types of operators to perform different types of operations in C language. Arithmetic Operators Relational Operators Shift Operators Logical Operators Bitwise Operators Ternary or Conditional Operators Assignment Operator Misc Operator Precedence of Operators in C The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operators direction to be evaluated, it may be left to right or right to left. Let's understand the precedence by the example given below: 1. int value=10+20*10; The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator). The precedence and associativity of C operators is given below: Category Operator Associativity Postfix () [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right

Upload: srmohan06

Post on 12-Apr-2017

180 views

Category:

Education


0 download

TRANSCRIPT

Page 1: C operators

C OperatorsAn operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc.

There are following types of operators to perform different types of operations in C language.

Arithmetic Operators Relational Operators Shift Operators Logical Operators Bitwise Operators Ternary or Conditional Operators Assignment Operator Misc Operator

Precedence of Operators in C

The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.

Let's understand the precedence by the example given below:

1. int value=10+20*10;  

The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).

The precedence and associativity of C operators is given below:

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative * / % Left to right

Additive + - Left to right

Page 2: C operators

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

Control statement:

if else StatementThe if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.

There are many ways to use if statement in C language:

If statement If-else statement If else-if ladder Nested if

Page 3: C operators

If Statement

The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below:

1. if(expression){  2. //code to be executed  3. }  

Flowchart of if statement in C

Let's see a simple example of c language if statement.

1. #include<stdio.h>  2. #include<conio.h>  3. void main(){  4. int number=0;  

Page 4: C operators

5. clrscr();  6.   7. printf("enter a number:");  8. scanf("%d",&number);  9.   10. if(number%2==0){  11. printf("%d is even number",number);  12. }  13.   14. getch();  15. }  

Outputenter a number:4

4 is even number

enter a number:5

If-else Statement

The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below:

1. if(expression){  2. //code to be executed if condition is true  3. }else{  4. //code to be executed if condition is false  5. }  

Page 5: C operators

Flowchart of if-else statement in C

Let's see the simple example of even and odd number using if-else statement in C language.

1. #include<stdio.h>  2. #include<conio.h>  3. void main(){  4. int number=0;  5. clrscr();  6.   7. printf("enter a number:");  8. scanf("%d",&number);  9.   10. if(number%2==0){  11. printf("%d is even number",number);  12. }  13. else{  14. printf("%d is odd number",number);  

Page 6: C operators

15. }  16. getch();  17. }  

Outputenter a number:4

4 is even number

enter a number:5

5 is odd number

If else-if ladder Statement

The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:

1. if(condition1){  2. //code to be executed if condition1 is true  3. }else if(condition2){  4. //code to be executed if condition2 is true  5. }  6. else if(condition3){  7. //code to be executed if condition3 is true  8. }  9. ...  10. else{  11. //code to be executed if all the conditions are false  12. }  

Page 7: C operators

Flowchart of else-if ladder statement in C

The example of if-else-if statement in C language is given below.

1. #include<stdio.h>  2. #include<conio.h>  3. void main(){  4. int number=0;  5. clrscr();  6.   7. printf("enter a number:");  8. scanf("%d",&number);  9.   10. if(number==10){  11. printf("number is equals to 10");  12. }  13. else if(number==50){  14. printf("number is equal to 50");  15. }  16. else if(number==100){  

Page 8: C operators

17. printf("number is equal to 100");  18. }  19. else{  20. printf("number is not equal to 10, 50 or 100");  21. }  22. getch();  23. }  

Outputenter a number:4

number is not equal to 10, 50 or 100

enter a number:50

number is equal to 50

C Switch StatementThe switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.

The syntax of switch statement in c language is given below:

1. switch(expression){    2. case value1:    3.  //code to be executed;    4.  break;  //optional  5. case value2:    6.  //code to be executed;    7.  break;  //optional  8. ......    9.     10. default:     11.  code to be executed if all cases are not matched;    12. }    

Rules for switch statement in C language

1) The switch expression must be of integer or character type.

2) The case value must be integer or character constant.

3) The case value can be used only inside the switch statement.

Page 9: C operators

4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.

Let's try to understand it by the examples. We are assuming there are following variables.

1. int x,y,z;  2. char a,b;  3. float f;  

Valid Switch Invalid Switch Valid Case Invalid Case

switch(x) switch(f) case 3; case 2.5;

switch(x>y) switch(x+2.5) case 'a'; case x;

switch(a+b-2) case 1+2; case x+2;

switch(func(x,y)) case 'x'>'y'; case 1,2,3;

Page 10: C operators

Flowchart of switch statement in C

Let's see a simple example of c language switch statement.

1. #include<stdio.h>  2. #include<conio.h>  3. void main(){  4. int number=0;  5. clrscr();  6.   7. printf("enter a number:");  8. scanf("%d",&number);  

Page 11: C operators

9.   10. switch(number){  11. case 10:  12. printf("number is equals to 10");  13. break;  14. case 50:  15. printf("number is equal to 50");  16. break;  17. case 100:  18. printf("number is equal to 100");  19. break;  20. default:  21. printf("number is not equal to 10, 50 or 100");  22. }  23. getch();  24. }  

Outputenter a number:4

number is not equal to 10, 50 or 100

enter a number:50

number is equal to 50

C Switch statement is fall-through

In C language, switch statement is fall through, it means if you don't use break statement in switch case, all the case after matching case will be executed.

Let's try to understand the fall through state of switch statement by the example given below.

1. #include<stdio.h>  2. #include<conio.h>  3. void main(){  4. int number=0;  5. clrscr();  6.   7. printf("enter a number:");  8. scanf("%d",&number);  9.   10. switch(number){  11. case 10:  12. printf("number is equals to 10\n");  13. case 50:  14. printf("number is equal to 50\n");  15. case 100:  16. printf("number is equal to 100\n");  17. default:  

Page 12: C operators

18. printf("number is not equal to 10, 50 or 100");  19. }  20. getch();  21. }  

Outputenter a number:10

number is equals to 10

number is equals to 50

number is equals to 100

number is not equal to 10, 50 or 100

enter a number:50

number is equal to 50

number is equals to 100

number is not equal to 10, 50 or 100

C LoopsThe loops in C language are used to execute a block of code or a part of the program several times.

In other words, it iterates a code or group of code many times.

Why use loops in C language?

Suppose that you have to print table of 2, then you need to write 10 lines of code.

By using the loop statement, you can do it by 2 or 3 lines of code only.

Advantage of loops in C

1) It saves code.

2) It helps to traverse the elements of array (which is covered in next pages).

Types of C Loops

There are three types of loops in C language that is given below:

Page 13: C operators

1. do while2. while3. for

do-while loop in C

It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.

It is better if you have to execute the code at least once.

The syntax of do-while loop in c language is given below:

1. do{  2. //code to be executed  3. }while(condition);  

Flowchart and Example of do-while loop

while loop in C

Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.

It is better if number of iteration is not known by the user.

The syntax of while loop in c language is given below:

1. while(condition){  2. //code to be executed  3. }  

Flowchart and Example of while loop

for loop in C

Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.

It is good if number of iteration is known by the user.

Page 14: C operators

The syntax of for loop in c language is given below:

1. for(initialization;condition;incr/decr){  2. //code to be executed  3. }  

do while loop in CTo execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true.

In do while loop, statement is given before the condition, so statement or code will be executed at lease one time. In other words, we can say it is executed 1 or more times.

It is better if you have to execute the code at least once.

do while loop syntax

The syntax of C language do-while loop is given below:

1. do{  2. //code to be executed  3. }while(condition);  

Flowchart of do while loop

Page 15: C operators

do while example

There is given the simple program of c language do while loop where we are printing the table of 1.

1. #include <stdio.h>    2. #include <conio.h>    3. void main(){    4. int i=1;  5. clrscr();    6.   7. do{  8. printf("%d \n",i);  9. i++;  10. }while(i<=10);  11.     12. getch();    13. }    

Output1

2

3

4

5

6

7

8

9

10

Program to print table for the given number using do while loop1. #include <stdio.h>    2. #include <conio.h>    3. void main(){    4. int i=1,number=0;  5. clrscr();    6.   7. printf("Enter a number: ");  8. scanf("%d",&number);  9.   10. do{  11. printf("%d \n",(number*i));  12. i++;  13. }while(i<=10);  

Page 16: C operators

14.     15. getch();    16. }    

OutputEnter a number: 5

5

10

15

20

25

30

35

40

45

50

Enter a number: 10

10

20

30

40

50

60

70

80

90

100

Infinitive do while loop

If you pass 1 as a expression in do while loop, it will run infinite number of times.

1. do{  2. //statement  3. }while(1);  

while loop in C

Page 17: C operators

The while loop in C language is used to iterate the part of program or statements many times.

In while loop, condition is given before the statement. So it is different from the do while loop. It can execute the statements 0 or more times.

When use while loop in C

The C language while loop should be used if number of iteration is uncertain or unknown.

Syntax of while loop in C language

The syntax of while loop in c language is given below:

1. while(condition){  2. //code to be executed  3. }  

Flowchart of while loop in C

Example of while loop in C language

Page 18: C operators

Let's see the simple program of while loop that prints table of 1.

1. #include <stdio.h>      2. #include <conio.h>      3. void main(){      4. int i=1;    5. clrscr();      6.     7. while(i<=10){    8. printf("%d \n",i);    9. i++;    10. }   11.       12. getch();      13. }      

Output1

2

3

4

5

6

7

8

9

10

Program to print table for the given number using while loop in C

1. #include <stdio.h>    2. #include <conio.h>    3. void main(){    4. int i=1,number=0;  5. clrscr();    6.   7. printf("Enter a number: ");  8. scanf("%d",&number);  9.   10. while(i<=10){  11. printf("%d \n",(number*i));  12. i++;  13. }  14.   15. getch();    16. }    

Page 19: C operators

OutputEnter a number: 50

50

100

150

200

250

300

350

400

450

500

Enter a number: 100

100

200

300

400

500

600

700

800

900

1000

Infinitive while loop in C

If you pass 1 as a expression in while loop, it will run infinite number of times.

1. while(1){  2. //statement  3. }  

for loop in CThe for loop in C language is also used to iterate the statement or a part of the program several times, like while and do-while loop.

Page 20: C operators

But, we can initialize and increment or decrement the variable also at the time of checking the condition in for loop.

Unlike do while loop, the condition or expression in for loop is given before the statement, so it may execute the statement 0 or more times.

When use for loop in C

For loop is better if number of iteration is known by the programmer.

Syntax of for loop in C

The syntax of for loop in c language is given below:

1. for(initialization;condition;incr/decr){  2. //code to be executed  3. }  

Page 21: C operators

Flowchart of for loop in C

Example of for loop in C language

Let's see the simple program of for loop that prints table of 1.

1. #include <stdio.h>      2. #include <conio.h>      3. void main(){      4. int i=0;    5. clrscr();      6.     7. for(i=1;i<=10;i++){    8. printf("%d \n",i);    9. }   

Page 22: C operators

10.       11. getch();      12. }      

Output1

2

3

4

5

6

7

8

9

10

C Program : Print table for the given number using C for loop

1. #include <stdio.h>    2. #include <conio.h>    3. void main(){    4. int i=1,number=0;  5. clrscr();    6.   7. printf("Enter a number: ");  8. scanf("%d",&number);  9.   10. for(i=1;i<=10;i++){    11. printf("%d \n",(number*i));  12. }  13.   14. getch();    15. }    

OutputEnter a number: 2

2

4

6

8

10

12

Page 23: C operators

14

16

18

20

Enter a number: 1000

1000

2000

3000

4000

5000

6000

7000

8000

9000

10000

Infinitive for loop in C

If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.

In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.

1. for(;;){  2. printf("infinitive for loop example by javatpoint");  3. }  

If you run this program, you will see above statement infinite times.

C break statementThe break statement in C language is used to break the execution of loop (while, do while and for) and switch case.

In case of inner loops, it terminates the control of inner loop only.

There can be two usage of C break keyword:

1. With switch case

Page 24: C operators

2. With loop

Syntax:1. jump-statement;  2. break;  

The jump statement in c break syntax can be while loop, do while loop, for loop or switch case.

Flowchart of break in c

Example of C break statement with switch case

Click here to see the example of C break with switch statement.

Example of C break statement with loop1. #include <stdio.h>    2. #include <conio.h>    3. void main(){    4. int i=1;//initializing a local variable  5. clrscr();    

Page 25: C operators

6.   7. //starting a loop from 1 to 10  8. for(i=1;i<=10;i++){    9. printf("%d \n",i);  10. if(i==5){//if value of i is equal to 5, it will break the loop  11. break;  12. }  13. }//end of for loop  14.   15. getch();    16. }    

Output1

2

3

4

5

As you can see on console output, loop from 1 to 10 is not printed after i==5.

C break statement with inner loop

In such case, it breaks only inner loop, but not outer loop.

1. #include <stdio.h>    2. #include <conio.h>    3. void main(){    4. int i=1,j=1;//initializing a local variable  5. clrscr();    6.   7. for(i=1;i<=3;i++){    8. for(j=1;j<=3;j++){  9. printf("%d &d\n",i,j);  10. if(i==2 && j==2){  11. break;//will break loop of j only  12. }  13. }  14. }//end of for loop  15.   16. getch();    17. }    

Output1 1

Page 26: C operators

1 2

1 3

2 1

2 2

3 1

3 2

3 3

As you can see the output on console, 2 3 is not printed because there is break statement after printing i==2 and j==2. But 3 1, 3 2 and 3 3 is printed because break statement works for inner loop only