edited by
1 flag 2,303 views
10 10 votes

Consider the following three ANSI-C programs, $\text{P1, P2,}$ and $\text{P3}$.

\[
\renewcommand{\arraystretch}{1.3}
\begin{array}{|l|l|l|}
\hline
\quad\quad\quad  \textbf{P1} & \quad\quad\quad \textbf{P2} & \quad\quad\quad \textbf{P3} \\
\hline
\text{#include <stdio.h>} & \text{#include <stdio.h>} & \text{#include <stdio.h>} \\
\begin{array}{l}
\text{int a=5;} \\
\text{int main()\{} \\
\quad \text{int a=7;} \\
\quad \text{return(0);} 
\end{array}
&
\begin{array}{l}
\text{int main()\{} \\
\quad \text{int a=5;} \\
\quad \text{int a=7;} \\
\quad \text{return(0);} 
\end{array}
&
\begin{array}{l}
\text{int main()\{} \\
\quad \text{int a=5;} \\
\quad \text{float a=7;} \\
\quad \text{return(0);} 
\end{array}
\\
{\text{\}}} &
{\text{\}}} &
{\text{\}}} \\
\hline
\end{array}
\]

Which one of the following statements is true?

 

  1. Only $\text{P1}$ will compile without any error
  2. Only $\text{P2}$ will compile without any error
  3. Only $\text{P3}$ will compile without any error
  4. All three programs $\text{P1, P2,}$ and $\text{P3}$ will compile without any error
  • 🚩 Edit necessary | 👮 akhil1 | 💬 “after the #include there is which is not visible in the question.”

6 Answers

5 5 votes

Only P1 will compile without any error.


In the semantic phase, the C compiler GCC resolves variables by scope hierarchy which initially search in the local scope (defined within a block) and if the variable is not found then only proceeds to global scope. 

As in P1 within the main function scope, a is only defined once locally, compiler will be able to resolve reference of a without proceeding to check global scope where a different declaration of a exists. Whereas, in both option B and C, there are two different definition for a within the local scope, hence the compiler wont be able to resolve which defination should be considered for a, hence compilation will fail in any case where re-declaration happens within the same scope.

 

moved by
2 2 votes

In P1, we can see the variable 'a' of integer datatype as both global variable (int a = 5; before creating the main function) and local variable (int a = 7; inside the main function). So, P1 will compile without any error.

 

In P2, we can see that the variable 'a' of integer datatype is created two times with different values (5 and 7), which makes it invalid. So, P2 will face an error during compilation.

 

In P3, we can see that the variable 'a' is declared two times with different datatypes (int and float), which makes it invlaid. So, P3 will face an error during compilation. 

 

Therefore, the correct option is A) Only P1 will compile without any error.

2 2 votes

P3: conflicting types for 'a'.

here, compiler never checks as a (variable name + data type) combo, it always checks (varaible name and scope) combo so even if we use float it throws compilation error as above.

P2: redefinition error

 

P1: no errors as both the a's are in different scope.

here, local variable a from the main method is shadowing/hideing the globally declared varaible.

edited by
1 1 vote

Option A: Only P1 will compile without any error​

ProgramCompiles?Reason
P1YesLocal variable shadows the global variable.
P2 NoSame variable declared twice in the same scope.
P3  NoSame variable name declared with a different type in the same scope
0 0 votes
Only P1 will complie without any error i.e option A is correct.

1) int a=5 is a global variable and int a=7 is a local variable as it's inside the main() in a different scope. local variable can legally shadow global variable with the same name in C language. So here we don't have any error

2) For P2 both int a=5 and int a=7 are declared in the same scope inside main() . Redeclaring a variable with the same name in the same block is not allowed and this is a compile error. P2 will not compile

3) Same as P2 even if we had changed the type of the variable i.e first int a=5 and then float a=7 these both are still in the same scope. Compiler will see first int a=5 bind the name a to that block. Then the second block float a tries to redefine the same identifier this makes redifination of a which is a compiler error again.

 
Answer:
Position:
Show:

Related questions

15 15 votes
2 2 answers
1.8k
1.8k views
gatecse asked Feb 23
1,773 views
In C runtime environment, which one of the following is stored in heap?A static variable declared inside a functionAn array of integers declared inside a functionA dynami...
10 10 votes
3 3 answers
1.7k
1.7k views
gatecse asked Feb 23
1,715 views
Consider the following ANSI-C program.#include <stdio.h int main(){ int *ptr, a, b, c; a=5; b=11; c=20; ptr=&a; *ptr=c; ptr=&c; a=*(&b); c=*ptr-a; printf("%d",c); return(...
11 11 votes
5 5 answers
3.5k
3.5k views
gatecse asked Feb 23
3,520 views
Consider the following ANSI-C function.int func(int start, int end){ int length=end+1-start; if((length<1)||(start<0)||(end<0)){ return(0); } if(length%3==0){ return(func...
5 5 votes
2 2 answers
2.0k
2.0k views
gatecse asked Feb 23
1,993 views
The figure in Panel $\text{I}$ below is a grid of cells with four rows and four columns. The numbers on the top and on the left represent the number of cells that are to ...