VARIABLES IN C++ TRB CSI › Programming with C++ › variables

VARIABLES IN C++

Variable என்பது program execution போது data/value-ஐ store செய்ய பயன்படுத்தப்படும் named memory location. Simple definition: A variable is a named memory location used to store a value that can change during program execution.

Premium — locked Advanced 29 min 207 cards 69 programs 11 MCQs v2
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

VARIABLES IN C++…

Premium This card is part of the premium lesson. Unlock
Definition

Variable என்பது program execution போது data/value-ஐ store செய்ய பயன்படுத்தப்படும் named m……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int age = 25; இதில்: int → Data type age → Variable n……

Premium This card is part of the premium lesson. Unlock
Real Life Example

ஒரு box-க்கு Marks என்று name வைக்கிறோம். அதற்குள்: 90 store செய்கிறோம். Later: 95 என்று change……

Premium This card is part of the premium lesson. Unlock
Explanation
Why variable is needed

Program-ல் values temporary-ஆக store செய்ய வ……

Premium This card is part of the premium lesson. Unlock
Output
Result

இதற்காக variables use செய்கிற……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int age = 20; float salary = ……

Premium This card is part of the premium lesson. Unlock
Flow

Variable declaration ↓ Memory allocated ↓……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int x = 10; Conceptually: Variable Name : x Data Type : int Value : 10 Memory : allocated……

Premium This card is part of the premium lesson. Unlock
Explanation
Declaration

Variable-ன் name மற்றும் data type compiler-க்கு introduce ……

Premium This card is part of the premium lesson. Unlock
Explanation
Initialization

Variable create செய்யும் நேரத்திலேயே initial value க……

Premium This card is part of the premium lesson. Unlock
Explanation
Assignment

Already existing variable-க்க……

Premium This card is part of the premium lesson. Unlock
Explanation

இதில்: int marks; Declaration.…

Premium This card is part of the premium lesson. Unlock
Practice
Assignment.

DECLARATION vs INITIALIZATION vs ASSIGNMENT இந்த difference மிகவும் imp……

Premium This card is part of the premium lesson. Unlock
Practice
Assignment

x = 20; Existing variable-க்க……

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Explanation

int marks = 80; Breakdown: int → Data type marks → Vari……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Program
Initially
Program
Later

இதனால் variable name same. ஆன……

Premium This card is part of the premium lesson. Unlock
Explanation
Program

int x = 10;…

Premium This card is part of the premium lesson. Unlock
Program
Step 1
Program
Step 2
Explanation

Old value 10 replaced by 20. ……

Premium This card is part of the premium lesson. Unlock
Program
Current
Program
So
Explanation

Final value: 25 VERY IMPORTAN……

Premium This card is part of the premium lesson. Unlock
Explanation

strange equation போல இருக்கும். Programm……

Premium This card is part of the premium lesson. Unlock
Explanation

Process: Right side first: x ……

Premium This card is part of the premium lesson. Unlock
Program
Final
Explanation
Variable naming rules

Variable name என்பது identifier. அதனால் identifier rules variables-க்கும் apply ஆகும். RULE 1: LETTER OR UNDE……

Premium This card is part of the premium lesson. Unlock
Explanation
Valid variable names

age marks student1 totalMarks……

Premium This card is part of the premium lesson. Unlock
Explanation
Invalid variable names

1age student mark total-mark ……

Premium This card is part of the premium lesson. Unlock
Explanation
Multiple variable declaration

Same data type variables ஒரே statement-ல் d……

Premium This card is part of the premium lesson. Unlock
Explanation
Multiple initialization

int a = 10, b = 20, c = 30;…

Premium This card is part of the premium lesson. Unlock
Definition
Meaning

a = 10 b = 20 c = 30…

Premium This card is part of the premium lesson. Unlock
Important
Important common confusion

Consider: int a, b = 10; இதன் meaning: a → declared, ……

Premium This card is part of the premium lesson. Unlock
Explanation
Variable with different data types

Integer variable int age = 25; Floating variable float weight = 65.5f; Double variable ……

Premium This card is part of the premium lesson. Unlock
Program
Program - different variables
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Input
Input into variable
Program
Example
Explanation

return 0; } If user enters: 25…

Premium This card is part of the premium lesson. Unlock
Definition
Meaning

User enter செய்யும் value-ஐ age variable-ல் s……

Premium This card is part of the premium lesson. Unlock
Explanation
Variable in calculation

Variables calculations-ல் use செய்……

Premium This card is part of the premium lesson. Unlock
Dry Run

a = 10 b = 20 sum = a + b = 1……

Premium This card is part of the premium lesson. Unlock
Program
Program - addition
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int x = 100; Conceptually mem……

Premium This card is part of the premium lesson. Unlock

x 100 Suppose later:…

Premium This card is part of the premium lesson. Unlock

Then: Variable Value x 200 Sa……

Premium This card is part of the premium lesson. Unlock
Explanation
Memory address

Every object/variable has a m……

Premium This card is part of the premium lesson. Unlock
Explanation

&x gives address of x.…

Premium This card is part of the premium lesson. Unlock
Output
Possible output

Exact address changes between……

Premium This card is part of the premium lesson. Unlock

prints: 10 But:…

Premium This card is part of the premium lesson. Unlock

Simple: x → Value &x → Address of x……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int marks = 90; marks is an identifier because it is a name. The named object represented by ……

Premium This card is part of the premium lesson. Unlock
Explanation

Valid. Constant const int x =……

Premium This card is part of the premium lesson. Unlock
Explanation

Invalid.…

Premium This card is part of the premium lesson. Unlock
Comparison

[VARIABLE vs LITERAL]…

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int age = 25; age → Variable 25 → Literal Var……

Premium This card is part of the premium lesson. Unlock
Explanation
Local variable

Function அல்லது block-க்குள் ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

x is local variable. Its scop……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Local variable example

#include using namespace std;……

Premium This card is part of the premium lesson. Unlock
Explanation
Global variable

Functions-க்கு வெளியே declare……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

int x = 100;…

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; } x is global variable. It can be accessibl……

Premium This card is part of the premium lesson. Unlock
Explanation
Scope

Scope என்பது variable name progr……

Premium This card is part of the premium lesson. Unlock
Program
Example
Program
Outside
Explanation

Invalid. Because x block scop……

Premium This card is part of the premium lesson. Unlock
Explanation
Block scope

{ int value = 100; cout…

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

int x = 100;…

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock

Local x global x-ஐ shadow செய்……

Premium This card is part of the premium lesson. Unlock

Here: x → Local variable ::x ……

Premium This card is part of the premium lesson. Unlock
Explanation
Static local variable

Normally local variable function call முடிந்ததும் lifeti……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

count++;…

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Program
Normal

Every call: 1 Static: static in……

Premium This card is part of the premium lesson. Unlock
Explanation
Auto variable

Historically, ordinary local ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

x is an automatic local variable. Modern C++-ல் auto keyword mostly type deduction-க்கு பயன்படுத்தப்படுகிறத……

Premium This card is part of the premium lesson. Unlock
Important

auto means type is automatica……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int x; Local automatic variable x has a……

Premium This card is part of the premium lesson. Unlock
Explanation

This can lead to undefined behavior f……

Premium This card is part of the premium lesson. Unlock
Practice
Initialization is good practice

Instead of: int total; Prefer when appropriate: int total = 0……

Premium This card is part of the premium lesson. Unlock
Explanation
Different initialization styles

C++ supports multiple forms. Copy initialization int x = 10; Direct initialization int x(10); List initializa……

Premium This card is part of the premium lesson. Unlock
Explanation
Reference variable

A reference acts as an alias fo……

Premium This card is part of the premium lesson. Unlock
Program
also changes
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock

Because ref refers to x.…

Premium This card is part of the premium lesson. Unlock
Explanation
Pointer variable

Pointer variable stores another object's address. int x ……

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock

*ptr accesses the value store……

Premium This card is part of the premium lesson. Unlock
Explanation
Variable lifetime

Scope and lifetime are related but different. S……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

x normally exists during exec……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 1

Using keyword as variable: int ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 2

Number at beginning: int 10ma……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 3

Using variable before initial……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 4

Wrong data type: int percentage = 89.……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 5

Confusing assignment and comp……

Premium This card is part of the premium lesson. Unlock
Practice
Assignment

x = 10;…

Premium This card is part of the premium lesson. Unlock
Comparison

x == 10…

Premium This card is part of the premium lesson. Unlock
Remember

= → Assign == → Compare…

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 6

Expecting both variables to initialize: int a, b = ……

Premium This card is part of the premium lesson. Unlock
Comparison

Variable vs Identifier vs Val……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int marks = 90;…

Premium This card is part of the premium lesson. Unlock
Important
A variable generally has

1. Name 2. Data type 3. Value……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int age = 25; Name → age Type → int Value → 25……

Premium This card is part of the premium lesson. Unlock
Remember

V = N T V M N → Name T → Type V → Value ……

Premium This card is part of the premium lesson. Unlock
TRB Point

What is a variable? A variable is a named memory ……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point - important

Consider: int x = 10; Identify: int ……

Premium This card is part of the premium lesson. Unlock
MCQ

Which is a variable declarati……

Premium This card is part of the premium lesson. Unlock
Interview Point

Difference between declaration and initialization? Declaration: int ……

Premium This card is part of the premium lesson. Unlock
Interview Point

Difference between variable and constant? int……

Premium This card is part of the premium lesson. Unlock
Interview Point

Is an uninitialized local int……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int x; A local automatic fundamental variable does not automatically become zero.……

Premium This card is part of the premium lesson. Unlock
MCQ

A variable is:…

Premium This card is part of the premium lesson. Unlock
MCQ

Which is a valid declaration?…

Premium This card is part of the premium lesson. Unlock
MCQ

Which statement initializes x?…

Premium This card is part of the premium lesson. Unlock
MCQ

Which operator assigns a valu……

Premium This card is part of the premium lesson. Unlock
MCQ

Which variable name is invali……

Premium This card is part of the premium lesson. Unlock
MCQ

C++ variable names are:…

Premium This card is part of the premium lesson. Unlock
MCQ

What is the final value? int ……

Premium This card is part of the premium lesson. Unlock
MCQ

In: int a, b = 10; which vari……

Premium This card is part of the premium lesson. Unlock
MCQ

Which is a local variable? in……

Premium This card is part of the premium lesson. Unlock
MCQ

Which statement changes x fro……

Premium This card is part of the premium lesson. Unlock
Practice

Identify declaration, initialization, assignment: int x; x = 10; int y = 20; x ……

Premium This card is part of the premium lesson. Unlock
Dry Run
Practice - dry run
Summary

Variable A variable is a named memory locat……

Premium This card is part of the premium lesson. Unlock
Explanation
Syntax

data_type variable_name; int a……

Premium This card is part of the premium lesson. Unlock
Explanation

Main concepts: Declaration → int x; Initialization → int x = 10; Assignment → x = 20; Important d……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program