C language questions
C language questions
Java?
→ Header files provide function declarations and macros to be included in
multiple source files.
What is the difference between #include <stdio.h> and #include
"myfile.h"?
→ <stdio.h> searches in standard directories, while "myfile.h" searches in the
local directory first.
Why does C use pointers instead of references like C++?
→ C does not support references, and pointers provide direct memory access.
What is the purpose of the register keyword in C?
→ It suggests storing the variable in a CPU register for faster access.
How does function pointer syntax work in C?
→ A function pointer is declared as returnType (*pointerName)(parameterType);.
What is the default return type of main() in C?
→ The default return type of main() is int.
Why doesn’t C support function overloading?
→ C does not support function overloading because it lacks name mangling like
C++.
How do malloc() and calloc() differ?
→ malloc() allocates uninitialized memory, while calloc() initializes it to zero.
Why does gets() pose a security risk in C?
→ gets() does not check buffer size, leading to buffer overflow vulnerabilities.
What is the purpose of sizeof() in C?
→ It returns the size of a data type or variable in bytes.
What does volatile mean in C?
→ It tells the compiler not to optimize a variable because its value may change
unexpectedly.
Why does C not have built-in exception handling like Java?
→ C focuses on low-level programming and relies on return values for error
handling.
What is the difference between const and #define?
→ const defines a constant variable, while #define creates a preprocessor macro.
How does C handle strings internally?
→ Strings in C are character arrays terminated by a null character (\0).
What is the difference between struct and union?
→ struct allocates separate memory for each member, while union shares
memory among members.
What is a void pointer in C?
→ A void * pointer can hold the address of any data type but needs casting for
dereferencing.
How does pointer arithmetic work in C?
→ It adjusts the memory address based on the data type size.
What happens if you free the same pointer twice in C?
→ It leads to undefined behavior, possibly causing a segmentation fault.
What is the purpose of extern in C?
→ It declares a global variable or function that is defined in another file.
What does the restrict keyword do in C?
→ It tells the compiler that a pointer is the only reference to its memory location,
allowing optimizations.
Why does int arr[5] = {1, 2, 3}; not initialize all elements explicitly?
→ Unspecified elements are automatically initialized to zero.
What is the output of sizeof('A') in C?
→ sizeof('A') is usually 4 (or sizeof(int)) because character literals are treated as
int.
Why do we use fflush(stdin); in C?
→ It is often misused to clear input buffers, but its behavior is undefined for stdin.
How do you implement a dynamically growing array in C?
→ By using malloc(), realloc(), and free().
What is a segmentation fault in C?
→ It occurs when accessing an invalid or unallocated memory location.
Why does C use fopen() instead of open() for file handling?
→ fopen() is part of the standard I/O library and provides buffered file handling.
What is the difference between lvalue and rvalue in C?
→ lvalue refers to a memory location, while rvalue refers to a data value.
What is a wild pointer in C?
→ A pointer that is declared but not initialized, leading to undefined behavior.
How does C handle memory leaks?
→ C does not have automatic garbage collection; memory must be freed
manually using free().
Why is main() special in C?
→ It serves as the entry point of program execution.