Computer Science

Assignment Operator in C

The assignment operator in C is used to assign a value to a variable. It is represented by the equal sign (=) and can be used with various data types such as integers, characters, and floating-point numbers. The value on the right side of the operator is assigned to the variable on the left side.

Written by Perlego with AI-assistance

5 Key excerpts on "Assignment Operator in C"

  • Learn C Programming from Scratch
    eBook - ePub

    Learn C Programming from Scratch

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

    assignment operators . Some of the commonly used assignment operators in C are:
    • = (Equal to ): This operator assigns a value to a variable. For example: x = 5;
    • += (Add and assign ): This operator adds the value on the right side to the value on the left side and assigns it to the left side variable. For example: x += 5; (x = x + 5;)
    • -= (Subtract and assign ): This operator subtracts the value on the right side from the value on the left side and assigns it to the left side variable. For example: x -= 5; (x = x – 5;)
    • *= (Multiply and assign ): This operator multiplies the value on the right side with the value on the left side and assigns it to the left side variable. For example: x *= 5; (x = x * 5;)
    • /= (Divide and assign ): This operator divides the value on the left side by the value on the right side and assigns it to the left side variable. For example: x /= 5; (x = x / 5;)
    • %= (Modulus and assign ): This operator finds the modulus of the value on the left side and the value on the right side and assigns it to the left side variable. For example: x %= 5; (x = x % 5;)
  • Java
    eBook - ePub

    Java

    The Comprehensive Guide

    • Christian Ullenboom(Author)
    • 2022(Publication Date)
    • SAP PRESS
      (Publisher)
    value operation because the value of the variable is considered (and not its location or even its variable name).
    Only after the expression has been evaluated does the assignment operator copy the result into the variable. If runtime errors occur, for example, due to a division by zero, no write access to the variable is possible.
    [»]   Language Comparison
    The simple equals sign = is only used for assignment in Java, which is also the case in almost all programming languages. Rarely, a programming language uses a different symbol for the assignment, such as := in Pascal or <- in F# and R. To separate assignments from comparisons, Java follows the C(++) tradition and defines a binary comparison operator == with two equal signs. The comparison operator always returns the boolean result type, as in the following code:
    int
    quantity = 1
    ; System.out.println (
    quantity == 1
    ); // "true": Expression with comparison System.out.println (
    quantity = 2
    ); // "2": Expression with assignment
    Assignments Are Also Expressions
    Although assignments are often found as expression statements , assignments can be found anywhere an expression is allowed, for example, in a method call like print*(...) :
    int
    quantity = 1
    ; // Declaration with initialization
    quantity = 2
    ; // Statement with assignment System.out.println (
    quantity = 3
    ); // Expression with assignment. Delivers 3.
    Multiple Assignments in One Step
    Assignments of the type a = b = c = 0; are allowed and are equivalent to the three statements c = 0; b = c; a = b; .
    The explicit parentheses a = (b = (c = 0)) make it clear once again that assignments can be nested, and assignments like c = 0
  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    6 Operators and Expressions
    DOI: 10.1201/9781003188254-6

    6.1 Introduction

    This chapter will discuss the two most essential parts of any programming language: operators and expressions. An operator is a symbol that tells the computer to perform specific mathematical or logical operations. Operators are used in programs to manipulate data and variables. C is very rich in built-in operators.
    An operator is a symbol that tells the computer to perform specific mathematical or logical operations.
    Operators can be either binary or unary. Binary operators are the operators where one operator will act upon two operands.
    For example:
    12 + 14
    In this example, the operator + is acted upon two operands 12 and 14. Hence + is a binary operator here. Similarly, a unary operator has one operator and one operand.
    For example:
    25
    In this example, the operator – is acted upon one operand 25. Hence – is a unary operator here. The following are the operators used in C. These operators may be used as binary or unary operators.
    1. Arithmetic operators;
    2. Relational operators;
    3. Assignment operators;
    4. Logical operators;
    5. Increment or decrement operators;
    6. Conditional operators;
    7. Bitwise operators;
    8. Special operators.
    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.
  • C++ for Financial Mathematics
    As we have already commented, to use a variable in an expression, we have to have said what the type of the variable is at some point. This is why we need to declare the type of the variable before we can use it in assignment statements.
    As a convenient shorthand, C++ allows you to combine assignment with arithmetic in a single operator. For example, suppose that you want to add 3 to an integer variable called i then instead of typing i=i+3 , you only need to type i+=3 .
    There is also an assignment operator -= for combining assignment with subtraction, and similarly there are operators *= and /= .
    The following code is valid C++
        int i = 3;    int j = 0;    j = (i += 4);    cout << "The value of i is " << i << "\n";    cout << "The value of j is " << j << "\n";
    It prints out that the value of i is 7 as is the value of j . The way this code works is that the expression i+=4 adds 4 to the value of i to compute 7. The expression i+=4 is then itself considered to take the value 7, which is the value assigned to j .
    Here is a slightly more complex example:
        int i = 3;    int j = 0;    j = 3 * (i += 4);    cout << "The value of i is " << i << "\n";    cout << "The value of j is " << j << "\n";
    This time i works out as 7 and j works out as 21.
    In general, an assignment expression both assigns a value and has a value, which can be used for further computations.
    Having said this, using the value of assignment expressions in this way makes your code very confusing, so you shouldn’t actually do it. Unfortunately, you will sometimes do it by accident. Consider the following code:
        bool i = false ;    bool j = true ;    bool areIAndJEqual = (i = j);    cout << "The value of areIAndJEqual is ";    cout << areIAndJEqual << "\n";    cout << "The value of i is ";    cout << i << "\n";
    The problem here is that the code contains a subtle bug. The expression i=j should probably read i==j and this tiny difference changes the meaning of the code!
    The next assignment operator is ++ . It adds one to a variable. For example, try the following code:
        int c = 5;    c++;    cout << c << "\n";
    It should print out 6. This is why C++ is called C++. It is the next thing after C. Did I mention that computer programmers like weak jokes?
  • PROGRAMMING IN C# 10 - Basic Techniques
    • Mario De Ghetto(Author)
    • 2022(Publication Date)
    • Youcanprint
      (Publisher)
    decision in the program flow . Let's see, then, in detail what are the types of operators we have available. The operators can be classified into the following types:
     
    assignment operators;
    arithmetic operators;
    comparison operators;
    boolean logical operators;
    bit by bit and shift operators;
    equality operators;
        Assignment operators
    The assignment operator used in Visual Studio is the same one that is used by many other languages: "= ".
     
    NOTE – This symbol is NOT used to compare two values. In C# we compare two values using a different symbol: "== ", that is, two equality symbols side by side.
     
    The value to be assigned or the expression are placed to the right of the assignment operator, while the name of the variable to be modified is placed to the left. For example, to assign the value “http://deghettoen.wordpress.com ” to a string variable named URL , or to assign the result of an expression to an integer variable, you can proceed as follows:
      string URL = "https://deghettoen.wordpress.com"; // Blog Address int result = 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21;// Sum of the first 8 // Sequence numbers of Fibonacci  
    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
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.