For clarity in the declaration and use of variables, you can completely use C++ language. If you declare a variable of type string, you cannot assign it in numeric. So you need to know the declaration syntax to use it correctly.
To be able to “Variable declaration and initialization” in C++, we must first declare it, then specify what data type it is. We just need to write the type name (like int, short, float…) followed by a valid variable name.
Syntax
char c; //character variable declaration. int area; //integer variable declaration. float num; //float variable declaration.
The first line declares a variable of type Char with the name c. The second line declares a variable of type int with the name area and the same with “float, num”. Once declared, the above variables can be used within their scope in the program.
If you want to declare several variables of the same type and want to save on writing effort, you can declare them on one line, separating the names with commas.
Integer types (char, short, long and int) can be signed or unsigned depending on the range of values we need to represent.
Declaring More than one Variable
If you want to declare several variables of the same type and want to save on writing effort, you can declare them on one line, separating the names with commas.
int a, b, c; //more than one variable declaration.
completely equivalent to
int a; //integer variable declaration. int b; //integer variable declaration. int c; //integer variable declaration.
Initialization of variables
When declaring a variable, its value is implicitly undefined. But you’ll probably want it to have a definite value when it’s declared. To do that, you just need to write the equal sign and the value you want the variable to carry.
here is syntax:
type identifier = initial_value;
for example:
int a = 10; //integer variable declaration & initialization.
practice:
//Write a CPP program for declaration & initialization of variable #include <iostream.h> int main () { int sum; //Variable declaration int a = 10; //Variable declaration & initialization int b = 5; //Variable declaration & initialization ans = a + b; cout << "Addition is:" << ans << endl; return 0; }
output:
Addition is: 15
Note when declaring variables
A variable declaration provides a guarantee to the compiler and is meaningful only at compile time, the compiler needs to declare the actual variable at link time of the program.
Variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at program link time.
You would use the extern keyword to declare a variable anywhere.
Although you can declare a variable multiple times in your C++ program, it can only be defined once in a file, a function, or a block of code
Conclusion
Above are the basic knowledge needed when variable declaration and initialization, hope our article is useful for you.
Related articles
- Top Ways To Create A User-Friendly Online Property Search For Your Real Estate Clients
If you’re running a real estate business, you’re well aware that pretty much most of the paperwork has become automated and it’s time for you really get an education on what you need to use in terms of technology. This is important to make your online presence and services stand out from the competition. Like […]
- List Education Websites for Students, providing a variety of materials and completely free
Everyone would like to get the highest quality of education in order to fulfill their goals. But the more an institution is of high quality and reputable, the more fees they charge. Students typically leave their education in a state of nil and work blue collar jobs to achieve their primary needs. Additionally, they collect […]
- Simple solution to correct the requests.exceptions.ConnectionError: (‘Connection aborted.’ RemoteDisconnected(‘Remote end closed connection without response’)) issue
Python is a popular programming language that can be used widely in a lot of applications. Python is also a good choice as a programming language depending on user background and perspective. Because it is used widely and popular, if you find any errors when using Python. It is a common problem, you face the […]
- “[Errno 61] Connection refused” is occurring even, the program is connecting with the port well and the socket is running in the interfaces.
If you see the “[Errno 61] Connection refused” issue although you checked the program, port, socket and interfaces. Although your program of Python works well in the server and the client, they are installed at the same device. The local IP from my device is connecting with the clients but this IP is not connected […]
- Description “Return by Reference”.
C++ is considered not only as a language of Object Oriented Programming, but also an intermediate level language. It identifies both high and low level languages. It became easy and widely used in computer programs and that is the reason why we should understand the definition and its function as well. Such as Return by […]