Variables
Variables are containers for storing data values. The variables are created in RAM. The value of
variable can change during program execution. A program can have as many variables as needed. If a new
value is stored in the variable, it replaces the previous value.
In C++, each variable has a specific type, which defines how much memory it uses, how that
memory is arranged, the range of values it can hold, and the operations that can be performed on it. The
name of a variable can include letters, numbers, and the underscore symbol. Since C++ is case-sensitive,
uppercase and lowercase letters are treated as different.
Example:
Marks = 10;
It create the marks name variable and store 10 in it.
Name of variable : It refers to an identifier that represents a memory location.
Address of variable : It refers to the memory location of the variable.
Contents of variable : It refers to the value stored in memory location referred by variable.
1. Concept/Purpose of Variable
Variable are used to store information to be referenced and manipulated in a computer program. The
main objective of a variable is to store the data so that it can be retrieved or update any time. Variables are
basically used in C++ to store the data and use it anywhere in the code and by using this data we can also
manipulate, calculate or perform any operation on data. Variables plays a very important role to perform any
task or any other operation in c++.
2. Concept of Data Types
Data type is a data storage format that can contain specific type or range of values.
1
Here are some basic data type detail:
Int Data Type
It Stores whole numbers, without decimals .Integer occupies 2 bytes memory space and its values
range limited to -32768 to [Link] keyword int is used to declare variable of int types.
Example:
int a;
int marks = 99;
int x,y;
Float DataType
Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal
digits.A variable of float type requires 4 [Link] keyword float is used to declare variable of float types.
Example:
float b = 1.0;
float height;
float l,w;
Char Data Type
It stores a single character,letter,number, or ASCII [Link] Char data type takes only 1 [Link]
Char data type always written on single quotes .The keyword char is used to declare variable of character
types.
Example:
char c;
char grade = ‘A’;
char subject,m;
String Data Type
It stores multiple characters in a variable. In string character must be surrounded by double quotes.
The keyword string is used to declare variable of string types.
Example:
string name = “Zikriya”;
String course;
String s,student;
3. Variable Declaration
The process in which we describe the variable name and its type .In the variable declaration we
cannot assign a value to a variable in the declaration. The variable declaration provides information to the
compiler about the variable. The compiler uses the declaration to determine how much memory is needed
for each variable. So, in the declaration we just declare a variable that shown its type and name only and this
process known as variable declaration.
Syntax:
data_type variable_name;
Data_type : It indicates type of data that can be stored in variable.
Variable_name : It refers to the memory location of the variable.
Example:
int s;
float height;
char m,name;
2
4. Syntax
Syntax are those rules or laws and regulation that can be followed in the program language. It defines
the arrangement of words and phrases to create well-formed sentence in a C++. It specifying your algorithm
using a programming language. If those rules or laws cannot be followed it generates an error. It is also the
type of error. It is also specify the structure and organization of the code.
Example:
Int num;//Incorrect because according to the C++ is case sensitive programming language.
int num;// Correct
5. Semantic
Semantics is the meaning of program. Basically, It refers the meaning behind the code written in
c++.It is meaning of those expressions, statements and program units that are written in programming
language. It is also the opposed of syntax. It is important for writing correct and efficient programs by
knowing there meaning.
Semantic analysis checks if code statements are meaningful.
Example:
int x;
float x; // semantic error: re-declaration of x
int x;
std::cout << x; // Value of x is undefined, leading to unpredictable output.
int myInt = "hello"; // Cannot assign a String to an int variable.
3
Rules for Naming a Variable
Variable may include letters, numbers and underscore (_) only. If there is any other thing used in
variable name then compiler generates a syntax error.
Example:
hope1;
s_sign;
name1;
First character of variable must be start with the letter or an underscore (_) only. It cannot be start
with digit or any symbol. If there is any other thing is written in variable name then compiler generates a
syntax error.
Example:
_type;
section;
Blank spaces are not allowed in variable names. If the spaces are used in variable name then compiler
generates an syntax error.
Example:
s sign;
t marks;
Special symbols such as @, $, % are not allowed in variable [Link] the special symbol are used in
variable name then compiler generates a syntax error.
Example:
@com;
Tot%al;
Success&;
Case Sensitive
Variables names are case sensitive because compiler can difference between upper and lower case. If
the same name variable with only difference is the upper and lower case between them used in same
program then those variable consider to be different.
Example:
mark != Mark;
Reserved word
Reserved word cannot be used as variable name. If the reserved word can be used as the variable
name then compiler generates a syntax error.
Example:
The long, double, void e.t.c. cannot be used as variable name.
Declared data type
A variable can be declared only for one data type. If it is more than one data type used in variable
name then compiler generates a syntax error.
Example:
int a;
float b,c;
Range of Variable Name
A variable can be up to 31 characters long for many compilers. If the variable name exceed 31
character limit then it only show first 31 characters and ignore remaining characters.
4
Example:
char name;
string subject;
6. Variable Initialization
In the Variable Initialization we can not only declare a variable we also assign a value to variable by
using ( = ) [Link] name should be written on the left side and its value written on the right side.
Garbage value: If a variable declared but not initialized, it may contain meaningless data. Then it produce
an unexpected results in some computations. All variables should be initialized to avoid this problem.
Syntax:
data_type variable_name = value;
Examples:
int n = 100;
int x = 50, y = 75;
float average = 50.73;
7. Accessing Values of a Variable
The most common way to access a value of a variable in C++ is that calling a variable by its name.
We can access a value of the variable at any time or anywhere in the program by calling its name.
Every variable have its own address in which the variables store the value of that variable who is
assigned the value and By calling the variable name in the same program then the compiler retrieve the
value stored in the variable memory or address.