CC Programmingbeginner

C Programming Interview Questions for Freshers

25 C questions for campus and viva rounds: pointers, arrays, strings, structures, unions, storage classes and dynamic memory, each with a short answer and output.

Published
TL;DR – Quick Answer

C interviews for freshers focus on pointers, arrays and strings, structures and unions, storage classes, dynamic memory and simple output questions. Learn each answer as a short definition plus a small program whose output you can explain line by line.

On This Page

C programming interview questions for freshers test whether you understand what your code does in memory. Pointers, arrays, structures and storage classes come up in almost every campus round and viva. This page has 25 questions, each with a short answer, and many with a small program and its output.

All programs follow standard C (C11 or later) and include only what matters for the answer. Where an output depends on the machine, the answer says so.

How to answer C interview questions

Use two steps: one-line definition, then a tiny example. "A pointer stores the address of another variable. If p holds the address of marks, then *p reads or changes marks."

For output questions, trace each line aloud. Interviewers give marks for the reasoning even when you slip on one detail.

Pro tip: Keep a notebook of programs whose output surprised you. Revise that list the night before the interview.

C basics questions

Q1. What is C, and why do interviewers still ask it?

30-second answer: C is a general-purpose, compiled programming language created by Dennis Ritchie at Bell Labs in the early 1970s. It gives direct access to memory through pointers and is used in operating systems, embedded systems and compilers.

Interviewers ask C because it shows how well you understand memory, functions and data layout. Those ideas carry over to every other language.

Q2. What are the steps from a .c file to a running program?

30-second answer: Preprocessing, compiling, assembling and linking. The preprocessor handles #include and #define. The compiler turns C into assembly. The assembler makes object code, and the linker joins it with libraries to make the executable.

Step Input Output
Preprocessing main.c Expanded source
Compiling Expanded source Assembly code
Assembling Assembly code Object file (main.o)
Linking Object files and libraries Executable

Q3. What is the difference between a declaration and a definition?

30-second answer: A declaration tells the compiler a name and its type. A definition also creates the storage or the function body.

extern int count;          /* declaration only */
int count = 0;             /* definition */
int add(int a, int b);     /* function declaration (prototype) */
int add(int a, int b) {    /* function definition */
    return a + b;
}

A variable can be declared many times but defined only once in the program.

Q4. What are the basic data types in C, and what are their sizes?

30-second answer: The basic types are char, int, float and double, with modifiers such as short, long, signed and unsigned. Only char has a fixed size of 1 byte. The others depend on the compiler and machine.

On most current 64-bit systems, int and float are 4 bytes and double is 8 bytes. Use sizeof to check and print it with %zu.

Q5. What is the output of this division program?

#include <stdio.h>

int main(void) {
    int x = 10;
    float f = 5 / 2;
    printf("%d %d\n", x / 3, x % 3);
    printf("%.1f\n", f);
    return 0;
}

Output:

3 1
2.0

30-second answer: 10 / 3 is integer division, so the result is 3 and the remainder is 1. In 5 / 2, both values are integers, so the result is 2 before it is stored in f. Write 5.0 / 2 to get 2.5.

Q6. What is the output of i++ + ++i?

30-second answer: There is no correct output. Changing i twice in one expression without a sequence point is undefined behaviour. Different compilers can print different values, and the program has no guaranteed meaning.

Common mistake: Giving a number with confidence. Saying "this is undefined behaviour" is the right answer.

Pointer interview questions

Q7. What is a pointer?

30-second answer: A pointer is a variable that stores the address of another variable. The & operator gives an address, and the * operator reads or writes the value at that address.

#include <stdio.h>

int main(void) {
    int marks = 90;
    int *p = &marks;
    printf("%d\n", marks);
    printf("%d\n", *p);
    *p = 95;
    printf("%d\n", marks);
    return 0;
}

Output:

90
90
95

Changing *p changed marks, because p points to it.

Q8. What are NULL, void, dangling and wild pointers?

30-second answer: A NULL pointer points to nothing on purpose. A void pointer (void *) can hold the address of any type but must be cast before you read through it. A dangling pointer still holds an address that is no longer valid, such as memory after free. A wild pointer was never initialised.

Pointer Example Safe to read through?
NULL int *p = NULL; No, check first
void void *v = &x; Only after a cast
Dangling Using p after free(p) No
Wild int *p; with no value No

Set a pointer to NULL after free so that a later check can catch it.

Q9. How does pointer arithmetic work?

30-second answer: Adding 1 to a pointer moves it to the next element of its type, not the next byte. For an int *, p + 1 moves by sizeof(int) bytes.

