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.
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;
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
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
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…