Lecture 5 – Operators in C

Operators in C
There are following types of operators in C

1.       Unary operators are the operators which has only one Operand


                                                              i.      Pre-increment – Value is incremented first, and then assigned

// This program demonstrates the use of pre-increment operator.


Output
A = 1      B = 1
# include <stdio.h>



# include <conio.h>

void main()

{

int a=0; b=0;

clrscr();                                                                                                        

b= ++a;

printf(“\nA = %d\t B = %d”, a, b);

getch();

}

Example 10 – Pre-increment

The value of A is incremented first, and then it is assigned to B. That is why both A and B have same values.

                                                            ii.      Post-increment – Value is assigned first and then it is incremented

// This program demonstrates the use of post-increment operator.

# include <stdio.h>

# include <conio.h>

void main()

{


Output
A = 6      B = 5
int a=5; b=10;

clrscr();                                                                                                        

b= a++;

printf(“\nA = %d\t B = %d”, a, b);

getch();

}

Example 11 – Post-increment

Firstly, the value of A is assigned to B, and then it is incremented. That is why A = 1 and B = 0


                                                          iii.      Pre-decrement – Value is decremented first, and then assigned

// This program demonstrates the use of pre-decrement operator.


Output
A = 4      B = 4
# include <stdio.h>

# include <conio.h>

void main()

{

int a=5; b=10;

clrscr();                                                                                                        

b= --a;

printf(“\nA = %d\t B = %d”, a, b);

getch();

}

Example 12 – Pre-decrement

The value of A is decremented first, and then it is assigned to B. That is why both A and B have same values.

                                                           iv.      Post-decrement – Value is assigned first and then it is decremented

// This program demonstrates the use of post-decrement operator.

# include <stdio.h>

# include <conio.h>

void main()


Output
A = 4      B = 5
{

int a=5; b=10;

clrscr();                                                                                                        

b= a--;

printf(“\nA = %d\t B = %d”, a, b);

getch();

}

Example 13 – Post-decrement

The value of A is assigned to B first and then it is decremented. That is why both A and B have different values.

Comments

Popular posts from this blog

Lecture 17 – Functions (continued)

Lecture 6 – Operators in C (continued)

Lecture 10 – Decisions (if-else-if)