2.4k views

Consider the following C code segment.

int a, b, c = 0;
void prtFun(void);
main()
{
static int a = 1;       /* Line 1 */
prtFun();
a += 1;
prtFun();
printf(“ \n %d %d ”, a, b);
}

void prtFun(void)
{
static int a = 2;       /* Line 2 */
int b = 1;
a += ++b;
printf(“ \n %d %d ”, a, b);
}


What output will be generated by the given code segment if:

Line 1 is replaced by auto int $a = 1$;

Line 2 is replaced by register int $a = 2$;

1. $\begin{array}{ll} \text{3} & \text{1} \\ \text{4} & \text{1} \\ \text{4} & \text{2} \\ \end{array}$
2. $\begin{array}{ll} \text{4} & \text{2} \\ \text{6} & \text{1} \\ \text{6} & \text{1} \\ \end{array}$
3. $\begin{array}{ll} \text{4} & \text{2} \\ \text{6} & \text{2} \\ \text{2} & \text{0} \\ \end{array}$
4. $\begin{array}{ll} \text{4} & \text{2} \\ \text{4} & \text{2} \\ \text{2} & \text{0} \\ \end{array}$

edited | 2.4k views
+1

main
$a=1$
$prtFun()$
$a=2$
$b=1$
$a= a + \text{++}b = 2+2 = 4$
$b = 2$
printf $\rightarrow 4 \ 2$
back to main
$a = a+1 \rightarrow 1+1 \rightarrow 2$
$prtFun()$
$a=1$ //previous a is lost
$b=1$
$a= a + \text{++}b = 2+2 = 4$
$b = 2$
printf $\rightarrow 4 \ 2$
back to main
$a = 2 \\ b = 0$ (initial value of global b. in $prtFun$ local b is only updated)
printf $\rightarrow 2 \ 0$

by Boss (11.1k points)
edited
0
what difference does the register keyword make ?
+24
It makes no such "programming" difference. The variable is treated as a local variable only.

It is handled by CPU registers for faster computation
+14
Yes almost similar except the fact that register variable uses register for storage instead of memory

But both behave as a local variable

In auto variable memory is allocated at run time
0
@ Sankaranarayanan P.N Sir, Why the value of variable 'a' in main() is not lost when call to prtfun() is made.. how it is able to retain its old value '2' even after control goes to another function even though it is not static variable. Please clarify.
+1
The variable a declared inside main is static. Also The variable a declared inside the function is also static. These two are separate variables. Static variables will retain the values
0

Sankaranarayanan P.N But in 49th question, the static variable in main function is replaced by static variable

0

Sankaranarayanan P.N But in 49th question, the static variable in main function is replaced by auto variable

0
Also we cannot dereference register variables since it has not any memory address.Default initial value of register variable is garbage.
+19

Refer: 0
What is the difference between scope & Life of a variable
0

@Anu007
Thanks a lot. This is the main reference for solving this question.

Ans D by Boss (38.5k points)

I tried the same and I am getting C
Here is my code:

#include<stdio.h>

int a, b, c = 0;
void prtFun(void);
main() {
static int a = 1;       /* Line 1 */
prtFun();
a += 1;
prtFun();
printf("\n%d %d", a, b);
}

void prtFun(void)
{
static int a = 2;       /* Line 2 */
int b = 1;
a += ++b;
printf("\n%d %d", a, b);
}

by (41 points)
0
best link .. thank you :)
0

Static keyword which declares locally to a function,doesnot update global variable. Only the variable in main will update the value of global variable . Even if we simply run this code

https://ideone.com/aXzwNc

the output clearly shows static keyword not updated global variable value