C Variable Declaration and Scope

Question 1
Consider the following two C lines
int var1;
extern int var2;
Which of the following statements is correct
Cross
Both statements only declare variables, don\'t define them.
Tick
First statement declares and defines var1, but second statement only declares var2
Cross
Both statements declare define variables var1 and var2


Question 1-Explanation: 
Question 2
Predict the output
#include <stdio.h>
int var = 20;
int main()
{
    int var = var;
    printf("%d ", var);
    return 0;
}
Tick
Garbage Value
Cross
20
Cross
Compiler Error


Question 2-Explanation: 
First var is declared, then value is assigned to it. As soon as var is declared as a local variable, it hides the global variable var.
Question 3
#include <stdio.h>
extern int var;
int main()
{
    var = 10;
    printf("%d ", var);
    return 0;
}
Tick
Compiler Error: var is not defined
Cross
20
Cross
0


Question 3-Explanation: 
var is only declared and not defined (no memory allocated for it) Refer: Understanding “extern” keyword in C
Question 4
#include <stdio.h>
extern int var = 0;
int main()
{
    var = 10;
    printf("%d ", var);
    return 0;
}
Tick
10
Cross
Compiler Error: var is not defined
Cross
0


Question 4-Explanation: 
If a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Refer: Understanding “extern” keyword in C
Question 5
Output?
int main()
{
  {
      int var = 10;
  }
  {
      printf("%d", var);  
  }
  return 0;
}
Cross
10
Tick
Compiler Error
Cross
Garbage Value


Question 5-Explanation: 
x is not accessible. The curly brackets define a block of scope. Anything declared between curly brackets goes out of scope after the closing bracket.
Question 6
Output?
#include <stdio.h>
int main()
{
  int x = 1, y = 2, z = 3;
  printf(" x = %d, y = %d, z = %d \n", x, y, z);
  {
       int x = 10;
       float y = 20;
       printf(" x = %d, y = %f, z = %d \n", x, y, z);
       {
             int z = 100;
             printf(" x = %d, y = %f, z = %d \n", x, y, z);
       }
  }
  return 0;
}
Cross
 x = 1, y = 2, z = 3
 x = 10, y = 20.000000, z = 3
 x = 1, y = 2, z = 100
Cross
Compiler Error
Tick
 x = 1, y = 2, z = 3
 x = 10, y = 20.000000, z = 3
 x = 10, y = 20.000000, z = 100 
Cross
 x = 1, y = 2, z = 3
 x = 1, y = 2, z = 3
 x = 1, y = 2, z = 3


Question 6-Explanation: 
Question 7
int main()
{
  int x = 032;
  printf("%d", x);
  return 0;
}
Cross
32
Cross
0
Tick
26
Cross
50


Question 7-Explanation: 
When a constant value starts with 0, it is considered as octal number. Therefore the value of x is 3*8 + 2 = 26
Question 8
Consider the following C program, which variable has the longest scope?
int a;
int main()
{
   int b;
   // ..
   // ..
}
int c;
Tick
a
Cross
b
Cross
c
Cross
All have same scope


Question 8-Explanation: 
a is accessible everywhere. b is limited to main() c is accessible only after its declaration.
Question 9
Consider the following variable declarations and definitions in C
i) int var_9 = 1;
ii) int 9_var = 2;
iii) int _ = 3;
Choose the correct statement w.r.t. above variables.
Tick
Both i) and iii) are valid.
Cross
Only i) is valid.
Cross
Both i) and ii) are valid.
Cross
All are valid.


Question 9-Explanation: 
In C language, a variable name can consists of letters, digits and underscore i.e. _ . But a variable name has to start with either letter or underscore. It can\'t start with a digit. So valid variables are var_9 and _ from the above question. Even two back to back underscore i.e. __ is also a valid variable name. Even _9 is a valid variable. But 9var and 9_ are invalid variables in C. This will be caught at the time of compilation itself. That\'s why the correct answer is A).  
Question 10
Find out the correct statement for the following program.
#include "stdio.h"

int * gPtr;

int main()
{
 int * lPtr = NULL;

 if(gPtr == lPtr)
 {
   printf("Equal!");
 }
 else
 {
  printf("Not Equal");
 }

 return 0;
}
Tick
It’ll always print Equal.
Cross
It’ll always print Not Equal.
Cross
Since gPtr isn’t initialized in the program, it’ll print sometimes Equal and at other times Not Equal.


Question 10-Explanation: 
It should be noted that global variables such gPtr (which is a global pointer to int) are initialized to ZERO. That’s why gPtr (which is a global pointer and initialized implicitly) and lPtr (which a is local pointer and initialized explicitly) would have same value i.e. correct answer is a.
There are 17 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads