C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
C pointer questions
|
# include < stdio.h > |
a. 30
b. 20
c. runtime error
d. compiler error
Answer: (B) 20
Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using the function fun(). So how do we modify the value of a local variable of a function inside another function. Pointer is the solution to such problems. Using pointers, we can modify a local variable of a function inside another function.
2.Output of following program ?
# include |
a. 20
b. 30
c compiler Error
d. runtime error
answer(b) The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an address. In the statement ‘*ptr = 30’, value at address ptr is changed to 30. The address operator & is used to get the address of a variable of any data type. In the function call statement ‘fun(&x)’, address of x is passed so that x can be modified using its address.
3. Can you combine the following two statements into one?
a. char p = *malloc(100);
b. char *p = (char) malloc(100);
c. char *p = (char *)(malloc*)(100);
d. char *p = (char*)malloc(100);
Answer (d)
Explanation: no
4. In which header file is the NULL macro defined?
a. stdio.h
b. stddef.h
c. math.h
d. stdio.h and stddef.h
Answer: Option (d)
Explanation:
The macro “NULL” is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.
5. Assume the float takes 4 bytes, predict the output of following program?
#include |
a. 0.5000003
b. 90.5000011
c. 10.0000013
d. 90.5000003
Answer: (d)
Explanation: When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type.
6. What is the output of this C code?
#include |
a.Machine dependent
b.513
c.258
d.Compiler Error
Answer: (a)
Explanation: Output is 513 in a little endian machine. To understand this output, let integers be stored using 16 bits. In a little endian machine, when we do y[0] = 1 and y[1] = 2, the number a is changed to 00000001 00000010 which is representation of 513 in a little endian machine.
7. What is the output of this C code?
int main() |
a. Compile time error
b. B. Segmentation fault/runtime crash
c. 10
d. Undefined behaviour
Answer: Option (a)
Explanation:
None.
8. What is the output of this C code?
int main() |
a. Compile time error
b. Undefined behaviour
c. 10
d. 0.000000
Answer: Option (a)
Explanation:
None.