search
Log In
28 votes
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}$
in Programming
edited by
3.4k views
1

Link to program execution: https://ideone.com/t5t6e

5 Answers

27 votes
 
Best answer

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$

Answer is D.


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.

7 votes

Ans D

3 votes

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

0 votes

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.!!
0 votes
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.
Answer:

Related questions

27 votes
3 answers
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}$
asked Sep 29, 2014 in Programming gatecse 4.3k views
23 votes
5 answers
2
3.4k views
What will be the output of the following C program segment? char inChar = 'A'; switch ( inChar ) { case 'A' : printf ("Choice A \n"); case 'B' : case 'C' : printf ("Choice B"); case 'D' : case 'E' : default : printf ("No Choice"); } No Choice Choice A Choice A Choice B No Choice Program gives no output as it is erroneous
asked Aug 5, 2014 in Programming gatecse 3.4k views
62 votes
9 answers
3
8.4k views
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}$
asked Nov 1, 2014 in Programming Ishrat Jahan 8.4k views
19 votes
4 answers
4
2.5k views
What is the output printed by the following C code? # include <stdio.h> int main () { char a [6] = "world"; int i, j; for (i = 0, j = 5; i < j; a [i++] = a [j--]); printf ("%s\n", a); } dlrow Null string dlrld worow
asked Oct 29, 2014 in Programming Ishrat Jahan 2.5k views
...