A variable is defined by declaring it in the program. Thus, for example, in func
ID: 3764910 • Letter: A
Question
A variable is defined by declaring it in the program. Thus, for example, in function f(int i), i is declared as a variable. When a variable is declared storage is allocated for the variable. Same name can be used to define different variables in the same program. Different storage is allocated for each variable. Because of using the same name for different variables in the program, it requires rules for conflict resolution in using the variables. This has been discussed in the class. Following is a part of a program declaring three variables (line 1, line 2 and line 8) with the same name i.
(a) Give the scopes of these three variables. Scope can be defined as within the entire file, within the entire function, within the entire block or part of a block. Scope of declaration of line 1: Entire file. Scope of declaration of line 2: Entire function f Scope of declaration of line 8: Staring from the declaration up to the end of that block.
(b) Give the print-out of the printf() functions.
(c) Values of the variables inside a function are not retained when the function returns. What is the mechanism for retaining the value in a variable even after the function returns. Define the variables as static.
Thank you!!!
#include«stdio.h> 1 3 int i; 5 void f(int i) printf("11- %d ", i); 9-2; if (i>1) 10 12 14 i); printf (" i2- int i; printf ("13- %d ", %d ", i); printf("14= i-4; %d ", i); 16 17 18 19 20 21 int h(void) %d ", i); 23 printf("15- 24 25 26 int main) 27 f(6) 28 0Explanation / Answer
line 2: Here 'i' is global variable ie scope entire file.
line5, 9: Here 'i' is local varriable to function means scope entire to function.
line 12: Here 'i' is local varriable to block means scope lies entire to 'if' block only.
Outputs :
i1= 6 from line 5
i2= 2 from line 9
i3=any random no like 16384 because you did not initialize i. from line 12
i4=2 from line 9
i5=1 from line 2
Answer 3. to retain the value of variable after function return for multiples calls of function you can declare variable as static like example given below
int h(void)
{
static int i=0;
i++;
printf("i5= %d ",i);
}
when you call h() 3 times in a row you will get output as
1
2
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.