Computer Science

Increment and Decrement Operators in C

Increment and decrement operators are used in C programming to increase or decrease the value of a variable by one. The increment operator (++) adds one to the variable, while the decrement operator (--) subtracts one from the variable. These operators can be used in a variety of ways to simplify code and make it more efficient.

Written by Perlego with AI-assistance

6 Key excerpts on "Increment and Decrement Operators in C"

  • Book cover image for: Ivor Horton's Beginning Visual C++ 2013
    • Ivor Horton(Author)
    • 2014(Publication Date)
    • Wrox
      (Publisher)
    The Increment and Decrement Operators I’ll now introduce some unusual arithmetic operators called the increment and decrement opera-tors . You will find them to be quite an asset once you get further into applying C++. These are unary operators that you use to increment or decrement a value stored in a variable by 1. For example, assuming count is of type int , the following three statements all have exactly the same effect: count = count + 1; count += 1; ++count; They each increment count by 1. The last statement uses the increment operator and is clearly the most concise. The increment operator not only changes the value of the variable to which you apply it, but also results in a value. Thus, using the increment operator to increase the value of a variable by 1 can also appear as part of a more complex expression. The expression ++count increments the value of the variable and the value that results is the value of the expression. For example, suppose count has the value 5, and you have defined total as type int . Suppose you write the following statement: total = ++count + 10; This increments count to 6. The expression ++count has the resultant value of count , 6, which is added to 10 so total is assigned the value 16. So far you have written the increment operator, ++ , in front of the variable to which it applies. This is called the prefix form of the operator. The operator also has a postfix form, where you write the operator after the variable to which it applies and the effect is slightly different. The variable is incremented only after its value has been used in context. For example: total = count++ + 10; If count has the value 5, total is assigned the value 15, because the initial value of count is used to evaluate the expression before the increment is applied. The preceding statement is equivalent to the two statements: total = count + 10; ++count; The clustering of + signs in the preceding example of the postfix form is likely to lead to confusion.
  • Book cover image for: Programming Microcontrollers in C
    • Ted VanSickle(Author)
    • 2001(Publication Date)
    • Newnes
      (Publisher)
    Several powerful short-hand operators were included in the language that will shorten the program. The increment and decrement operators are examples of such short-hand operators. In the examples earlier there were in-stances of expressions such as i = i + 1; 31 Here the i value stored in memory is replaced by one more than the value found there at the beginning of execution of the expression. The C expression ++i; will do exactly the same thing. The increment operator ++ causes 1 to be added to the value in the memory location i . The decrement operator --causes 1 to be subtracted from the value in the memory location. The increment and decrement operators can be either pre-fix or postfix operators. If, like above, the ++ operator precedes the variable, it is called a prefix operator. If the variable is used in an expression, it will be incremented prior to its use. For example, sup-pose i = 5 . Then the expression j = 2 * ++i; will leave a 12 for the value j and 6 for i . On the other hand, if i again is 5, the expression j = 2 * i--; will leave a value of 10 for j and 4 for I . An easy way to see how the preincrement and the postincrement works is as follows: Suppose that you have a pair of statements j=j+1; These statements can be replaced with The preincrement means that you should replace j with j+1 before you evaluate the expression. Likewise the statements j=j+1; can be replaced with with the post increment, you should evaluate the expression and then replace j with j+1. Increment and Decrement Operators 32 Chapter 1 Introduction to C Often somebody will wonder what will happen if you have mul-tiple increments, either pre or post, of a variable within a single expression. There is an easy answer for that question. Do not do it.
  • Book cover image for: Computer Programming with C++
    The increment/decrement operators can be used in prefix or postfix form. When an increment/decrement operator is used in the prefix form the value of the variable is incremented/decremented before it is used to evaluate the result of a particular Delete MSBs Right shift Pa d 0s Figure 3.15: Working of right shift operator 90 ✦ Computer Programming with C++ expression. Whereas, when an increment/decrement operator is used in the postfix form, the value of the variable is first used to evaluate the result of the expression following which the increment/decrement operation is performed over the variable. Hence, it will be correct to say that the prefix form of increment/decrement evaluates the result of a particular expression based on the newly calculated value of the variable whereas, the postfix form of increment/decrement evaluates the result of the expression based on the old value of the variable which is there before applying the increment/decrement operation on the variable. Given below is the syntax of using the increment operator ++ and the decrement operator --over a particular variable ++Variable; /*usage of increment operator in a prefix form*/ Variable++; /*usage of increment operator in a postfix form*/ --Variable; /*usage of decrement operator in a prefix form*/ Variable--; /*usage of decrement operator in a postfix form*/ Let us now discuss the difference between prefix and postfix from of increment operations with examples. 3.6.1 Increment operator in a prefix form So as to understand the usage of increment operator in the prefix form, let us create two variables a and b as shown below: int a =10,b; Note that, the value of variable a will be set as 10 and the value of the variable b will be junk at this stage as shown in Figure 3.17.
  • Book cover image for: C Programming
    No longer available |Learn more

    C Programming

    A Self-Teaching Introduction

    However, also note that if you use these operators in an expression such as count = count++; or count =++count; then these two expressions will not produce the same effect. This is because in the first expression, the value of count is assigned to count- variable (on the left-hand side) and the count’s value will be increment- ed by one. On the other hand, in the second expression, the count value increments first and then it is assigned to the variable –count on the left-hand side. We are in a position to solve an example now. Example 1: Write a C program to show implement the concept of post- and pre-increment operators. Solution 1: The program is as follows: #include void main( ) { int counter, precount, postcount; counter = 10; precount = ++counter; postcount= counter++; printf(“%d %d\n”,precount,postcount); counter=50; postcount = counter--; precount = --counter; printf(“%d %d\n”,postcount,precount); } PROGRAMMING USING C • 53 OUTPUT (after running): 11 11 50 48 Assignment Operators We have already studied how the assignment operator assigns the value of the expression which is on its right to the left-side variable. Each assignment state- ment itself returns a value. For example, counter1=counter2 =1; In this statement, the effect is to assign the value of 1 to both of the variables— counter1 and counter2. Please note here that the assignment operator has right-to-left associativity; that is, here counter2 is assigned a value of 1 first and then it is assigned to the variable counter1. Arithmetic Assignment Operators ºº C provides arithmetic assignment operators that also shorten expressions. For example, counter = counter + 50; Can also be written as counter += 50; Table 2.3 shows the arithmetic assignment operators.
  • Book cover image for: C Programming
    eBook - ePub

    C Programming

    Learn to Code

    The C language provides two special operators ++ and – –, called increment and decrement operators. These are unary operators because they operate only on one operand. The operand must be a variable and not a constant.
    Example:
    1. x++ is valid but 5++ is not valid;
    2. – – y is valid but – –7 is not valid.
    The ++ operator will increment the value of a variable by 1, and the – – operator will decrement the value of a variable by 1. Let us take a simple example to understand how it works (see Figure 6.7 ).
    Figure 6.7 Program showing increment and decrement operators.
    In the above example, the line x++ is executed as x = x + 1 and the line y– – is executed as y = y – 1. So, the output is x = 6, y = 4. These operators can be used either before (prefix) or after (postfix) their operands. So, we can have x++ (postfix) and ++x (prefix).
    • Prefix and postfix operators have the same effect if they are used in a separate C statement: in Figure 6.8 , x++ and ++x both execute as x = x + 1, and produce 6 as the output.
    • Prefix and postfix operators have different effects when used in association with some other variable. For instance, in Figure 6.9 , y represents a variable that holds the value of x++ in case 1, and ++x in case 2. The execution process is explained below.
    Figure 6.8 Increment or decrement operator executed separately.
    Figure 6.9 Increment and decrement operator executed in association with variable y.
    Case 1 (y=x++; ): as it is postfix, first, the value of x is assigned to y, then x gets incremented. Hence y = 5 and x = 6.
    Case 2 (y=++x; ): as it is prefix, first, the value of x is incremented and x becomes 6, then the incremented value of x is assigned to y. Hence, y = 6 and x = 6.
    The decremented operators are used in a similar way, except of course the values of x and y are decremented.
    Quiz: Analyze the programs shown in Figure 6.10 and find the outputs.
    Figure 6.10 Quiz questions.

    6.7 Conditional Operators

    The conditional operator is also known as a ternary operator because it has two operators and can take three operands. The general form of the conditional operator is shown in Figure 6.11(a)
  • Book cover image for: Learn C Programming from Scratch
    eBook - ePub

    Learn C Programming from Scratch

    A step-by-step methodology with problem solving approach (English Edition)

    Any programming language must include operators, C is no exception. Their importance can be attributed to the fact that they are used to carry out different operations on variables and values in C, simplifying the manipulation of data and the execution of intricate calculations. We will encounter types of operators in the following sections.
    Arithmetic operators In C, arithmetic operators are used to execute operations on numeric data types:
    • + ’ operator is used to denote the addition operation.
    • - ‘ operator is used to denote the subtraction operation.
    • * ’ operator is used to denote the multiplication operation.
    • / ‘ operator is used to denote the division operation and returns the quotient.
    • % ‘ operator is used to denote the division operation and returns the remainder.
    For the two variables x and y int x = 10, y = 5; int s = x + y; // addition int d = x - y; // subtraction int p = x * y; // multiplication int q = x / y; // integer division int r = x % y; // modulus Relational operators
    In C, relational operators are used to compare items and discover their relation. A relational expression’s outcome is either true or false, which are represented as the integer values 1 and 0, respectively, in C.
    Here are the relational operators in C:
    • `= =` (equal to): This operator compares the values of two operands and returns true if they are equal. For example: if (a == b)
    • `!=` (not equal to): This operator compares the values of two operands and returns true if they are not equal. For example: if (a != b)
    • `<` (less than): This operator compares the values of two operands and returns true if the left operand is less than the right operand. For example: if (a < b)
    • `>` (greater than): This operator compares the values of two operands and returns true if the left operand is greater than the right operand. For example: if (a > b)
    • `<=` (less than or equal to): This operator compares the values of two operands and returns true if the left operand is less than or equal to the right operand. For example: if (a <= b)
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.