C++ Program to Initialize Variable

This program shows the difference between initializing and not initializing variables. It is wise to initialize a variable before they are declared.

#include<iostream>
using namespace std;
int main(){
	int a;
	int b = 10;
	cout<<"a = "<<a<<" b = "<<b<<endl;
	return 0;
}
Output of above program uninitialized variable
Output of above program uninitialized variable

You can get different trash value instead of 16, that I got here.

#include<iostream>
using namespace std;
int main(){
	int a = 30;
	int b = 10;
	cout<<"a = "<<a<<" b = "<<b<<endl;
	return 0;
}
 Output of above program initialized variable
Output of above program initialized variable

I got the output 30, as per the above program as variable was initialized.

It is better to initialize the variable before using.

One Reply to “C++ Program to Initialize Variable”

Leave a Reply

Your email address will not be published. Required fields are marked *