#include <stdio.h>

int main(void) {
    int a[] = {10, 20, 30, 40};
    int *p = a;
    printf("%d\n", *(p + 2));
    p++;
    printf("%d\n", *p);
    return 0;
}

Output:

30
20

Q10. What is the difference between call by value and passing an address?

30-second answer: C always passes arguments by value, so a function gets a copy. To change the caller's variable, pass its address and change the value through the pointer.

#include <stdio.h>

void swapCopy(int x, int y) { int t = x; x = y; y = t; }
void swap(int *x, int *y)   { int t = *x; *x = *y; *y = t; }

int main(void) {
    int a = 3, b = 7;
    swapCopy(a, b);
    printf("%d %d\n", a, b);
    swap(&a, &b);
    printf("%d %d\n", a, b);
    return 0;
}

Output:

3 7
7 3

The first function swapped its own copies. The second changed a and b through their addresses.

Q11. What is a pointer to a pointer?

30-second answer: It is a variable that stores the address of another pointer. It is declared with two stars.

int n = 5;
int *p = &n;
int **pp = &p;
printf("%d\n", **pp);   /* prints 5 */

You use it when a function must change a pointer itself, for example to allocate memory for the caller.

Q12. What is the difference between const int *p and int *const p?

30-second answer: const int *p is a pointer to a constant int. You cannot change the value through p, but you can point p somewhere else. int *const p is a constant pointer. It must always point to the same place, but the value there can change.

Read it right to left: "p is a const pointer to int" or "p is a pointer to const int".

Arrays and strings questions

Q13. Is an array the same as a pointer?

30-second answer: No. An array is a block of elements. In most expressions its name turns into a pointer to the first element. But sizeof on an array gives the whole array size, and you cannot assign a new address to an array name.

#include <stdio.h>

void show(int arr[]) {
    printf("%zu\n", sizeof(arr));   /* arr is a pointer here */
}

int main(void) {
    int a[5] = {0};
    printf("%zu\n", sizeof(a));
    show(a);
    return 0;
}

On a typical 64-bit system where int is 4 bytes, this prints 20 and then 8. Inside show, arr is only a pointer, so pass the length as a separate argument.

Q14. How are strings stored in C?

30-second answer: A string is an array of char that ends with the null character '\0'. strlen counts characters before '\0'. sizeof on the array also counts the '\0'.

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "code";
    printf("%zu %zu\n", sizeof(s), strlen(s));
    return 0;
}

Output:

5 4

Q15. Why should you not use gets?

30-second answer: gets does not know the size of the buffer, so long input can overflow it. It was removed from the C standard in C11. Use fgets(buf, sizeof buf, stdin), which stops at the buffer size.

Q16. What values do missing array elements get?

30-second answer: If you give fewer values than the array size, the rest are set to zero. A local array with no initialiser at all holds garbage values.

int a[3] = {1};
printf("%d %d\n", a[1], a[2]);   /* prints 0 0 */

Structures and unions questions

Q17. What is the difference between a structure and a union?

30-second answer: In a structure, every member has its own memory, so the size is at least the sum of the members. In a union, all members share the same memory, so the size is at least the size of the largest member. Only one union member holds a useful value at a time.

struct Student { int roll; char grade; float cgpa; };
union Value    { int i; char c; float f; };

On common compilers, sizeof(struct Student) is 12 because of padding after grade, and sizeof(union Value) is 4. Say "typically", since padding depends on the compiler.

Q18. What is structure padding?

30-second answer: The compiler may add unused bytes between members so that each member starts at an address suited to its type. This makes memory access faster on most processors. Placing larger members first often reduces padding.

Q19. What do typedef and the -> operator do?

30-second answer: typedef gives a type a new name. The -> operator reads a member through a pointer to a structure. p->roll means the same as (*p).roll.

typedef struct { int roll; float cgpa; } Student;

Student s = {101, 8.5f};
Student *p = &s;
printf("%d %.1f\n", p->roll, p->cgpa);   /* prints 101 8.5 */

Storage class questions

Q20. What are the storage classes in C?

30-second answer: The four classic ones are auto, register, static and extern. They decide where a variable lives, how long it lives and where its name can be used.

Storage class Default value Lifetime Scope
auto Garbage Until the block ends Inside the block
register Garbage Until the block ends Inside the block
static Zero Whole program run Block or file
extern Zero Whole program run Across files

register is only a hint, and you cannot take the address of a register variable. C11 also added _Thread_local, which you can mention as a bonus.

