1. Identify global and local variables in the following program. Does a global v
ID: 3553882 • Letter: 1
Question
1. Identify global and local variables in the following program.
Does a global variable have a default value? Does a local variable have a default value?
what will be the output of the code?
#include <iostream>
using namespcae std;
int j;
int main ()
{
int i;
cout << " i is " << i << endl;
cout << " j is " << j << endl;
}
2. Identify and correct the errors in the following program:
void p(int i)
{
int i = 5;
cout << " i is " << i << endl;
}
Explanation / Answer
1. Yes, global variables have a default value equal to 0... If you run the givan code, output will be
i is (some random number)
j is 0
2.
void p(int i)
{
i = 5; // i is already defined as a parameter. remove this declaration to correct the error.
cout << " i is " << i << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.