C Interview Questions
In this page:
Local vs. Global Variables
A local variable is declared inside a function and exists only for the duration of that function's execution, disappearing once the function returns, while a global variable is declared outside any function and persists for the entire lifetime of the program, visible from every function that doesn't shadow it.
Example: Local vs. Global Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare globalVar and set it to 100
int globalVar = 100;
void show() {
// Declare localVar and set it to 5
int localVar = 5;
// Print formatted text to the screen
printf("%d %d", localVar, globalVar);
}
// Program execution starts in main()
int main() {
show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Value vs. Reference Parameters
Pass by value copies a variable's actual data into the function's parameter, so changes made inside the function never affect the caller's original variable, whereas pass by reference (achieved in C using a pointer) passes the variable's address, letting the function modify the original data directly through that pointer.
Example: Value vs. Reference Parameters
// Include standard input/output (printf, scanf)
#include <stdio.h>
void byValue(int x) { x = 100; }
void byRef(int *x) { *x = 100; }
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
byValue(a);
byRef(&a);
// Print formatted text to the screen
printf("%d", a);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structure vs. Union
A struct allocates separate, independent memory for every one of its members, so all fields can be read or written simultaneously without affecting each other, while a union allocates a single shared block of memory sized for its largest member, meaning only one member holds a valid value at any given time.
Example: Structure vs. Union
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named S
struct S { int a; int b; };
union U { int a; int b; };
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%zu %zu", sizeof(struct S), sizeof(union U));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Why is gets() Dangerous?
gets() reads a line of input into a buffer with absolutely no way to limit how many characters it writes, so any input longer than the buffer silently overwrites adjacent memory — a classic buffer overflow vulnerability.
It was formally removed from the C standard for exactly this reason; fgets() is the safe replacement.
Example: Why is gets() Dangerous?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char buffer[10];
// Print formatted text to the screen
printf("Use fgets() instead of gets()");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
What is a Null Pointer?
A null pointer is a pointer explicitly set to the value NULL to indicate it doesn't currently point at any valid object, which is the conventional way to represent 'no target yet' and lets code check if (ptr != NULL) before dereferencing to avoid crashing on an uninitialized or already-freed pointer.
Example: What is a Null Pointer?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int *ptr = NULL;
// Print formatted text to the screen
printf("%d", ptr == NULL);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
gets()for input, which is unsafe because it has no size limit. - Comparing strings with
==instead ofstrcmp. - Mixing up call by value and call by reference when answering.
- Features such as
typedef, type casting, bit fields, variable length arrays, and command line arguments extend what C programs can do. - Function pointers and callbacks let you pass behavior around, and the standard headers provide ready-made functions for strings, math, time, and characters.
- Error handling with
errnoandassert, along with debugging, code style, and common mistakes, help you write reliable code.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions