Fresher C Interview Questions
What is C language?
C is a general purpose programming language created by Dennis Ritchie at Bell Labs in 1972, to write the UNIX operating system. It is a procedural, compiled language, which means the code is divided into functions and is converted to machine code before running.
It is fast and close to the hardware, so it is used for operating systems, embedded systems, compilers and databases. Many languages like C++, Java and PHP took their syntax from C. See the C Introduction chapter.
What are the main features of C language?
- Simple: small set of keywords and easy syntax.
- Fast: compiled to machine code.
- Portable: the same code can be compiled on many systems.
- Structured: a program is divided into functions.
- Pointers: direct work with memory addresses.
- Rich library: ready functions for input, output, strings, math and files.
Read more in C Features.
What is the basic structure of a C program?
A C program has header files, a main() function, and statements inside it. The execution starts from main().
#include <stdio.h>
int main(void) {
printf("Hello\n");
return 0;
}
Output:
Hello
#include adds a header, printf() prints the text, and return 0 tells the system that the program finished well. See Basic Program Structure.
What is the difference between a compiler and an interpreter?
| Compiler | Interpreter |
|---|---|
| Converts the whole program to machine code at once | Reads and runs the program line by line |
| Errors are shown for the full program before it runs | Stops at the first error while running |
| Program runs fast after compiling | Usually slower |
| Example: C, C++ | Example: Python, PHP |
C uses a compiler, like GCC or Clang. Learn how to install it in C Installation.
What is the difference between a variable and a constant in C?
A variable stores a value which can change. A constant stores a value which cannot change after it is set.
#include <stdio.h>
#define DAYS 7
int main(void) {
int count = 5;
const float PI = 3.14f;
count = 10; // allowed
// PI = 3.15f; // error: constant cannot change
printf("%d %d %.2f\n", count, DAYS, PI);
return 0;
}
Output:
10 7 3.14
What are the data types in C?
- Basic:
int,char,float,double. - Derived: array, pointer, function.
- User defined:
struct,union,enum,typedef. - void: means no value.
The basic types have modifiers like short, long, signed and unsigned. See the table in C Data Types.
What is the difference between = and == in C?
= is the assignment operator. It stores a value. == is the equality operator. It compares two values and gives 1 or 0.
#include <stdio.h>
int main(void) {
int x = 5;
if (x == 10) printf("equal\n"); // false, nothing printed
if (x = 10) printf("assigned\n"); // assigns 10, always true
printf("x = %d\n", x);
return 0;
}
Output:
assigned
x = 10
The second if changed x to 10 by mistake. See C Operators.
What is the difference between while and do while loop in C?
| while | do while |
|---|---|
| Condition is checked first | Body runs first, condition is checked after |
| Body may run zero times | Body runs at least one time |
| No semicolon after the condition | Semicolon after while (condition); |
Example: with int x = 10; and the condition x < 5, a while loop prints nothing, but a do while loop prints once. See do while loop.
What is the difference between break and continue in C?
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 5; i++) {
if (i == 2) continue; // skip 2
if (i == 4) break; // stop at 4
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
1 3
continue skipped 2, and break ended the loop at 4. See break and continue.
What is the difference between call by value and call by reference in C?
In call by value the function gets a copy of the variable, so the original value does not change. In call by reference we pass the address (a pointer), so the function can change the original value.
#include <stdio.h>
void byValue(int n) { n = 100; }
void byAddress(int *n) { *n = 100; }
int main(void) {
int a = 1, b = 1;
byValue(a);
byAddress(&b);
printf("%d %d\n", a, b);
return 0;
}
Output:
1 100
See the full chapter Call by Value and Reference.
What is an array in C?
An array stores many values of the same data type in continuous memory. Each value is reached with an index, and the index starts from 0.
#include <stdio.h>
int main(void) {
int marks[3] = {70, 85, 90};
printf("%d %d\n", marks[0], marks[2]);
return 0;
}
Output:
70 90
C does not check the array limits, so reading marks[5] is a bug. See C Arrays.
How is a string stored in C?
C has no string type. A string is a char array which ends with the null character '\0'.
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "Hi"; // stored as 'H' 'i' '\0'
printf("%zu %zu\n", strlen(s), sizeof(s));
return 0;
}
Output:
2 3
The length is 2, but the array needs 3 bytes. See C Strings.
What is a pointer in C?
A pointer is a variable which stores the address of another variable. We get the address with & and the value at an address with *.
#include <stdio.h>
int main(void) {
int n = 10;
int *p = &n;
*p = 20;
printf("%d\n", n);
return 0;
}
Output:
20
Changing *p changed n. See C Pointers.
What is a NULL pointer in C?
A NULL pointer does not point to any valid memory. We set a pointer to NULL when we have no address to give yet, and check it before using.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = NULL;
if (p == NULL) {
printf("No memory yet\n");
}
return 0;
}
Output:
No memory yet
Reading or writing through a NULL pointer crashes the program. Functions like malloc() and fopen() return NULL when they fail.
What is the difference between ++i and i++ in C?
#include <stdio.h>
int main(void) {
int a = 5, b = 5;
printf("%d\n", ++a); // increase first, then use
printf("%d\n", b++); // use first, then increase
printf("%d %d\n", a, b);
return 0;
}
Output:
6
5
6 6
Both end with the value 6. The difference is only in the value of the expression. In a simple loop like for (i = 0; i < n; i++) there is no difference.
What is the difference between structure and union in C?
| Structure | Union |
|---|---|
| Each member has separate memory | All members share the same memory |
| Size is the sum of members (plus padding) | Size is the size of the biggest member |
| All members hold values together | Only one member holds a valid value |
#include <stdio.h>
struct S { int i; double d; };
union U { int i; double d; };
int main(void) {
printf("%zu %zu\n", sizeof(struct S), sizeof(union U));
return 0;
}
Output:
16 8
See Structures and Union.
What is typedef in C?
#include <stdio.h>
typedef struct {
int roll;
char name[20];
} Student;
int main(void) {
Student s = {1, "Robin"};
printf("%d %s\n", s.roll, s.name);
return 0;
}
Output:
1 Robin
We wrote Student instead of struct Student. typedef does not create a new type, only a new name. See C typedef.
What is the use of the sizeof operator in C?
sizeof returns the size in bytes of a type or a variable. It is calculated at compile time and has the type size_t.
#include <stdio.h>
int main(void) {
int arr[10];
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(arr));
printf("%zu\n", sizeof(arr) / sizeof(arr[0]));
return 0;
}
Output:
4
40
10
The last line is the common way to find the number of elements of an array. Use %zu to print the value.
What is recursion in C?
Recursion means a function calls itself to solve a smaller part of the problem. It must have a base case, otherwise it never stops.
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1);
}
int main(void) {
printf("%d\n", factorial(5));
return 0;
}
Output:
120
Each call uses stack memory, so a very deep recursion causes a stack overflow. See C Recursion.
What is the difference between local and global variables in C?
| Local variable | Global variable |
|---|---|
| Declared inside a function or block | Declared outside all functions |
| Can be used only in that block | Can be used in all functions |
| Default value is garbage | Default value is 0 |
| Removed when the function ends | Lives until the program ends |
Try to use local variables as much as possible. Global variables can be changed from anywhere, so bugs are hard to find. See Scope and Storage Classes.