0% found this document useful (0 votes)
22 views8 pages

Lec-04 - W4 (Variables and Constab)

The document covers fundamental concepts of C++ programming, including variable declaration, initialization, constants, and naming conventions. It explains the differences between local and global variables, their scopes, and provides examples of their usage. Additionally, it discusses the importance of constants and the rules for naming identifiers in C++.

Uploaded by

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

Lec-04 - W4 (Variables and Constab)

The document covers fundamental concepts of C++ programming, including variable declaration, initialization, constants, and naming conventions. It explains the differences between local and global variables, their scopes, and provides examples of their usage. Additionally, it discusses the importance of constants and the rules for naming identifiers in C++.

Uploaded by

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

Programming Fundamental

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

A variable must be declared by specifying the variable's name


and the type of information that it will hold
data type variable name

int total;

Multiple variables can be created in one declaration:

int count, temp, result;


4

Variable Declaration

A variable can be given an initial value in the declaration with an equal


sign int sum = 0;
int base = 32, max = 149;

When a variable is referenced in a program, its current value is used


int keys = 88;
cout << “A piano has ” << keys << “ keys.”);

Prints as:
A piano has 88 keys.

Assigning Value to a Variable


An assignment statement changes the value of a variable
The equal sign is used as a assignment operator
total = 55;

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

A constant is like a variable except that it holds the same value


during its entire existence.

As the name implies, it is constant, not variable

In C++, we use the reserved word const in the declaration of a


constant

const int myNum = 15; // myNum will always be 15

const float PI = 3.14;

Constants
Constants are useful for three important reasons

First, they give meaning to otherwise unclear literal values


 For example, NUM_STATES means more than the literal 50

Second, they facilitate program maintenance


 If a constant is used in multiple places and you need to change its value later, its value
needs to be updated in only one place

Third, they formally show that a value should not change, avoiding
inadvertent errors by other programmers

Names (naming entities)

 Used to denote program values or components


 A valid name is a sequence of
 Letters (upper and lowercase)
 A name cannot start with a digit
 Names are case sensitive
 MyObject is a different name than MYOBJECT
 There are two kinds of names
 Keywords
 Identifiers
9

3
Keywords
Keywords are words reserved as part of
the language
• int, return, float, double

They cannot be used by the programmer


to name things

They consist of lowercase letters only

They have special meaning to the compiler


10

10

Examples of Keywords Used in C++


break extern
case false
catch float
char for
class friend
Const goto
do if
double int
else long
enum
explicit
export

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

Identifier should hint toward purpose


Constants are all upper case
Variables are lower case
Classes start with upper case
Multi-word identifiers should distinguish each
word using a capital or underscore
Sales_item, booksSold, totalRbis

14

Scoping or Visibility of variables

C++ variables scopes are categorized mainly


in two parts −
 Local Variables
 Global Variables

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

Advantages of Local Variables…

The use of local variables offer a guarantee that the values of


variables will return intact while the task is running.
local variables are deleted as soon as any function is over and
release the memory space it occupies.
we can give local variables the same name in the different
functions because they are only recognized by the function they
are declared in.

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

 It is ideally used for storing "constants" as it helps you


keep the consistency.
 A Global variable is useful when multiple functions are
accessing the same data.
 You only require to declare global variable single time
outside the modules.

19

Example using global and local variables


1. #include <iostream>
2. using namespace std;
3. // Global variable declaration
4. int g;
5. int main () {
6. // Local variable declaration
7. int a, b;
8. // actual initialization
9. a = 10;
10. b = 20;
11. g = a + b;
12. cout << g;
13. return 0;
14. }

20

Local and Global Variables with Same Names

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

You might also like