Initialization of variables
When declaring a regular local variable, its value is by default
undetermined. But you may want a variable to store a concrete value at the same
moment that it is declared. In order to do that, you can initialize the
variable. There are two ways to do this in C++:
The
first one, known as c-like, is done by appending an equal sign followed by the
value to which the variable will be initialized: type identifier = initial_value ;
For example, if we want to declare an int variable called a initialized with a value of 0 at the moment in which it is declared, we could write:
int a = 0; |
type identifier (initial_value) ;
For example:
int a (0); |
// initialization of variables
#include <iostream>
using namespace std;
int main () {
int a=5; // initial value = 5 int b(2); // initial value = 2 int result; // initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
|
6
|
No comments:
Post a Comment