WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

C Β· Intermediate Β· question 27 of 100

Explain the difference between ++i and i++.?

πŸ“• Buy this interview preparation book: 100 C questions & answers β€” PDF + EPUB for $5

++i and i++ are both unary operators in C programming language that increment the value of a variable by 1. However, there is a subtle difference between the two that can affect the behavior of a program.

The ++i operator is known as the pre-increment operator, which means that it increments the value of the variable before it is used in the expression. The value of the expression is the incremented value of the variable. Here is an example:

    int i = 5;
    int j = ++i;
    // i is now 6, j is also 6

In this example, the value of i is incremented before it is assigned to j, so both i and j have a value of 6.

The i++ operator is known as the post-increment operator, which means that it increments the value of the variable after it is used in the expression. The value of the expression is the original value of the variable. Here is an example:

    int i = 5;
    int j = i++;
    // i is now 6, j is 5

In this example, the value of i is assigned to j before it is incremented, so j has a value of 5 and i has a value of 6.

The same applies to the pre-decrement operator –i and the post-decrement operator i–. The pre-decrement operator decrements the value of the variable before it is used in the expression, while the post-decrement operator decrements the value of the variable after it is used in the expression.

In general, the choice between using pre-increment/decrement or post-increment/decrement depends on the specific requirements of the program. Pre-increment/decrement can be used when the updated value of the variable is needed immediately, while post-increment/decrement can be used when the original value of the variable is needed in the expression. It is important to use the correct operator to avoid unintended behavior and logical errors in the program.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview β€” then scores it.
πŸ“ž Practice C β€” free 15 min
πŸ“• Buy this interview preparation book: 100 C questions & answers β€” PDF + EPUB for $5

All 100 C questions Β· All topics