Skip to content
FACE PREP CAMPUS

C Programming Coding Questions and Answers

Whether you’re preparing for exams, placements, or just sharpening your skills, these C programming Coding questions cover everything from basic syntax to pointers and file handling. Each question includes a clean code example, expected output, and a plain-English explanation of how it works.

Table of Contents

SectionTopics
Basic C Programming QuestionsQ1 — Print Hello World
Q2 — Sum of Two Numbers
Q3 — Check Odd or Even
Q4 — Factorial Using a Loop
Q5 — Fibonacci Series
Intermediate C Programming QuestionsQ6 — Reverse an Array
Q7 — Check Palindrome String
Q8 — Factorial Using Recursion
Q9 — Find Largest in Array
Advanced C Programming QuestionsQ10 — Swap Using Pointers
Q11 — Structure for Student Record
Q12 — Dynamic Memory with malloc
Frequently Asked QuestionsFAQs on C Programming
Keep PractisingPractice More C Programming Problems

All code examples compile and run on GCC. Test them on any standard C compiler — onlinegdb.com works great if you don’t have a local setup.


Basic C programming Coding questions

These C programming Coding questions cover fundamental concepts every beginner must know — data types, operators, loops, and conditionals.

  1. Print Hello WorldEasy

Write a C program to print “Hello, World!” on the screen.

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Output

Every C program starts execution from main(). The printf() function prints text to the console. \n moves the cursor to a new line.

2. Sum of two numbersEasy

Write a program to read two integers and print their sum.

#include <stdio.h>

int main() {
    int a, b, sum;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    sum = a + b;
    printf("Sum = %d\n", sum);
    return 0;
}

Output

scanf() reads input from the user. The & operator passes the memory address of each variable so scanf can store the value directly.

3. Check odd or evenEasy

Write a program to check whether a number is odd or even.

#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n % 2 == 0)
        printf("%d is Even\n", n);
    else
        printf("%d is Odd\n", n);
    return 0;
}

Output

The modulus operator % returns the remainder of division. If a number divides evenly by 2 (remainder 0), it is even. Otherwise it is odd.

4. Factorial using a loopEasy

Write a program to find the factorial of a number using a for loop.

#include <stdio.h>

int main() {
    int n, i;
    long long fact = 1;
    printf("Enter a number: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
        fact *= i;
    printf("Factorial of %d = %lld\n", n, fact);
    return 0;
}

Output

The loop multiplies every integer from 1 to n. Using long long prevents overflow for larger inputs since factorials grow very fast.

5. Fibonacci seriesEasy

Print the first n terms of the Fibonacci series.

#include <stdio.h>

int main() {
    int n, a = 0, b = 1, next, i;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    return 0;
}

Output

Each term equals the sum of the previous two. The loop updates a and b in each iteration by shifting them forward by one position.

C programming coding questions and answers sample

Intermediate C programming questions

These questions test your understanding of arrays, functions, strings, and recursion — the core of any serious C programming practice session.

6. Reverse an array Medium

Write a program to reverse an array of integers.

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5, i, temp;
    for (i = 0; i < n / 2; i++) {
        temp = arr[i];
        arr[i] = arr[n - 1 - i];
        arr[n - 1 - i] = temp;
    }
    printf("Reversed array: ");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    return 0;
}

Output

The loop swaps elements from both ends moving toward the centre. You only need to iterate halfway — swapping past the midpoint would undo previous swaps.

7. Check palindrome stringMedium

Write a program to check whether a string is a palindrome.

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

int main() {
    char str[100];
    int i, n, flag = 1;
    printf("Enter a string: ");
    scanf("%s", str);
    n = strlen(str);
    for (i = 0; i < n / 2; i++) {
        if (str[i] != str[n - 1 - i]) {
            flag = 0;
            break;
        }
    }
    if (flag)
        printf("%s is a palindrome\n", str);
    else
        printf("%s is not a palindrome\n", str);
    return 0;
}

Output

The program compares characters from both ends moving inward. If any pair mismatches, the string is not a palindrome. The flag variable tracks whether a mismatch occurred.

8. Factorial using recursionMedium

Write a recursive function to calculate the factorial of a number.

#include <stdio.h>

long long factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    return n * factorial(n - 1);
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial = %lld\n", factorial(n));
    return 0;
}

Output

The function calls itself with a smaller value each time until it hits the base case (0 or 1). Each call returns its result to the previous call, building the final answer on the way back up the call stack.

9. Find largest in arrayMedium

Write a program to find the largest element in an array.

#include <stdio.h>

int main() {
    int arr[] = {12, 45, 7, 89, 23};
    int n = 5, i, max = arr[0];
    for (i = 1; i < n; i++) {
        if (arr[i] > max)
            max = arr[i];
    }
    printf("Largest element = %d\n", max);
    return 0;
}

Output

Start by assuming the first element is the largest. The loop checks each remaining element — if any beats the current max, it becomes the new max.


Advanced C programming questions

These C programming questions cover pointers, structures, and memory management — topics that appear frequently in technical interviews and university exams.

10. Swap using pointersAdvanced

Write a function to swap two numbers using pointers.

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    printf("Before swap: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swap:  x = %d, y = %d\n", x, y);
    return 0;
}

Output

Passing variables by value would not change the originals. Passing their addresses lets the function directly modify the values at those memory locations using the dereference operator .

11. Structure for student recordAdvanced

Write a program to store and display student details using a structure.

#include <stdio.h>

struct Student {
    char name[50];
    int age;
    float marks;
};

int main() {
    struct Student s1 = {"Arjun", 20, 92.5};
    printf("Name  : %s\n", s1.name);
    printf("Age   : %d\n", s1.age);
    printf("Marks : %.1f\n", s1.marks);
    return 0;
}

Output

struct groups different data types under one name. A single Student variable holds a string, an integer, and a float — accessed using the dot operator.

12. Dynamic memory with mallocAdvanced

Write a program to allocate memory dynamically for an array using malloc.

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

int main() {
    int n, i;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    int *arr = (int*)malloc(n * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    for (i = 0; i < n; i++) {
        arr[i] = (i + 1) * 10;
        printf("%d ", arr[i]);
    }
    free(arr);
    return 0;
}

Output

malloc() allocates a block of memory at runtime. Always check if the returned pointer is NULL and always call free() when done to prevent memory leaks.


Frequently asked questions

Which C programming questions appear most in placement interviews?

Interviewers most commonly ask about pointers, string manipulation, recursion, sorting algorithms, and linked lists. Focus on understanding how memory works in C — that separates average answers from strong ones.

How do beginners start practising C programming questions?

Start with basic input/output, then move to loops and conditions, then arrays and strings, and finally pointers and structures. Write every program yourself — don’t just read the solution.

What compiler should beginners use for C programming?

GCC is the standard choice. Install it via Code::Blocks or Dev-C++ on Windows, or use the terminal on Linux/Mac. For quick practice online, onlinegdb.com runs C code instantly in your browser.

What is the difference between arrays and pointers in C?

An array stores a fixed-size collection of elements in contiguous memory. A pointer stores a memory address. Arrays and pointers relate closely — the array name itself acts as a pointer to the first element — but they are not the same thing.


Keep practising

The best way to get better at C programming coding questions is simple: write code every day. Don’t memorise solutions — understand the logic, then close the page and write it yourself from scratch. That is what builds real skill for exams and interviews alike.

Bookmark this page and work through 2–3 questions daily. Start from Question 1 if you’re a beginner — the difficulty builds gradually from basic to advanced.


Author

FACE Prep Campus