You are on page 1of 9

C++ Introduction – Day 1


C++ Variables


Variable is storage area in computers memory


It holds data during the life cycle of program


To access information or data stored in storage area each variable
should be given unique name (unique is also called identifier)


While defining variable we should define data type (we will learn about
data types in coming days)


Value stored in storage can change during hence the name Variable
(The thing that keeps on changing)
C++ Introduction – Day 1


Why we need variables

Assume that we are writing a program that will take one input e.g.
NameOfPerson. Once the name is input to program it will print
“Hello “ + NameOfPerson on the screen


To understand the concept open C++ IDE (Click hyperlink or go to
https://www.online-ide.com/online_c++_ide)


Type below program in the input area. You may not understand
everything right now but that’s Ok. You can copy paste also but
typing helps in understanding and memorise syntax.
C++ Introduction – Day 1

1) #include <iostream>
2) using namespace std;
3) int main() {
4) char NameOfPerson[50];
5) cout << "What's your Name : ";
6) cin >> NameOfPerson; // Taking input
7) cout << "Hello!! " << NameOfPerson;
8) return 0;
9) }
C++ Introduction – Day 1
1) Read line no. 4 in the code above, this line defines our variable.


char NameOfPerson[50];

Data Type Name of Variable Array of Size 50

2) In our example the name of variable is “NameOfPerson”


3) Data Type of variable is “char”
4) Its array data type (Defined by SQUARE Bracket) which can hold 50 elements in array
C++ Introduction – Day 1


Click the Run Button. Program will halt and cursor will blink after line

“What’s your name : “


Waiting for input from user. Type your name or anything and press enter key.

Program will type “Hello !! and the text you entered” On next line.

Each time you run the program and provide input by typing the value of input is stored in
storage area named “NameOfPerson”.

How to find out that the input we typed is stored in variable named “NameOfPerson”. Look at
line no. 7 in our program

1) cout << "Hello!! " << NameOfPerson;


C++ Introduction – Day 1


We are calling variable “NameOfPerson” in this line.

What ever value stored in this variable will be printed on the screen.

If you rerun the program and type any other text now this value will be
stored in variable “NameOfPerson” and this new value assigned to
the variable “NameOfPerson” will be printed.

The “NameOfPerson” can accept any value while program is running
and gives back this stored value whenever this variable is used in
program at later point of time.

We use variables to store value which can be changed or accessed
at later point of time.
C++ Introduction – Day 1


Variable Naming Rules

A variable name can only have

alphabets(A-Z or a-z)

Numbers (0-9)

The underscore (_)

Following are valid variables names

first_name , lastName, student1 , address1 etc.

A variable name cannot begin with a number.

e.g. 1VariableName is not acceptable variable name

Variable names should not begin with an uppercase character.

e.g. FirstName , Last_Name etc are not acceptable variable names

A variable name cannot be a keyword. e.g. int is a keyword that is used to
denote integers.

A variable name can start with an underscore. However, it's not considered a good
practice.
C++ Introduction – Day 1


Program Explanation
#include <iostream> // This line ask compiler to include iostream library
using namespace std; // This is short cut while writing program. If this was not declared here we would have to
write std : cout

int main()
{ // Every CPP program should have function named main and its return type int
char NameOfPerson[3]; //Variable Definition DataType char,
cout << "What's your Name : "; // Outputs string * What's your Name : * on Screen. cout function is defined in
iostream library.
cin >> NameOfPerson; // Taking input from User
cout << "Hello!! " << NameOfPerson; // Outputs string Hello!! and value of variable NameOfPerson
return 0; //
}
C++ Introduction – Day 1

“//” in above program is used to write comments or explanation of program.
Compiler ignores everything after “//”. This is called commenting in program.

To add comment anywhere in program just type “//” and then type what ever
you want to write after that. Compiler will ignore everything after that until
next line.

To Comment out line in program from slide 8 we simply add “//” at the
beginning of that line e.g. to comment out line

#include <iostream> --> //#include <iostream>

Exercise one

Copy program from previous slide to online IDE

Comment one line at a time and run program

Each time note down output and write your understanding in word file and send
back to me.

You might also like