Lec-04 - W4 (Variables and Constab)
Lec-04 - W4 (Variables and Constab)
C++
Lecture w4
Outline
Variables
Variable Declaration
Variable Initialization
Constant
Names (naming entities)
Keywords
Identifier
Scoping or Visibility of variables
Local Variables
Global Variables
Variable
Variables
location in memory where a value can be stored
for use by a program OR
A variable is a name for a location in memory
must be declared with a name and a data type
before they can be used
Must appear before variable is used
Some common data types are:
• int - integer numbers
• char - characters
• double - floating point numbers
1
Variables Declaration
int total;
Variable Declaration
Prints as:
A piano has 88 keys.
The expression on the right is evaluated and the result is stored as the
value of the variable on the left
The value previously stored in variable (total) is overwritten
You can only assign a value to a variable that is consistent with the
variable's declared type
2
Constants
Constants
Constants are useful for three important reasons
Third, they formally show that a value should not change, avoiding
inadvertent errors by other programmers
3
Keywords
Keywords are words reserved as part of
the language
• int, return, float, double
10
11
C++ Keywords
noexcept
static_assert union
nullptr
static_cast unsigned
operator
struct using
private
switch virtual
protected
template void
public
this volatile
register
thread_local wchar_t
reinterpret-cast
throw while
return
true
short
try
signed
typedef
sizeof
typeid
static
typename
12
4
Identifiers
• Identifier = name – for variable, function,
constant, class, type, etc.
• Cannot be a keyword in C++
• Identifiers may be composed of letters, digits,
and underscore char
• Must begin with _ or letter
• Identifiers are case-sensitive
• Main and MAIN are not treated similarly in
C++.
13
Identifier Conventions
14
15
5
Local Variables
Variables that are declared inside a function or a block are called Local
Variables.
1) Local variables must be declared before they have used in the
program.
2) Local variables can only be used inside the function or block.
3) These variables are created when the "function is called" or "the block
in entered" and destroyed after exiting from "the block" or when
the "call returns from the function".
4) Initilisation of Local variables is mandatory
For Example: // declaration of local variable
int main() {
int x = 2; // Local variable
}
16
17
Global Variables
Global variables are defined outside of all the functions, usually on
top of the program.
A global variable can be accessed by any function.
The global variables will hold their value throughout the life-time of a
program.
For Example: // declaration of Global variables
int y = 10; // Global variable
int main() {
int x = 5; // Local variable
18
6
Advantages of Global variables
19
20
1. #include <iostream>
2. using namespace std;
3. // Global variable declaration
4. int g = 20;
5. int main () {
6. // Local variable declaration
7. int g = 10; When the above code is compiled and
8. cout << g; executed, it produces the following
9. return 0; result −
10. }
10
21
7
#include <iostream>
Accessing Global Variable using namespace std;
You can access a global // Global variable declaration:
variable when there is a local
int g = 20;
variable with the same name
by using the SRO (Scope int main() {
Resolution Operator) :: before // Local variable declaration:
the name of that variable.
int g = 10;
cout << "Value of g (Local variable):" << g;
cout << endl;
When the above code is cout << "Value of g (Global variable):" << ::g;
compiled and executed, it
return 0;
produces the following result −
}
Value of g (Local variable): 10
Value of g (Global variable): 20
22
Thank You
23