|
|
Tutorials

Cpp

|
Topic |
What is a variable in C++?.
|
|
Explanation |
|
A variable defines a location in the memory that holds a value which can be modified later.
Syntax:
type variable_list;
In the above syntax "type" refers to any one of the C++ data types.
Declaring Variables:
Variables in C++ should be declared before they are used in the code block. It is better to
declare it right at the place where its used as this makes the programming much easier. This kind of declaration
reduces the errors in the program.
Example:
#include <iostream.h>
void main()
{
int n = 10;
cout << "Integer value is:"<< n << '\n';
}
|
Result:
Integer value is: 10
In the above example "n" is an integer variable declared using return type "int".
Variables can also be intialized dynamically at the place of declaration using the expression as below.
Example:
float area = 3.14159 * rad * rad
In the above example the variable "area" is declared dynamically when it is used.
Naming variables in C++:
- Always the first character of a variable must be a letter or an underscore.
- The variable name cannot start with a digit.
- Other characters should be either letters, digits, underscores.
- There is no limit for the length of variables.
- Declared keywords cannot be used as a variable name.
- Upper and lower case letters are distinct.
|
| Note |
C++ is one of the most used programming languages in the world. Also known as "C with Classes".
Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.
|
|
|
|