Q21. What is the output of a static local variable?

#include <stdio.h>

void count(void) {
    static int n = 0;
    n++;
    printf("%d ", n);
}

int main(void) {
    count();
    count();
    count();
    printf("\n");
    return 0;
}

Output:

1 2 3

30-second answer: A static local variable is created once and keeps its value between calls. Without static, n would start at 0 each time and the program would print 1 1 1.

Q22. What does extern do?

30-second answer: extern declares a variable that is defined in another file or later in the same file. It does not create new storage.

/* file1.c */
int total = 50;

/* file2.c */
extern int total;   /* uses the total defined in file1.c */

A static global, in contrast, is visible only inside its own file.

Dynamic memory questions

Q23. What is the difference between malloc, calloc, realloc and free?

30-second answer: malloc gives a block of bytes with garbage values. calloc gives memory for a number of elements and sets all bytes to zero. realloc changes the size of an existing block. free returns the memory.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = 3;
    int *a = calloc(n, sizeof(int));
    if (a == NULL) {
        return 1;
    }
    a[0] = 5;
    printf("%d %d %d\n", a[0], a[1], a[2]);
    free(a);
    a = NULL;
    return 0;
}

Output:

5 0 0

Always check for NULL, because allocation can fail.

Q24. What is a memory leak?

30-second answer: A memory leak happens when a program allocates memory and loses every pointer to it without calling free. The memory stays reserved until the program ends. In long-running programs, leaks slowly use up memory.

The usual fix is clear ownership: whoever calls malloc makes sure free is called once on every path.

Q25. Why can a macro give a wrong answer where a function does not?

30-second answer: A macro is plain text replacement done by the preprocessor. It does not evaluate its argument first.

#define SQUARE(x) x * x
#define SQUARE_OK(x) ((x) * (x))

printf("%d\n", SQUARE(2 + 3));      /* 2 + 3 * 2 + 3 = 11 */
printf("%d\n", SQUARE_OK(2 + 3));   /* 25 */

Put brackets around each argument and around the whole macro. A function checks types and evaluates the argument once.

Quick output practice

Predict each output, then check the answers below.

  1. printf("%d\n", 7 / 2 * 2);
  2. printf("%d\n", 5 > 3 && 2 > 4);
  3. int i = 0; while (i < 3) i++; printf("%d\n", i);
  4. int x = 5; int *p = &x; printf("%d\n", *p + 1);
  5. char t[10] = "hi"; printf("%zu %zu\n", sizeof(t), strlen(t));
  6. int a[] = {4, 8, 12}; printf("%d\n", *(a + 1) + a[2]);

Answers: 1) 6, because 7 / 2 is 3. 2) 0, because the second condition is false. 3) 3. 4) 6. 5) 10 2, because sizeof gives the full array size. 6) 20.

How to prepare in one week

Day Focus
1 Data types, operators, integer division
2 Pointers, & and *, swap program
3 Arrays, strings, strlen vs sizeof
4 Structures, unions, padding
5 Storage classes, static and extern
6 malloc, calloc, free, leaks
7 20 output questions and a mock round

Write every program by hand once before you run it. Written tests often give you no compiler.

After C, most interviewers move to object-oriented questions. Continue with C++ interview questions for freshers, and read C vs C++ to explain the difference in one minute. For coding rounds, practise DSA interview questions and write simple programs, such as a prime number check, by hand. Core subject rounds often add operating system interview questions, which build on the memory ideas here.

Frequently Asked Questions

Which C topics are asked most in fresher interviews?
Pointers come first, then arrays and strings, structures and unions, storage classes, malloc and free, and output prediction. Many viva rounds also ask about the compilation steps and the difference between call by value and passing an address.
Is C still useful if I want a Java or Python job?
Yes. Service companies and core companies still ask C in written tests and first technical rounds because it shows whether you understand memory, loops and functions. The same ideas help you explain references in Java and objects in Python.
Do I need to memorise the size of every data type?
No. Remember that char is 1 byte by definition and that the other sizes depend on the compiler and machine. On most current 64-bit systems int is 4 bytes and a pointer is 8 bytes. Say 'typically' and use sizeof to check.
What should I say when an output question has undefined behaviour?
Say so clearly. For example, modifying the same variable twice in one expression without a sequence point, such as i++ + ++i, is undefined behaviour. The interviewer usually wants to see that you know this, not a guessed number.
How should I practise C output questions?
Write small programs, predict the output on paper and then run them. When your guess is wrong, find the exact line where your thinking went wrong. Twenty such programs teach more than reading a hundred answers.
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us