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

2 Key excerpts on "Assignment Operator in C"

  • 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.
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.