1. What do you mean by scope of a variable? What are the different types of scopes that a variable can have?
— Scope Indicates the region over which the variable's declaration has an effect. The four kinds of scopes are — file,function,block,prototype.
2. What are differences between a declaration and a definition?
— There are two differences :
i⇒ In the definition of a variable space is reserved for the variable and some initial value is given to it, whereas a declaration only identifies the type of the variable.
ii⇒ Redefinition is an error, whereas, redeclaration is not an error.
E.g. ⇒ a) extern int i;
b) int j;
'a' is declaration and 'b' is definition.
More Examples:
A)double pow (double, double)
B)float square (float x) {…}
'A' is declaration and 'B' is definition.
3. Is it true that a global variable and a function may have several declarations but only one definition?
–Yes
4. If the definition of an external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.
— True
5. Which statement should you add to the function main() to make it work?
1 2 3 4 5 6 7 |
#include <stdio.h> int main() { printf("%c",x); return 0; } char x='a'; |
— extern char x;
6. Consider below program:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> int main() { struct book { char name[20]; int noofpages; float price; }; struct book b={0}; printf("%d %f\n",b.noofpages,b.price); return 0; } |
Which outputs – 0 0.000000 How can we obtain same effect in any other way?
— The same effect can be obtained using the following statement: static struct book b; Being static, all elements of b would be initialized to 0.
7. If you are to share the variables or functions across several source files how would you ensure that all definitions and declarations are consistent?
— The best arrangement is to place each definition in a relevent '.c' file. Then, put an external declaration in a header file ('.h' file) and use #include to bring in the declaration wherever needed. The '.c' file which contains the definition should also include the header file,so that the compiler can check that the definition matches the declaration.
8. What do you mean by translation unit?
— A translation unit is a set of source files seen by the compiler and translated as a unit—generally one '.c' file, plus all header files mentioned in #include directives.
9. How would we initialize a function pointer and call that function?
—
1 2 3 4 |
//initializing function pointer p to the address of function fun() int (*p)() = fun; // calling function with the pointer; (*p)(); |
10. What are the different types of linkages?
— There are three different types of linkages—external, internal and none. External linkage means gloabl, non-static variables and functions, internal linkages means static variables and functions with file scope, and no linkage menas local variables.
11. What is size_t?
— It is the type of the result of the sizeof oerator. size_t is used to express the size of something or the number of characters in something. For example, it is the type that you pass to malloc() to indicate how many bytes you wish to allocate. Or it is the type returned by strlen() to indicate the number of characters in a string. Each compiler implementation chooses a type like unsigned int or unsigned long (or something else) to be its size_t, depending on what makes most sense . Each implementation publishes its own choiceof size_t in several header files like 'stdio.h', 'stdlib.h', etc. In most implementations size_t is defined as:
1 |
typedef unsigned int size_t; |
This means that on this particular implementation size_t is an unsigned int. Other implementations may make other choices. What is important is that you should not worry about what size_t looks like for a particular implementation; all you should care about is that it is the right type for representing object sizes and count.
12. what is storage class of i in the following statement?
1 |
int i; |
— The storage class of i is auto if declared inside the main() and extern if it is declared outside the main().
13. Can the variables i and j declared below be used in any other file of the same program?
1 2 |
auto int i; static int j; |
— It can be used provided it is declared as
1 |
extern int i; |
in the file which it is being used. j cannot be used because of its static storage class.
14. what values would be stored in variables i and j defined below?
1 2 |
int i; float j; |
— The values stored in these variables depend upon where they are defined. If they are defined outside all functions, they would be treated as external storage class variables and hence would contain default values 0 and 0.0 respectively.
However, if they are defined inside a function then they would be treated as automatic storage class variables. In this case the variables would contain garbage values.