Cpp programming variables and types
- Home
- Data types
- Cpp programming variables and types
- On
- By
- 0 Comment
- Categories: Data types
Cpp programming variables and types
Cpp programming variables and types
In this tutorial, we will discuss Cpp programming variables and types
Variable is a very fundamental concept of all programming language for programmers. purpose of the variable in programming which is used to allocate memory(space) in memory location before we start to write programming.
Type of variables in C++
Variable declaration
always, when we start programming, we should be defined essential variables. Variable definition means that the programmer allocate to store value needed space or memory location for programming purpose.
Common variable declaration
Syntax
data_Type variable name;
Or
data_Type variable name1, variable_Name2,variable_name3 ............ variable_name n;
A variable declaration always ending with semicolon most programming languages
Here, data type means C++ language has many data type such as int, float, double, char etc..
The variable name is unique to help to identify memory locations
variable declaration
Declare a integer variable
Integer type
int age; int age,marks,total;
Declare a charactor variable
Character type
char letter; char letter, alphabet;
Declare a float variable
float
float average; int average, area;
Declare a double variable
double
double balance; double balance, prize;
Variable initialization in C++
In the above examples, we declared variables only, but none of them has been assigned or initialized any suitable values.
in this part, we are providing an initial value to variables or assign the value to variables according to their declaration. it is known as variable initialization.
Syntax
method 1
data_type variable_name=value; //declaration and initilization in single line
method 2
data_type variable_name; //variable declaration variable_name=value;//variable initilazation
Examples
variable initialization
Declare and initialize a integer variable
Integer type
int age=29;//declaration and initilization of age variable int marks;//declaration of marks variable marks=67;//initilization of variables
Declare and initialize a charactor variable
Character type
char letter='A';//declaration and initilization of letter variable char alphabet;//declaration of alphabet variable alphabet='c';//initilization of variables
Declare and initialize a floating point variable
float
float average=78.6;//declaration and initilization of average variable int area;//declaration of area variable area=345.6//initilization of variables
Declare and initialize a floating point large variable
double
double balance=23456.76;//declaration and initilization of balance variable double prize;//declaration of prize variable prize=1234.54;//initilization of variables
Rules of Declaring variables in C++
- A variable name can contain Alphabet letter(Upper case A to Z, Lower case a to z), digit 0 – 9 and the underscore character.
- The first letter must be a letter or underscore.
- Space can not be used variable name
- Special charactor(lsimilar#, $) can not used as variable name.
- Keyword in C++ language cannot be used as the variable name
- Variable names always case sensitive
Example program for variable
#include <iostream> #include <conio.h> using namespace std; int main() { int age; //declare a integer variable age=34; //initialize a integer variable char ch;//declare a character variable ch='A'; //initialize a character variable float avg; //declare a floating point variable avg=78.98;//initialize a floating point variable cout << "age is: "<<age << endl; //display the age cout << "letter is: "<<ch << endl; // display the letter cout << "average is: "<<avg << endl; //display the average return 0; }
age is: 34 letter is: A average is: 78.98
Suggested for you