The Gateway to Computer Science Excellence
+22 votes
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}$
in Programming by Veteran (105k points)
edited by | 2.4k views
+1

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

4 Answers

+23 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=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$

Answer is D.

by Boss (11.1k points)
edited by
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.

+7 votes

Ans D

by Boss (38.5k points)
+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);
        }
 

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

0 votes

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

by Active (1k points)
0
There is something special about register variables  too, they speed up program execution but wont affect output generated in anyway.!!

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,616 answers
195,897 comments
102,361 users