• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Variable Declaration and Scope

Question 1

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

Question 2

Predict the output C
#include <stdio.h>
int var = 20;
int main()
{
    int var = var;
    printf(\"%d \", var);
    return 0;
}
  • Garbage Value
  • 20
  • Compiler Error

Question 3

C
#include <stdio.h>
extern int var;
int main()
{
    var = 10;
    printf(\"%d \", var);
    return 0;
}
  • Compiler Error: var is not defined
  • 20
  • 0

Question 4

C
#include <stdio.h>
extern int var = 0;
int main()
{
    var = 10;
    printf(\"%d \", var);
    return 0;
}
  • 10
  • Compiler Error: var is not defined
  • 0

Question 5

Output? C
int main()
{
  {
      int var = 10;
  }
  {
      printf(\"%d\", var);  
  }
  return 0;
}
  • 10
  • Compiler Error
  • Garbage Value

Question 6

Output? C
#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;
}
  •  x = 1, y = 2, z = 3
     x = 10, y = 20.000000, z = 3
     x = 1, y = 2, z = 100
  • Compiler Error
  •  x = 1, y = 2, z = 3
     x = 10, y = 20.000000, z = 3
     x = 10, y = 20.000000, z = 100 
  •  x = 1, y = 2, z = 3
     x = 1, y = 2, z = 3
     x = 1, y = 2, z = 3

Question 7

C
int main()
{
  int x = 032;
  printf(\"%d\", x);
  return 0;
}
  • 32
  • 0
  • 26
  • 50

Question 8

Consider the following C program, which variable has the longest scope? C
int a;
int main()
{
   int b;
   // ..
   // ..
}
int c;
  • a
  • b
  • c
  • All have same scope

Question 9

Consider the following variable declarations and definitions in C C
i) int var_9 = 1;
ii) int 9_var = 2;
iii) int _ = 3;
Choose the correct statement w.r.t. above variables.
  • Both i) and iii) are valid.
  • Only i) is valid.
  • Both i) and ii) are valid.
  • All are valid.

Question 10

Find out the correct statement for the following program. C
#include \"stdio.h\"

int * gPtr;

int main()
{
 int * lPtr = NULL;

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

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

There are 17 questions to complete.

Last Updated :
Take a part in the ongoing discussion