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

7 Key excerpts on "Assignment Operator in C"

  • Book cover image for: C# 2.0
    eBook - PDF

    C# 2.0

    Practical Guide for Programmers

    • Michel de Champlain, Brian G. Patrick(Authors)
    • 2005(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    c h a p t e r 5 Operators, Assignments, and Expressions O perators, assignments, and expressions are the rudimentary building blocks of those programming languages whose design is driven in large part by the underlying architecture of the von Neumann machine. And C# is no exception. An expression in its most basic form is simply a literal or a variable. Larger expres-sions are formed by applying an operator to one or more operands (or expressions). Of all operators, the most fundamental is the assignment operator that stores the result of an expression in a variable. Because variables are expressions themselves, they can be used as operands in other expressions and hence, propel a computation forward. In this chapter, we present all variations of the arithmetic, conditional, relational, and assignment operators in C# . We discuss which simple types and objects are valid for each operator and what types and values are generated for each expression. Because most operators in C# are derived from the lexicon of C/C++, explanations are relatively short but always augmented with simple examples. To disambiguate the order of expression evaluation, the rules of precedence and associativity are also presented along with the powerful notion of operator overloading that was first introduced in Chapter 3. 5.1 Operator Precedence and Associativity An expression in C# is a combination of operands and operators and is much like expres-sions in C. An operand is a literal or a variable, and an operator acts upon the operands to return to a single value. Table 5.1 lists all the operators in order of precedence from highest ( Primary ) to lowest ( Assignment ). Operators with the same precedence appear on the same line of the table. However, before presenting the operators starting from those 83
  • Book cover image for: Programming Microcontrollers in C
    • Ted VanSickle(Author)
    • 2001(Publication Date)
    • Newnes
      (Publisher)
    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. The standard provides that between sequence points, an object shall have its value modified at most once and the prior value of the object shall be accessed only to determine its value. Interpretations of the above requirements disallow statements such as j = j++; or a[j] = j++; or m = j++ + ++j; Assignment Operators Another shorthand that was included in C is called the assign-ment operator. When you are programming, you will find that expressions such as i = i+2; or x = x<<1; are used often. Almost any binary operator can be found on the right side of the expression. A special set of operators was created in C to simplify these expressions. The first expression can be written i += 2; and the second x <<= 1; These expressions use what is defined as an assignment operator. The operators that can be used in assignment operators are + >> -<< * & 33 / ^ % | If you have two expressions e1 and e2 , and let the operand $ repre-sent any binary C operator, then e1 $= e2; is equivalent to e1 = (e1) $ (e2); The precedence of all of the operator assignments are the same and less than the precedence of the conditional operator discussed in the next section. These operators assignments and the = operator are associated from right to left. The Conditional Expression Another code sequence found frequently is if(exp1) exp2 ; else exp3 ; The logical expression exp1 is evaluated. If that expression is TRUE, exp2 is executed. Otherwise, exp3 is executed. In the compact no-tation of C, the above code sequence can be written exp1 ? exp2 : exp3; This expression is read if exp1 is TRUE , execute exp2 .
  • Book cover image for: An Introduction to Programming with C++
    (Your instructor may require you to use integers in monetary calculations; however, for simplicity, this book will use real numbers.) Arithmetic Assignment Operators In addition to the standard arithmetic operators listed earlier in Figure 4-7, C++ also provides several arithmetic assignment operators , which can be used to abbreviate assignment statements that contain an arithmetic operator. However, the assignment statement must have the following format, in which variableName is the name of the same variable: variableName = variableName arithmeticOperator value; . For example, you can use the multiplication assignment operator (*=) to abbreviate the statement price = price * 1.05; as follows: price *= 1.05; . Both statements tell the computer to multiply the contents of the price variable by 1.05 and then store the result in the price variable. Arithmetic assignment operators are also called compound assignment operators or shortcut operators. Figure 4-13 shows the syntax of a C++ statement that uses an arithmetic assignment operator. The figure also lists the most commonly used arithmetic assignment operators; each consists of an arithmetic operator followed immediately by the assignment operator (=). The arithmetic assignment operators do not contain a space; in other words, the addition assignment operator is +=, not + =. Including a space in an arithmetic assignment operator is a common syntax error. Also included in the figure are examples of using the operators. To abbreviate an assignment statement, you simply remove the variable name that appears on the left side of the assignment operator (=), and then put the assignment operator immediately after the arithmetic operator. In the assignment statement’s syntax, value is usually either a constant (literal or named) or the name of a different variable.
  • Book cover image for: Mathematical Objects in C++
    eBook - PDF

    Mathematical Objects in C++

    Computational Tools in A Unified Object-Oriented Approach

    • Yair Shapira(Author)
    • 2009(Publication Date)
    • CRC Press
      (Publisher)
    A command in C is also a function that not only carries out some operation but also returns a value. In particular, the assignment operator ’=’ used above not only assigns a value to a particular variable but also returns this value as an output for future use. This feature can be used to write x = a = i = 0; This command is executed from right to left. First, the integer value 0 is assigned to ’i’. This assignment also returns the assigned integer number 0, which in turn is converted implicitly to the real number 0 . and assigned to the ”float” variable ’a’. This assignment also returns the (single-precision) real number 0 . , which in turn is converted implicitly to the (double-precision) real number 0 . and assigned to the ”double” variable ’x’. Thus, the above command is the same as i = 0; a = 0.; x = 0.; 13.7 Initialization The above approach, in which the variables initially contain meaningless values before being assigned meaningful values, is somewhat inefficient. After all, why not assign to them with meaningful values immediately upon defi-nition? Fortunately, one can indeed avoid the above assignment operation by defining and initializing the variables at the same time: int i = 0; float a = 0.; double x = 0.; char c = ’A’; Here, the ’=’ symbol stands not for an assignment to an existing variable as before but rather for an initialization of a new variable that is being defined now. Unlike assignment, initialization returns no value, so it is impossible to write double x = double y = 0.; /* error! */ 258 CHAPTER 13. BASICS OF PROGRAMMING Here, the characters ”/ * ” indicate the start of a comment, which ends with the characters ” * /”. Such comments are skipped and ignored by the C com-piler; their only purpose is to explain the code to the reader. (C++ has another form of comment: the characters ”//” indicate the start of a comment line ignored by the C++ compiler.) Usually, comments are used to explain briefly what the code does.
  • Book cover image for: 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.
  • Book cover image for: C++ for Financial Mathematics
    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! 32 C++ for Financial Mathematics 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.
  • Book cover image for: Basic Computation and Programming with C
    When parenthetical expressions are nested, execution will start from innermost parenthetical expression and continued from inner to outer. Note 2: Postfix increment and decrement operators have higher precedence, but the actual increment or decrement of the operand is delayed until the other related operations are not completed. So in the statement a = b/c++; the current value of c is used to evaluate the expression and after completion of all operations in the statement, c will be incremented. Operators and Expressions 87 S UMMARY Operators are the symbols that help the programmer to instruct the computer to perform certain arithmetic, relational or logical operations. Arithmetic operators are +, −, *, / and %. Assignment Operator is =. Shorthand assignment operators are +=, −=, *=, /= and %=. ++ and −− are increment and decrement operators, respectively. Relational operators are >, ≥, <, ≤, == and !=. Logical operators are &&, || and !. Conditional operator is ?:. Bitwise operators are &, |, ^, ~, » and «. An expression is a combination of variables, constants, operators, and functions that evaluates to a certain value. When a conversion occurs automatically by the compiler, it is called implicit type conversion. When a particular data type is converted to another data type forcefully, the process is known as explicit conversion or type casting. Precedence determines the priority or order in which the operations are performed. Associativity is the right-to-left or left-to-right order of execution for a group of operands to operators that have the same precedence. R EVIEW E XERCISES 1. What do you mean by operator? 2. What are the different types of operators available in C? 3. What are the unary operators available in C? 4. Is there any ternary operator available in C? 5. What do you mean by pre-increment and post-increment? 6. What is the use of sizeof( ) operator? 7. What type of value is returned on execution of relational operator? 8.
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.