Storage-Classes
Question 1 |
The default storage class for functions in ‘C’ language is:
Static | |
Register | |
Extern | |
Auto |
Question 1 Explanation:
The default storage class for variable is auto and function is extern.
We can use functions in multiple files with the help of header files.
We can use functions in multiple files with the help of header files.
Question 2 |
Which of the following storage classes have global visibility in C/C++ ?
Auto | |
Extern | |
Static | |
Register |
Question 2 Explanation:
→ Register and auto variables have limited scope (the block in which they are declared) and limited lifetimes (as for automatic variables). However, in some applications it may be useful to have data which is accessible from within any block and/or which remains in existence for the entire execution of the program. Such variables are called global variables. The scope of external variables is global.
Question 3 |
After 3 calls of the c function bug( ) below, the values of i and j will be :
int j=1;
bug( )
{
static int i=0; int j=0;
i++;
j++;
return (i);
}
int j=1;
bug( )
{
static int i=0; int j=0;
i++;
j++;
return (i);
}
i = 0, j = 0 | |
i = 3, j = 3 | |
i = 3, j = 0 | |
i = 3, j = 1 |
Question 3 Explanation:
Step-1: Initially i=0 but given as static. The scope is lifetime.
Step-2: Call-1: i=1 and j=1
Call-2: i=2 and j=1 because ‘i’ is static ‘j’ is again become 0 when it starts new call.
Call-3: i=3 and j=1 because ‘i’ is static ‘j’ is again become 0 when it starts new call.
Note: A static ‘int’ variable remains in memory while the program is running.
Step-2: Call-1: i=1 and j=1
Call-2: i=2 and j=1 because ‘i’ is static ‘j’ is again become 0 when it starts new call.
Call-3: i=3 and j=1 because ‘i’ is static ‘j’ is again become 0 when it starts new call.
Note: A static ‘int’ variable remains in memory while the program is running.