C Questions
C Questions
Ans: Static variables are the variables which retain their values between the function calls. They are
initialized only once their scope is within the function in which they are defined.
2. What is a pointer?
Ans: Pointers are variables which stores the address of another variable. That variable may be a scalar
(including another pointer), or an aggregate (array or structure). The pointed-to object may be part of a
larger object, such as a field of a structure or an element in an array.
4. What is a structure?
Ans: Structure constitutes a super data type which represents several different data types in a single unit.
A structure can be initialized if it is static or global.
5. What is a union?
Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by
allocating enough memory to hold the largest member. Here a single area of memory contains values of
different types at different time. A union can never be initialized.
10. What are macros? What are its advantages and disadvantages?
Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is called the
entire code is substituted by a single line though the macro definition is of several lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of
function.
The disadvantage of it is here the entire code is substituted so the program becomes
lengthy if a macro is called several times.
14. Where does global, static, and local, register variables, free memory and C Program
instructions get stored?
Ans: Global: Wherever the linker puts them. Typically the “BSS segment” on many platforms.
Static: Again, wherever the linker puts them. Often, they’re intermixed with the globals. The only
difference between globals and statics is whether the linker will resolve the symbols across compilation
units.Local: Typically on the stack, unless the variable gets register allocated and never spills.Register:
Nowadays, these are equivalent to “Local” variables. They live on the stack unless they get register-
allocated.
17. Describe about storage allocation and scope of global, extern, static, local and register
variables?
Ans:
Globals have application-scope. They’re available in any compilation unit that includes an
appropriate declaration (usually brought from a header file). They’re stored wherever the linker puts them,
usually a place called the “BSS segment.”
Extern? This is essentially “global.”
Static: Stored the same place as globals, typically, but only available to the compilation unit that contains
them. If they are block-scope global, only available within that block and its subblocks.
Local: Stored on the stack, typically. Only available in that block and its subblocks.
(Although pointers to locals can be passed to functions invoked from within a scope where that local is
valid.)
Register: See tirade above on “local” vs. “register.” The only difference is that
the C compiler will not let you take the address of something you’ve declared as “register.”
18. What are register variables? What are the advantages of using register variables?
Ans: If a variable is declared with a register storage class,it is known as register variable.The
register variable is stored in the cpu register instead of main memory.Frequently used variables
are declared as register variable as it’s access time is faster.
20. Can we specify variable field width in a scanf() format string? If possible how?
Ans: All field widths are variable with scanf(). You can specify a maximum field width for a given
field by placing an integer value between the ‘%’ and the field type specifier. (e.g. %64s). Such a specifier
will still accept a narrower field width.
The one exception is %#c (where # is an integer). This reads EXACTLY # characters, and it is the
only way to specify a fixed field width with scanf().
21. Out of fgets() and gets() which function is safe to use and why?
Ans: fgets() is safer than gets(), because we can specify a maximum input length. Neither one is
completely safe, because the compiler can’t prove that programmer won’t overflow the buffer he pass to
fgets ().
25. What is storage class? What are the different storage classes in C?
Ans: Storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope
and linkage. The storage classes in c are auto, register, and extern, static, typedef.
32. In C, why is the void pointer useful? When would you use it?
Ans: The void pointer is useful because it is a generic pointer that any pointer can be cast into and
back again without loss of information.
35. What does the error ‘Null Pointer Assignment’ means and what causes this error?
Ans: As null pointer points to nothing so accessing a uninitialized pointer or invalid location may cause an
error.
37. Are the expressions arr and &arr same for an array of integers?
Ans: Yes for array of integers they are same.
38. IMP>How pointer variables are initialized?
Ans: Pointer variables are initialized by one of the following ways.
I. Static memory allocation
II. Dynamic memory allocation
53. What is the difference between an enumeration and a set of pre-processor # defines?
Ans: There is hardly any difference between the two, except that #defines has a global effect (throughout
the file) whereas an enumeration can have an effect local to the block if desired. Some advantages of
enumeration are that the numeric values are automatically assigned whereas in #define we have to
explicitly define them. A disadvantage is that we have no control over the size of enumeration variables.
54. How are Structure passing and returning implemented by the complier?
Ans: When structures are passed as argument to functions, the entire structure is typically pushed on
the stack. To avoid this overhead many programmer often prefer to pass pointers to structure instead of
actual structures. Structures are often returned from functions in a location pointed to by an extra,
compiler-supported ‘hidden’ argument to the function.
61. What do the ‘c’ and ‘v’ in argc and argv stand for?
Ans: The c in argc(argument count) stands for the number of command line argument the program is
invoked with and v in argv(argument vector) is a pointer to an array of character string that contain the
arguments.
Logical Error
1-logical error are caused by an incorrect algorithm or by a statement mistyped in such a way
that it doesn’t violet syntax of language.
2-difficult to find.
66. Write a program to interchange 2 variables without using the third one.
Ans:
a ^= b; ie a=a^b
b ^= a; ie b=b^a;
a ^= b ie a=a^b;
here the numbers are converted into binary and then xor operation is performed.
You know, you’re just asking “have you seen this overly clever trick that’s not worth applying on
modern architectures and only really applies to integer variables?”
67. What is the maximum combined length of command line arguments including the space
between adjacent arguments?
Ans: It depends on the operating system.
68. What are bit fields? What is the use of bit fields in a Structure declaration?
Ans: A bit field is a set of adjacent bits within a single implementation based storage unit that we
will call a “word”.
The syntax of field definition and access is based on structure.
Struct {
unsigned int k :1;
unsigned int l :1;
unsigned int m :1;
}flags;
the number following the colon represents the field width in bits.Flag is a variable that contains three bit
fields.
72. How would you use the functions randomize() and random()?
Ans:
Randomize() initiates random number generation with a random value.
Random() generates random number between 0 and n-1;
The data type returned for functions fread,fseek and fwrite is int and ftell is long int.
75. What is the difference between the functions memmove() and memcpy()?
Ans: The arguments of memmove() can overlap in memory. The arguments of memcpy() cannot.
Pointers to an array
1-Declaration is data_type ( *array_name)[size];
2-Size represents the column size.
90. Can we use any name in place of argv and argc as command line arguments ?
Ans: yes we can use any user defined name in place of argc and argv;
91. What are the pointer declarations used in C?
Ans:
1- Array of pointers, e.g , int *a[10]; Array of pointers to integer
2-Pointers to an array,e.g , int (*a)[10]; Pointer to an array of into
3-Function returning a pointer,e.g, float *f( ) ; Function returning a pointer to float
4-Pointer to a pointer ,e.g, int **x; Pointer to apointer to int
5-pointer to a data type ,e.g, char *p; pointer to char
93. Is the allocated space within a function automatically deallocated when the function returns?
Ans: No pointer is different from what it points to .Local variables including local pointers
variables in a function are deallocated automatically when function returns.,But in case of a
local pointer variable ,deallocation means that the pointer is deallocated and not the block of
memory allocated to it. Memory dynamically allocated always persists until the allocation is freed
or the program terminates.
96. What are the advantages of using array of pointers to string instead of an array of strings?
Ans:
i) Efficient use of memory.
ii) Easier to exchange the strings by moving their pointers while sorting.
98. What would be the equivalent pointer expression foe referring the same element as
a[p][q][r][s] ?
Ans : *( * ( * ( * (a+p) + q ) + r ) + s)
99. Are the variables argc and argv are always local to main?
Ans: Yes they are local to main.