123

Storage Classes and Type Qualifiers

Question 1
Which of the following is not a storage class specifier in C?
A
auto
B
register
C
static
D
extern
E
volatile
F
typedef
Storage Classes and Type Qualifiers    
Discuss it


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);
    }
}
A
4 3 2 1
B
1 2 3 4
C
0 0 0 0
D
Compiler Error
Storage Classes and Type Qualifiers    
Discuss it


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();
    }
}
A
4 3 2 1
B
1 2 3 4
C
4 4 4 4
D
0 0 0 0
Storage Classes and Type Qualifiers    
Discuss it


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;
}
A
Compiler Error
B
Runtime Error
C
6
D
5
Storage Classes and Type Qualifiers    
Discuss it


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 icrementing value pointed by ptr.
Question 5
#include <stdio.h>
int main()
{
    int x = 5;
    int const * ptr = &x;
    ++(*ptr);
    printf("%d", x);
  
    return 0;
}
A
Compiler Error
B
Runtime Error
C
6
D
5
Storage Classes and Type Qualifiers    
Discuss it


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;
}
A
Runtime Error
B
0
C
Garbage Value
D
Compiler Error
Storage Classes and Type Qualifiers    
Discuss it


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;
}
A
Compiler Error
B
Runtime Error
C
0
D
1
Storage Classes and Type Qualifiers    
Discuss it


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;
}
A
Compiler Error
B
Garbage Value
C
10
D
0
Storage Classes and Type Qualifiers    
Discuss it


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;
}
A
Infinite loop
B
13 10 7 4 1
C
14 11 8 5 2
D
15 12 8 5 2
Storage Classes and Type Qualifiers    
Discuss it


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; 
}
A
Compiler Error
B
Equal
C
Greater
D
Less
Storage Classes and Type Qualifiers    
Discuss it


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 25 questions to complete.
123

Company Wise Coding Practice    Topic Wise Coding Practice