← Back to C Course | Chapter 12: Advanced Topics | Lesson 20 of 20

C Interview Questions

Interview questions are classic questions asked when applying for C jobs. Practicing them is like rehearsing before a big game.

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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?

c
// 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;
}

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?

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using gets() for input, which is unsafe because it has no size limit.
  2. Comparing strings with == instead of strcmp.
  3. Mixing up call by value and call by reference when answering.
Chapter Summary
  • 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 errno and assert, along with debugging, code style, and common mistakes, help you write reliable code.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.