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

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

  • PROGRAMMING IN C# 10 - Basic Techniques
    • Mario De Ghetto(Author)
    • 2022(Publication Date)
    • Youcanprint
      (Publisher)
    The same variable used to store the result can also be used in the expression. In this case, first the result to the right of the assignment operator is calculated, then the result is assigned to the variable indicated to the left of that operator:
      counter = counter + 1;  
    If before the counter operation was valid 10 , the operation 10+1 is performed and the value 11 is assigned to the counter . After the execution of this instruction, counter will have the value 11 .
     
    NOTE – We'll see alternative methods of performing a unit increment of a variable in a moment, with the increment operator and with compound assignments.
        Arithmetic operators
    We have just talked about this, but we still want to highlight the existence of arithmetic operators, among which the most common are addition, subtraction, multiplication, and division. In Table 12.1 you can see that among the operators we also have operators for integer division (they give the integer result without decimals, that is, without remainder) and for the mathematical modulus (that is, the remainder of integer division).
     
    Table 12.1 – Mathematical operators.
     
    Operator Description
    ++ Unary increment operator
    -- Unary decrement operator
    - Negative unary operator
    + Positive unary operator
       
    (continued) Table 12.1 – Mathematical operators.
     
    Operator Description
    * Multiplication
    / Decimal Division
    + Addition
    - Subtraction
    \ Integer division (result without decimals)
    % Rest of the division (module)
     
    Unary operators are operators that apply to a single operand. For example, the negative unary operator applied to the variable X, that is -X , has the function of inverting the sign of the value contained in the variable: if X = 10 , -X will be equal to -10 ; if X = -10 , -X will be equal to 10 . The others are binary operators that apply to two operands.
      Augmentation operator
    The increment operator, represented by the symbol "++ ", is an operator which increases the variable to which it is applied by one unit. This operator can be prefixed (placed before the variable) or postfixed
  • Ivor Horton's Beginning Visual C++ 2012
    • Ivor Horton(Author)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    + signs in the preceding example of the postfix form is likely to lead to confusion. Generally, it isn’t a good idea to write the increment operator in the way that I have written it here. It would be clearer to write:
    total = 6 + count++;
    Alternatively you could put parentheses around count++ . Where you have an expression such as a++ + b , or even a+++b , it becomes less obvious what is meant or what the compiler will do. They are actually the same, but in the second case, you might really have meant a + ++b , which is different. It evaluates to one more than the other two expressions.
    The rules that I have discussed in relation to the increment operator also apply to the decrement operator, -- . For example, if count has the initial value 5, then the statement
    total = --count + 6;
    results in total having the value 10 assigned, whereas
    total = 6 + count--;
    sets the value of total to 11. Both operators are usually applied to integers, particularly in the context of loops, as you will see in Chapter 3 . You will see in later chapters that they can also be applied to other data types in C++, notably variables that store addresses.

    TRY IT OUT: The Comma Operator

    The comma operator allows you to specify several expressions where normally only one might occur. This is best understood by looking at an example that demonstrates how it works:
    // Ex2_06.cpp // Exercising the comma operator #include <iostream> using std::cout; using std::endl; int main() { long num1(0L), num2(0L), num3(0L), num4(0L); num4 = (num1 = 10L, num2 = 20L, num3 = 30L); cout << endl << "The value of a series of expressions " << "is the value of the rightmost: " << num4; cout << endl; return 0; }
    How It Works If you compile and run this program you will get this output:
    The value of a series of expressions is the value of the rightmost: 30
    This is fairly self-explanatory. The first statement in main() creates four variables, num1 through num4 , and initializes them to zero using functional notation. The variable num4 receives the value of the last of the series of three assignments, the value of an assignment being the value assigned to the left-hand side. The parentheses in the assignment for num4
  • Designing Embedded Systems with 32-Bit PIC Microcontrollers and MikroC
    • Dogan Ibrahim(Author)
    • 2013(Publication Date)
    • Newnes
      (Publisher)
    Table 3.3 gives a list of the arithmetic operators. All these operators, except the autoincrement and autodecrement, require at least two operands. Autoincrement and autodecrement operators are unary as they require only one operand.
    Table 3.3 Arithmetic Operators
    Operator Operation
    + Addition
    Subtraction
    Multiplication
    / Division
    % Remainder (integer division only)
    ++ Autoincrement
    −− Autodecrement
    The arithmetic operators “+, −, , and /” are obvious and require no explanation. Operator “%” gives the remainder after an integer division is performed. Some examples are given below:
    12 % 3     gives 0 (no remainder) 12 % 5     gives 2 (remainder is 2) −10 % 3     gives −1 (remainder is −1)
    The autoincrement operator is used to increment the value of a variable by one. Depending on how this operator is used, the value is incremented before (preincrement) or after (postincrement) an assignment. Some examples are given below:
    i = 5;                        // i is equal to 5 i++;                         // i is equal to 6 i = 5;                        // i = 5 j = i++;                       // i = 6 and j = 5 i = 5;                        // i = 5 j = ++i;                      // i = 6 and j = 6
    Similarly, the autodecrement operator is used to decrement the value of a variable by one. Depending on how this operator is used, the value is decremented before (predecrement) or after (postdecrement) an assignment. Some examples are given below:
    i = 5;                         // i is equal to 5 i−−;                          // i is equal to 4 i = 5;                         // i  = 5 j = i−−;                       // i = 4 and j = 5 i = 5;                        // i = 5 j = −−i;                      // i = 4 and j = 4
    Relational Operators
    The relational operators are used in comparisons. All relational operators are evaluated from left to right and they require at least two operands. Table 3.4
  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    In the subsequent section, we will discuss the feature of all these operators, how to form expressions, how they are executed, and in what order. Finally, we will introduce the precedence of operators.
    On completion of this chapter, students will have learnt the working of different operators, the precedence of operators, and the way the C compiler executes an expression. Some new operators like increment, decrement, ternary, and other special operators are also a part of this chapter.

    6.2 Arithmetic Operators

    We all know what an arithmetic operator is, and the C language does support all these operators with slight deviations with division and modulo operators. Table 6.1 shows the complete set of arithmetic operators and their symbols.
    Table 6.1 Arithmetic Operators
    Serial No. Operator Name Symbol
    1 Addition +
    2 Subtraction
    3 Multiplication *
    4 Division /
    5 Modulus (remainder) %
    We all know the result produced by these operators. For the sake of completeness, Figure 6.1 shows the results obtained by these arithmetic operators. You can observe that the division operation returns the quotient, and the modulus operator returns the remainder.
    Figure 6.1 Arithmetic operations.
    Let us write an example program that shows the result of the divide and modulo operations. Figure 6.2 shows the C program code. You can see that when 6 divides 25, we obtain 4 (quotient), and when we perform the modulo operation, we get the remainder, which is 1.
    Figure 6.2 C program showing operations of arithmetic operators.

    6.3 Relational Operators

    There are situations where we need to compare two values and make decisions. The C language provides several comparison operators for this purpose. The work of these operators is to compare two items for their equality, inequality, or whether one is greater/smaller than the other.
    • These are binary operators and work upon two operands;
    • The result of these operators is either “true” or “false”;
    • If the comparison result is true, then it returns 1, else it returns 0.
    Table 6.2
  • 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.