GATE2012-49

3.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
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=2$ //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$

edited by
0
what difference does the register keyword make ?
26
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.
23

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.

1
Small correction I think In second prtfun() record a=2 not 1 @sankarnarayanan.p.n

And thanks anu007 for detail reference.
0

@MRINMOY_HALDER, Scope - tells where this identifier can be available to access and Life Time -  says how long this identifier will live in the program irrespective of availability for accessing or not.

Ans D

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);
}

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

Static is special but Register and Auto has nothing special in terms of Scope. So a simple execution follows.

0
There is something special about register variables  too, they speed up program execution but wont affect output generated in anyway.!!
Line 1 replaced by auto int a=1;
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.

Related questions

1
4.3k 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 ... $\begin{array}{lll} 3 & & 1 & \\ 5 & & 2 & \\ 5 & & 2 & \end{array}$
Which one of the choices given below would be printed when the following program is executed ? #include <stdio.h> struct test { int i; char *c; }st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"}; main () { struct test *p = st; p += 1; ++p -> c; ... ); } $\text{jungle, n, 8, nclastor}$ $\text{etter, u, 6, ungle}$ $\text{cetter, k, 6, jungle}$ $\text{etter, u, 8, ncestor}$