What is the difference between = and == operators in C

Interview Questions
  • What is the difference between = and == operators in C.
  • What is the difference between pre increment operator(++var) and post increment operator(var++).
  • What is the difference between pre decrement operator(--var) and post decrement operator(var--).

What is the difference between “=” and “==” operators in C

  • First of all = is a assignment operator and == is a comparison operator.
  • = operator is used to assign value to a variable and == operator is used to compare two variable or constants.
  • The left side of = operator can not be a constant, while for == operator both sides can be operator.


What is the difference between pre increment operator(++var) and post increment operator(var++).

Pre increment operator(++var) first increment the value of variable by one and then it returns its value whereas post increment operator (var++) first returns the value of variable then increments its value by 1.

For Example :

int var = 2;
printf("%d", ++var);
Pre increment operator first increment the value of var to 3 and then returns its value. Above printf statement will print 3. Let. int var = 2;
printf("%d", var++);
Post increment operator first returns the value of var and then increments its value by one. Above printf statement will print 2 and value of var becomes 3.


What is the difference between pre decrement operator(--var) and post decrement operator(var--).

Pre decrement operator(--var) first decrement the value of variable by one and then it returns its value whereas post decrement operator (var--) first returns the value of variable then decrements its value by 1.

For Example :

printf("%d", --var);
Pre decrement operator first decrement the value of var by one and then returns its value. Above printf statement will print 1.
int var = 2;
printf("%d", var--);
Post decrement operator first returns the value of var and then decrement its value by one. Above printf statement will print 2 and value of var becomes 1.


Related Topics
What is the difference between & and * operators in C
What is the difference between variable declaration and variable definition in C
What is the difference between constant and variable in C
What is the difference between C and C++.
What is the difference between compiler, interpreter and assembler.
What is constant in C and Different Types of Constants.
What is the difference between auto variable and register variable in C
What is the difference between C and Java Programming Language.
Why is default statement used in switch case in C
What is the difference between strcpy and strncpy function