C Storage Classes and Type Qualifiers

Question 1

Which of the following is not a storage class specifier in C?
 

Cross

auto
 

Cross

register
 

Cross

static
 

Cross

extern
 

Tick

volatile
 



Question 1-Explanation: 

volatile is not a storage class specifier. volatile and const are type qualifiers.

Question 2
Output of following program?
#include <stdio.h>
int main()
{
    static int i=5;
    if(--i){
        main();
        printf("%d ",i);
    }
    return 0;
}
Cross
4 3 2 1
Cross
1 2 3 4
Tick
0 0 0 0
Cross
Compiler Error


Question 2-Explanation: 
A static variable is shared among all calls of a function. All calls to main() in the given program share the same i. i becomes 0 before the printf() statement in all calls to main().
Question 3
#include <stdio.h>
int main()
{
    static int i=5;
    if (--i){
        printf("%d ",i);
        main();
    }
}
Tick
4 3 2 1
Cross
1 2 3 4
Cross
4 4 4 4
Cross
0 0 0 0


Question 3-Explanation: 
Since i is static variable, it is shared among all calls to main(). So is reduced by 1 by every function call.
Question 4
#include <stdio.h>
int main()
{
    int x = 5;
    int * const ptr = &x;
    ++(*ptr);
    printf("%d", x);
  
    return 0;
}
Cross
Compiler Error
Cross
Runtime Error
Tick
6
Cross
5


Question 4-Explanation: 
See following declarations to know the difference between constant pointer and a pointer to a constant. int * const ptr —> ptr is constant pointer. You can change the value at the location pointed by pointer p, but you can not change p to point to other location. int const * ptr —> ptr is a pointer to a constant. You can change ptr to point other variable. But you cannot change the value pointed by ptr. Therefore above program works well because we have a constant pointer and we are not changing ptr to point to any other location. We are only incrementing value pointed by ptr.
Question 5
#include <stdio.h>
int main()
{
    int x = 5;
    int const * ptr = &x;
    ++(*ptr);
    printf("%d", x);
  
    return 0;
}
Tick
Compiler Error
Cross
Runtime Error
Cross
6
Cross
5


Question 5-Explanation: 
See following declarations to know the difference between constant pointer and a pointer to a constant. int * const ptr —> ptr is constant pointer. You can change the value at the location pointed by pointer p, but you can not change p to point to other location. int const * ptr —> ptr is a pointer to a constant. You can change ptr to point other variable. But you cannot change the value pointed by ptr. In the above program, ptr is a pointer to a constant. So the value pointed cannot be changed.
Question 6
#include<stdio.h>
int main()
{
  typedef static int *i;
  int j;
  i a = &j;
  printf("%d", *a);
  return 0;
}
Cross
Runtime Error
Cross
0
Cross
Garbage Value
Tick
Compiler Error


Question 6-Explanation: 
Compiler Error -> Multiple Storage classes for a. In C, typedef is considered as a storage class. The Error message may be different on different compilers.
Question 7
Output?
#include<stdio.h>
int main()
{
   typedef int i;
   i a = 0;
   printf("%d", a);
   return 0;
}
Cross
Compiler Error
Cross
Runtime Error
Tick
0
Cross
1


Question 7-Explanation: 
There is no problem with the program. It simply creates a user defined type i and creates a variable a of type i.
Question 8
#include<stdio.h>
int main()
{
  typedef int *i;
  int j = 10;
  i *a = &j;
  printf("%d", **a);
  return 0;
}
Tick
Compiler Error
Cross
Garbage Value
Cross
10
Cross
0


Question 8-Explanation: 
Compiler Error -> Initialization with incompatible pointer type. The line typedef int *i makes i as type int *. So, the declaration of a means a is pointer to a pointer. The Error message may be different on different compilers.
Question 9
Output?
#include <stdio.h>
int fun()
{
  static int num = 16;
  return num--;
}

int main()
{
  for(fun(); fun(); fun())
    printf("%d ", fun());
  return 0;
}
Cross
Infinite loop
Cross
13 10 7 4 1
Tick
14 11 8 5 2
Cross
15 12 8 5 2


Question 9-Explanation: 
Since num is static in fun(), the old value of num is preserved for subsequent functions calls. Also, since the statement return num– is postfix, it returns the old value of num, and updates the value for next function call.
fun() called first time: num = 16 // for loop initialization done;


In test condition, compiler checks for non zero value

fun() called again : num = 15

printf(\"%d \\n\", fun());:num=14 ->printed

Increment/decrement condition check

fun(); called again : num = 13

----------------

fun() called second time: num: 13 

In test condition,compiler checks for non zero value

fun() called again : num = 12

printf(\"%d \\n\", fun());:num=11 ->printed

fun(); called again : num = 10

--------

fun() called second time : num = 10 

In test condition,compiler checks for non zero value

fun() called again : num = 9

printf(\"%d \\n\", fun());:num=8 ->printed

fun(); called again   : num = 7

--------------------------------

fun() called second time: num = 7

In test condition,compiler checks for non zero value

fun() called again : num = 6

printf(\"%d \\n\", fun());:num=5 ->printed

fun(); called again   : num = 4

-----------

fun() called second time: num: 4 

In test condition,compiler checks for non zero value

fun() called again : num = 3

printf(\"%d \\n\", fun());:num=2 ->printed

fun(); called again   : num = 1

----------

fun() called second time: num: 1 

In test condition,compiler checks for non zero value

fun() called again : num = 0 => STOP 
Question 10
#include <stdio.h>
int main() 
{ 
  int x = 10; 
  static int y = x; 
  
  if(x == y) 
     printf("Equal"); 
  else if(x > y) 
     printf("Greater"); 
  else
     printf("Less"); 
  return 0; 
}
Tick
Compiler Error
Cross
Equal
Cross
Greater
Cross
Less


Question 10-Explanation: 
In C, static variables can only be initialized using constant literals. This is allowed in C++ though.  See this GFact for details.
There are 31 questions to complete.

  • Last Updated : 09 Sep, 2021

Share your thoughts in the comments
Similar Reads