C++ Variable Declaration
Overview
In this tutorial, we will learn C++ Variable declaration. We need to declare variables in the C++ program before using them in the program.
A variable declaration specifies the data type and the name of the variable used in the program. C++ is a statically typed language. The compiler should know the type and how much space would be needed for the variable at compile time.
Declare single variable
We can specify the type of the variable and the variable name. For example, to declare a variable sno as an integer:
int sno;
We can also initialize the variables during the declaration. Initializing a variable is assigning an initial value to the variable. For example, to declare and initialize the variable sno to 1
int sno = 1;
Declare multiple variables
We can also declare multiple variables of the same type with comma-separated names. For example, to declare price, salesTax, and totalPrice as double:
double price, salesTax, totalPrice;
Code Snippet
Let’s declare variables i,j, and k. Assign the variables with values 10,20,30 respectively.
/************************************ * C++ Variables * Filename: variables.cpp * Declare variables in C++ Program * C++ Tutorials - www.TestingDocs.com **************************************/ #include using namespace std; int main() { int i=10,j=20,k=30; //output variables cout << "i = " << i << endl; cout << "j = " << j << endl; cout << "k = " << k << endl; return 0; } // end main
Output
i = 10
j = 20
k = 30
—
C++ Tutorials
C++ Tutorials on this website:
https://www.testingdocs.com/c-coding-tutorials/
For more information on the current ISO C++ standard
https://isocpp.org/std/the-standard