0% found this document useful (0 votes)
52 views

Lvalues and Rvalues

Lvalues refer to expressions that refer to a memory location and can appear on either side of an assignment. Rvalues refer to data values stored in memory and can only appear on the right side of an assignment. Variables are lvalues while literals are rvalues. C++ has various storage classes like auto, register, static, and extern that define the scope and lifetime of variables. It also has type qualifiers like const and volatile and various operators for arithmetic, comparison, logic, bitwise, and assignment operations.

Uploaded by

Gagan Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Lvalues and Rvalues

Lvalues refer to expressions that refer to a memory location and can appear on either side of an assignment. Rvalues refer to data values stored in memory and can only appear on the right side of an assignment. Variables are lvalues while literals are rvalues. C++ has various storage classes like auto, register, static, and extern that define the scope and lifetime of variables. It also has type qualifiers like const and volatile and various operators for arithmetic, comparison, logic, bitwise, and assignment operations.

Uploaded by

Gagan Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lvalues and Rvalues:- There are two kinds of expressions in C++.

1. lvalue − Expressions that refer to a memory location is called "lvalue" expression.


An lvalue may appear as either the left-hand or right-hand side of an
assignment.
2. rvalue − The term rvalue refers to a data value that is stored at some address in
memory. An rvalue is an expression that cannot have a value assigned to it
which means an rvalue may appear on the right- but not left-hand side of an
assignment.
Variables are lvalues and so may appear on the left-hand side of an assignment.
Numeric literals are rvalues and so may not be assigned and can not appear on the left-

A scope is a region of the program and broadly speaking there are three places, where
variables can be declared.

 Inside a function or a block which is called local variables,


 In the definition of function parameters which is called formal parameters.
 Outside of all functions which is called global variables.

Local Variables:- Variables that are declared inside a function or block are
local variables. They can be used only by statements that are inside that
function or block of code.

Global Variables:- Global variables are defined outside of all the functions,
usually on top of the program. The global variables will hold their value
throughout the life-time of your program. A global variable can be accessed
by any function.

Type Qualifiers in C++

The type qualifiers provide additional information about the variables they
precede.

Sr.No Qualifier & Meaning


const
1
Objects of type const cannot be changed by your program during
execution.
volatile
2
The modifier volatile tells the compiler that a variable's value may be
changed in ways not explicitly specified by the program.
restrict

3 A pointer qualified by restrict is initially the only means by which the


object it points to can be accessed. Only C99 adds a new type
qualifier called restrict.

Storage Class in C++

A storage class defines the scope (visibility) and life-time of variables and/or
functions within a C++ Program. These specifiers precede the type that they
modify. There are following storage classes, which can be used in a C++
Program

 auto
 register
 static
 extern
 mutable

The auto Storage Class

The auto storage class is the default storage class for all local variables.

The register Storage Class

The register storage class is used to define local variables that should be
stored in a register instead of RAM. This means that the variable has a
maximum size equal to the register size (usually one word) and can't have
the unary '&' operator applied to it (as it does not have a memory location).

{
register int miles;
}

The register should only be used for variables that require quick access such
as counters. It should also be noted that defining 'register' does not mean
that the variable will be stored in a register. It means that it MIGHT be
stored in a register depending on hardware and implementation restrictions.

The static Storage Class

The static storage class instructs the compiler to keep a local variable in
existence during the life-time of the program instead of creating and
destroying it each time it comes into and goes out of scope. Therefore,
making local variables static allows them to maintain their values between
function calls.

The static modifier may also be applied to global variables. When this is
done, it causes that variable's scope to be restricted to the file in which it is
declared.

In C++, when static is used on a class data member, it causes only one copy
of that member to be shared by all objects of its class.

The extern Storage Class

The extern storage class is used to give a reference of a global variable that
is visible to ALL the program files. When you use 'extern' the variable cannot
be initialized as all it does is point the variable name at a storage location
that has been previously defined.

When you have multiple files and you define a global variable or function,
which will be used in other files also, then extern will be used in another file
to give reference of defined variable or function. Just for understanding
extern is used to declare a global variable or function in another file.

The extern modifier is most commonly used when there are two or more
files sharing the same global variables or functions as explained below.

The mutable Storage Class

The mutable specifier applies only to class objects, which are discussed
later in this tutorial. It allows a member of an object to override const
member function. That is, a mutable member can be modified by a const
member function.

Operator in C++

An operator is a symbol that tells the compiler to perform specific


mathematical or logical manipulations. C++ is rich in built-in operators and
provide the following types of operators −

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ are as follows −

p q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

The Bitwise operators supported by C++ language are listed in the following
table. Assume variable A holds 60 and variable B holds 13, then −

Operator Description Example


& Binary AND Operator copies a (A & B) will give 12 which is
bit to the result if it exists in 0000 1100
both operands.
| Binary OR Operator copies a (A | B) will give 61 which is
bit if it exists in either 0011 1101
operand.
^ Binary XOR Operator copies (A ^ B) will give 49 which is
the bit if it is set in one 0011 0001
operand but not both.
~ Binary Ones Complement (~A ) will give -61 which is
Operator is unary and has the 1100 0011 in 2's complement
effect of 'flipping' bits. form due to a signed binary
number.
<< Binary Left Shift Operator. The A << 2 will give 240 which is
left operands value is moved 1111 0000
left by the number of bits
specified by the right operand.
>> Binary Right Shift Operator. A >> 2 will give 15 which is
The left operands value is 0000 1111
moved right by the number of
bits specified by the right
operand.

Assignment Operators

There are following assignment operators supported by C++ language −

Operator Description Example


= Simple assignment operator, C = A + B will assign value of
Assigns values from right side A + B into C
operands to left side operand.
+= Add AND assignment operator, It C += A is equivalent to C = C
adds right operand to the left +A
operand and assign the result to
left operand.
-= Subtract AND assignment C -= A is equivalent to C = C
operator, It subtracts right -A
operand from the left operand
and assign the result to left
operand.
*= Multiply AND assignment C *= A is equivalent to C = C
operator, It multiplies right *A
operand with the left operand
and assign the result to left
operand.
/= Divide AND assignment operator, C /= A is equivalent to C = C
It divides left operand with the /A
right operand and assign the
result to left operand.
%= Modulus AND assignment C %= A is equivalent to C =
operator, It takes modulus using C%A
two operands and assign the
result to left operand.
<<= Left shift AND assignment C <<= 2 is same as C = C
operator. << 2
>>= Right shift AND assignment C >>= 2 is same as C = C
operator. >> 2
&= Bitwise AND assignment C &= 2 is same as C = C & 2
operator.
^= Bitwise exclusive OR and C ^= 2 is same as C = C ^ 2
assignment operator.
|= Bitwise inclusive OR and C |= 2 is same as C = C | 2
assignment operator.

Misc Operators

The following table lists some other operators that C++ supports.

Sr.No Operator & Description


1 sizeof

sizeof operator returns the size of a variable. For example, sizeof(a),


where ‘a’ is integer, and will return 4.
2 Condition ? X : Y

Conditional operator (?). If Condition is true then it returns value of


X otherwise returns value of Y.
3 ,

Comma operator causes a sequence of operations to be performed.


The value of the entire comma expression is the value of the last
expression of the comma-separated list.
4 . (dot) and -> (arrow)

Member operators are used to reference individual members of


classes, structures, and unions.
5 Cast

Casting operators convert one data type to another. For example,


int(2.2000) would return 2.
6 &

Pointer operator & returns the address of a variable. For example


&a; will give actual address of the variable.
7 *

Pointer operator * is pointer to a variable. For example *var; will


pointer to a variable var.

You might also like