0

Is it possible to declare a variable without assigning value in C++?

int a; int a=15; Which one will be Correct?

If i assign value to a variable 3 times or more which one will count at the end in C++?? int a=15; int a=10; int a=5; Which value will be execute for a at the end?

2 Answers2

3
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
Aplet123
  • 33,825
  • 1
  • 29
  • 55
2

int x; declares a variable x without assigning a value.

If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.

john
  • 85,011
  • 4
  • 57
  • 81