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

C++ Variables and Constants

Uploaded by

sambhavdwivedi48
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C++ Variables and Constants

Uploaded by

sambhavdwivedi48
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

C++ Variables and

Constants
1. C++ Variables :)
• Variables in C++ is a name given to a memory location.
It is the basic unit of storage in a program.
• The value stored in a variable can be changed during
program execution.

• A variable is only a name given to a memory location,


all the operations done on the variable effects that
memory location.

• In C++, all the variables must be declared before use.


2. How to Declare Variables?
• A variable name can consist of alphabets (both upper
and lower case), numbers, and the underscore ‘_’
character. However, the name must not start with a
number.

• int var = 30;

int : datatype: Type of data that can be stored in this variable.


var : variable_name: Name given to the variable.
30 : value: It is the initial value stored in the variable.
; : Semicolon.
3. Rules For Declaring Variable
• The name of the variable contains letters, digits, and underscores.

• The name of the variable is case sensitive (ex Arr and arr both are
different variables).

• The name of the variable does not contain any whitespace and
special characters (ex #,$,%,*, etc).

• All the variable names must begin with a letter of the alphabet or
an underscore(_).

• We cannot used C++ keyword(ex float,double,class)as a variable


name.
4. Valid variable names:
• Int x = 12;
• Int y12 = 134;
• Int _yz = 123;
5. Invalid variable names:
• Int 789; // Should not be a number
• Int X Y; // Should not contain any whitespace
• Int float // Should not be used any reserved keyword in C++.
6. Difference Between Variable
Declaration and Definition
• The variable declaration refers to the part where a
variable is first declared or introduced before its first
use.
• A variable definition is a part where the variable is
assigned a memory location and a value.
• Most of the time, variable declaration and definition are
done together.
• #include <iostream>
• using namespace std;
• int main() {
• // this is declaration of variable a
• int a;
• // this is initialisation of a
• a = 10;
• // this is definition = declaration + initialisation
• int b = 20;
•}
8. What is a constant in C++?
• As the name suggests, a constant in C++ is a variable
that cannot be modified once it is declared in the
program.
• We can not make any change in the value of the
constant variables after they are defined.
9. How to Define Constant in C+
+?
• We define a constant in C++ language using
the const keyword.

10. Syntax to Define Constant.


: Const data_type var_name =
value;
Ex : const int var = 5;
11. Scope of Variables in C++.
• In programming also the scope of a variable is defined
as the extent of the program code within which the
variable can be accessed or declared or worked with.
• Local Variables.
• Global Variables.
12. Local Variables :)
• Variables defined within a function or block are said to
be local to those functions.
• Anything between ‘{‘ and ‘}’ is said to inside a block.

• Local variables do not exist outside the block in which


they are declared, i.e. they can not be accessed or
used outside that block.

• Declaring local variables: Local variables are


declared inside a block.
Example
13. Global Variables
• As the name suggests, Global Variables can be accessed
from any part of the program.
• They are available through out the life time of a program.

• They are declared at the top of the program outside all of


the functions or blocks.

• Declaring global variables: Global variables are


usually declared outside of all of the functions and
blocks, at the top of the program. They can be accessed
from any portion of the program.
Example :)
14. How to access a global variable when
there is a local variable with same name?
• To solve this problem we will need to use the
scope resolution operator.

You might also like