
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to create Variables and Constants in C++?
To define a variable in C++, you need to use the following syntax −
Syntax
datatype variable_name;
You need to know what type of data is your variable is going to hold and what it will be called. The variable name has constraints on what you can name it. Following are the rules for naming variables −
- Variable names in C++ can range from 1 to 255 characters.
- All variable names must begin with a letter of the alphabet or an underscore(_).
- After the first initial letter, variable names can also contain letters and numbers.
- Variable names are case sensitive.
- No spaces or special characters are allowed.
- You cannot use a C++ keyword (a reserved word) as a variable name.
- Here are some examples of acceptable variable names −
mohd Piyush abc move_name a_123 myname50 _temp j a23b9 retVal
Examples of defining variables −
int my_var; float my_float; double num;
You can make any variable a constant by prepending the const keyword before its definition. For example, if we want to make the above variables const, we'll declare them as −
const int my_var; const float my_float; const double num;
- Related Articles
- Difference between constants and final variables in Java?
- What are constants in Kotlin and how to create them?
- What is the difference between constants and variables?
- How to create constants in JavaScript? Explain with examples.
- Which is faster? Constants, Variables or Variable Arrays in PHP?
- Mention the variables and constants from the following equation:$2x-5=7$.
- How to create scratch variables in JShell in Java 9?
- What is data storage? the terms data objects, variables, and constants concerning data storage?
- How to create JavaScript regexes using string variables?
- How to define constants in C++?
- How to implement constants in java?
- How to create many Javascript variables in a single statement?
- Difference between C++ string constants and character constants
- How to define integer constants in JavaScript?
- How to define character constants in C#?

Advertisements