In C++, variables are used to store data that can be manipulated by the program. They are named storage locations in memory.
Variables can be of different types such as int, float, char, etc., each serving different purposes and constraints.
Declaring a variable involves specifying its type followed by its name, e.g., int age;
.
Initialization is when you assign a value to a variable at the time of declaration, e.g., int age = 25;
.
#include <iostream>
using namespace std;
int main() {
int age = 25;
cout << "Age: " << age;
return 0;
}
Console Output:
Age: 25
Local variables are declared inside a function or block and can only be accessed within that function or block.
Global variables are declared outside of all functions and can be accessed from any function within the program.
#include <iostream>
using namespace std;
int globalVar = 100; // Global variable
int main() {
int localVar = 50; // Local variable
cout << "Global: " << globalVar << ", Local: " << localVar;
return 0;
}
Console Output:
Global: 100, Local: 50
Constants are variables whose value cannot be changed once defined. They are declared using the const
keyword.
#include <iostream>
using namespace std;
int main() {
const float PI = 3.14159;
cout << "Value of PI: " << PI;
return 0;
}
Console Output:
Value of PI: 3.14159
C++ supports various primitive data types like int, char, float, and double, each with different memory sizes and ranges.
#include <iostream>
using namespace std;
int main() {
int integerVar = 10;
char charVar = 'A';
float floatVar = 3.14;
double doubleVar = 3.14159265359;
cout << "Int: " << integerVar << ", Char: " << charVar;
return 0;
}
Console Output:
Int: 10, Char: A
Also known as automatic conversion, it occurs when data types are automatically converted by the compiler.
Also known as type casting, it is the conversion of one data type into another explicitly by the programmer.
#include <iostream>
using namespace std;
int main() {
int i = 10;
double d = i; // Implicit conversion
double pi = 3.14;
int intPi = (int)pi; // Explicit conversion
cout << "Double: " << d << ", Int from double: " << intPi;
return 0;
}
Console Output:
Double: 10, Int from double: 3
Variable names must begin with a letter or an underscore, followed by letters, numbers, or underscores.
Use meaningful names that convey the purpose of the variable. Avoid single-character names except for loop counters.
#include <iostream>
using namespace std;
int main() {
int userAge = 30;
float accountBalance = 1000.50;
cout << "User Age: " << userAge << ", Account Balance: " << accountBalance;
return 0;
}
Console Output:
User Age: 30, Account Balance: 1000.5
Static variables are initialized only once and retain their value between function calls.
Automatic variables are created and destroyed each time a function is called.
#include <iostream>
using namespace std;
void demoFunction() {
static int staticVar = 0;
int autoVar = 0;
staticVar++;
autoVar++;
cout << "Static: " << staticVar << ", Auto: " << autoVar << endl;
}
int main() {
demoFunction();
demoFunction();
return 0;
}
Console Output:
Static: 1, Auto: 1 Static: 2, Auto: 1
The default storage class for local variables.
Used to define variables that should be stored in a register instead of RAM for faster access.
Allows a variable to be accessible across multiple files.
#include <iostream>
using namespace std;
extern int globalVar;
int main() {
register int regVar = 10;
cout << "Register Variable: " << regVar;
return 0;
}
Console Output:
Register Variable: 10
Dynamic variables are allocated memory during runtime using operators like new and delete.
#include <iostream>
using namespace std;
int main() {
int* ptr = new int;
*ptr = 20;
cout << "Dynamic Variable: " << *ptr;
delete ptr;
return 0;
}
Console Output:
Dynamic Variable: 20
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies