Arrays in C FAQ — 20 Interview Questions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram
  1. Home
  2. Tutorials
  3. C Language
  4. Arrays FAQ

Arrays in C — Frequently Asked Questions (FAQ)

By Sankalan Data Tech · Last updated: 30 October 2026

📑 On this page:
  • What arrays are and how to declare them
  • Initialization and size
  • Arrays vs pointers
  • 1D, 2D, and 3D arrays
  • Common mistakes and bounds
  • Frequently Asked Questions
Arrays in C FAQ — questions on declaring, initializing, sizing, pointers vs arrays, and 1D and 2D arrays
Arrays in C FAQ — interview questions with clear answers.
📚 On this page, you will revise:
  • What an array is and how to declare one
  • Two ways to fill an array with values
  • How to get the number of elements with sizeof
  • Why pointers and arrays are similar, and where they differ
  • One-dimensional, two-dimensional, and three-dimensional arrays
  • Common out-of-bounds mistakes and how to avoid them
  • When to use an array and when to use a struct or a pointer

This page answers 20 of the most-asked array questions from C interviews and exams. It starts with what an array is and how to declare one, then moves through initialization, the sizeof trick for counting elements, the array-vs-pointer comparison, and the 1D / 2D / 3D family. Each answer is in plain English with a short code example you can use in interviews or in your own code. If you're preparing for a technical interview, pay close attention to Q6 (arrays vs pointers), Q10 (the sizeof trick), and Q14 (out-of-bounds reads) — these come up again and again.

What arrays are and how to declare them

Q1. What is an array in C?

Answer: A fixed-size collection of items of the same type, stored together in one block of memory.

An array gives one name to many values of the same type — int, float, char, double, or a struct you define yourself. All elements sit next to each other in memory, and you reach each one by its index, starting from 0:

int marks[5] = {90, 85, 77, 92, 88};
printf("%d\n", marks[0]);   /* 90  — first element  */
printf("%d\n", marks[4]);   /* 88  — last element   */

The word "fixed-size" matters: the size is decided when you write the code and cannot grow at runtime. If you need a growable list, you use dynamic memory (malloc / realloc) or a higher-level structure.

Q2. How do you declare an array in C?

Answer: datatype name_of_array[size]; — for example, int marks[10];

int   marks[10];      /* 10 integers        */
float prices[20];     /* 20 floats          */
char  name[50];       /* 50 characters      */

The size must be a positive integer constant in classic C. C99 also allows variable-length arrays (VLAs), where the size is decided at runtime — but they live on the stack and should be used carefully. You can declare an array with more than one dimension:

int grid[3][4];   /* 3 rows, 4 columns */

Q3. What are the types of arrays in C?

Answer: Based on dimension — one-dimensional (1D), two-dimensional (2D), and multi-dimensional (3D and beyond).

1D array — a single row of values. The most common form:

int a[5] = {10, 20, 30, 40, 50};

2D array — a grid of rows and columns. Useful for matrices, tables, and image pixels:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

3D array — a collection of 2D arrays. C allows any number of dimensions, but each extra level adds complexity and memory cost, so 3D is about as far as most code goes. Higher dimensions are rare outside specialized applications like simulations.

Initialization and size

Q4. How is an array initialized in C?

Answer: Two ways — an initializer list at declaration, or element-by-element assignment using a loop.

Initializer list — the value list is written with the declaration:

int a[5] = {10, 20, 30, 40, 50};

If you give fewer values than the size, the rest are set to 0:

int a[5] = {10, 20};   /* {10, 20, 0, 0, 0} */

Loop-based assignment — after the array is declared, you fill it in a loop:

int a[5];
for (int i = 0; i < 5; i++) {
    a[i] = i * 10;   /* 0, 10, 20, 30, 40 */
}

Use the initializer list for small, fixed values you know at coding time. Use the loop when values come from the user, a file, or a calculation.

Q5. What happens if you initialize an array with more values than its size?

Answer: The compiler raises an error — you cannot pack more elements than the array can hold.

int a[3] = {1, 2, 3, 4};   /* error: too many initializers */

C requires every initializer to fit into the declared size. If you want to grow, declare a larger array or use dynamic memory. Watch out for the opposite case: too few values is fine (the rest become 0), but too many is a compile-time error.

Q6. How do you find the size of an array in C?

Answer: Use the sizeof operator — divide the total array size by the size of one element.

int a[10];
int elements = sizeof(a) / sizeof(a[0]);   /* 10 */
int bytes    = sizeof(a);                  /* typically 40 on 32-bit int */

The divide trick works because the compiler knows the full array size at the point of declaration. Two things to keep in mind:

  • Inside a function, if the array arrives as a parameter, it has already decayed to a pointer — sizeof will return the pointer size, not the array size. Pass the length as a separate parameter in that case.
  • sizeof is not a function. It's evaluated at compile time for fixed-size objects, which is why it's efficient.

Q7. Why do we need arrays in programming?

Answer: One name and one block of memory for many values — simpler to pass, loop over, sort, and store than dozens of separate variables.

With three values, separate variables v1, v2, v3 work fine. With three hundred values, you'd be naming variables v1 through v300, and any loop over them would be impossible. An array gives you one variable that holds hundreds of values, so you can write one loop to process all of them.

Real uses:

  • Grouping related data: a list of marks, prices, or names.
  • Sequential access: walking through elements with a for loop.
  • Efficient storage: contiguous memory means fast indexing — a[i] costs the same no matter how big the array is.
  • Building blocks: arrays underpin stacks, queues, matrices, hash tables, and many sorting algorithms.

Arrays vs pointers

Q8. What is the difference between pointers and arrays in C?

Answer: A pointer is a single variable that holds an address; an array is a fixed block of items of one type. Arrays are allocated by the compiler; pointers can point to any block, including memory you allocate at runtime.

Side by side:

  • Pointer — a derived type that holds the address of another variable. You can change where it points. Allocated wherever the variable lives (stack, heap, or static). One value: an address.
  • Array — a homogeneous collection of items. The name refers to the first element and cannot be reassigned. Allocated at the point of declaration. Many values in contiguous memory.

They look similar in code because the array name decays to a pointer to its first element in most expressions:

int a[3] = {10, 20, 30};
int *p = a;          /* p points to a[0] */
printf("%d\n", p[1]);   /* 20 — same as a[1] */
printf("%d\n", *(p + 2)); /* 30 — same as a[2] */

But they are not the same: sizeof(a) is 12 (3 × 4 bytes on a 32-bit int), while sizeof(p) is the pointer size (usually 8 on 64-bit systems). And you cannot reassign an array name: a = p; is a compile error.

Q9. Can you return an array from a function in C?

Answer: Not directly — C functions return by value, and you can't return a whole array. Return a pointer instead, pointing to memory that stays alive after the function returns.

The dangerous pattern — returning a local array — is a bug, because the array lives on the stack and disappears the moment the function returns:

int *make_bad(void) {
    int a[3] = {1, 2, 3};
    return a;   /* WRONG — a is gone after return */
}

Two safe options:

/* Option 1 — pass the destination array in */
void fill(int *out, int n) {
    for (int i = 0; i < n; i++) out[i] = i;
}

/* Option 2 — allocate on the heap */
int *make_good(int n) {
    int *a = malloc(n * sizeof(int));
    return a;   /* caller must free(a) */
}

1D, 2D, and 3D arrays

Q10. What is a one-dimensional array in C?

Answer: An array with a single index — one row (or column) of values. The most common type.

int a[5] = {10, 20, 30, 40, 50};

/* Access by index: a[0] .. a[4] */
for (int i = 0; i < 5; i++) {
    printf("%d\n", a[i]);
}

A 1D array is what most people mean when they say "array." It holds N elements of the same type in a single row of memory, indexed 0 to N-1.

Q11. What is a two-dimensional array in C?

Answer: A grid of rows and columns — declared as type name[rows][cols];.

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        printf("%d ", matrix[i][j]);
    }
    printf("\n");
}

Output:

1 2 3
4 5 6

In memory, a 2D array is stored as a single row — the first row's elements, then the second row's, and so on (row-major order). Use a 2D array for tables, matrices, grids, and image pixel data.

Q12. What is a three-dimensional array in C?

Answer: A collection of 2D arrays — declared as type name[d1][d2][d3];.

int cube[2][3][4];
/* 2 layers, each 3 rows × 4 columns = 24 ints total */

A 3D array is rare in day-to-day C. It shows up in simulations (voxels), some game data, and when each 2D slice naturally belongs to a group. Each dimension adds indexing overhead and makes the code harder to follow — if you find yourself writing three loops to reach one value, double-check whether 3D is really needed.

Common mistakes and bounds

Q13. What is array index out of bounds in C?

Answer: Reading or writing an element outside the valid index range (0 to size-1). C does not check this for you.

int a[5] = {10, 20, 30, 40, 50};

a[5] = 99;      /* WRONG — valid indexes are 0..4   */
printf("%d\n", a[5]);   /* reads past the end of the array */

Both lines compile without a warning (usually). But the program is touching memory that does not belong to the array. The behaviour is undefined: it may crash, print garbage, or silently corrupt other variables. In security-sensitive code, this is the source of buffer overflow bugs. Always index in 0 .. size-1, and be careful with loop bounds — i < n, not i <= n.

Q14. Can you change the size of an array after it's declared?

Answer: No. An array's size is fixed at declaration. If you need a growable list, use dynamic memory (malloc / realloc).

This is the main limitation of C arrays compared to container types in higher-level languages. You have two workarounds:

  • Declare a maximum size up front and only use part of it. Simple, but wastes memory if the maximum is never reached.
  • Allocate on the heap with malloc and grow with realloc:
    int *a = malloc(10 * sizeof(int));       /* 10 elements  */
    a = realloc(a, 20 * sizeof(int));        /* grow to 20   */
    /* ... use a[0]..a[19] ... */
    free(a);                                  /* release      */

Q15. What is the difference between an array of characters and a string in C?

Answer: A char array holds characters; a string is a char array that ends with a null byte '\0'.

char a[4] = {'c', 'a', 't', '\0'};   /* a string "cat"   */
char b[3] = {'c', 'a', 't'};         /* a char array only — NOT a string */

Every standard string function — strlen, strcpy, printf("%s") — relies on the null terminator to know where the string ends. Without '\0', the function keeps reading past the array, which is undefined behaviour. When you write string literals, the compiler adds the null byte for you: "cat" is stored as 'c', 'a', 't', '\0'.

Q16. How do you copy one array to another in C?

Answer: You cannot assign arrays with =. Use a loop or memcpy from <string.h>.

/* WRONG — will not compile */
int a[5] = {1, 2, 3, 4, 5};
int b[5];
/* b = a;  -- not allowed */

/* RIGHT — loop */
for (int i = 0; i < 5; i++) b[i] = a[i];

/* RIGHT — memcpy */
#include <string.h>
memcpy(b, a, sizeof(a));

The reason b = a; fails: the array name is not an assignable value, it decays to a pointer to the first element. So b = a would be trying to assign one pointer to another, which doesn't copy any data anyway. Both loop and memcpy copy the values element by element.

Q17. How do you pass an array to a function in C?

Answer: Pass the array name — it decays to a pointer to the first element. Also pass the size, because the function can't determine it.

void print_all(int a[], int n) {   /* int *a also works */
    for (int i = 0; i < n; i++) {
        printf("%d\n", a[i]);
    }
}

int main(void) {
    int data[5] = {10, 20, 30, 40, 50};
    print_all(data, 5);
    return 0;
}

Inside print_all, a is a pointer — sizeof(a) gives the pointer size, not the array size. That's why the length must be a second parameter. Any modification to a[i] inside the function changes the caller's array, since the pointer points to the same memory.

Q18. What is a variable-length array (VLA) in C?

Answer: An array whose size is decided at runtime (C99 feature) — still lives on the stack and disappears when the function returns.

void process(int n) {
    int data[n];   /* VLA — size decided at call time */
    for (int i = 0; i < n; i++) data[i] = i;
    /* ... */
}

VLAs are useful when the size depends on input and you don't want to allocate on the heap. But they have real costs:

  • Stack size is limited (typically 1 MB or a few MB) — a huge VLA can overflow it and crash.
  • They were optional in C11 and removed in some embedded compilers.
  • They can't be initialized with an initializer list.

For anything sizeable or uncertain, prefer malloc on the heap.

Q19. Are all array elements initialized to zero by default?

Answer: Only if the array is declared outside any function, or explicitly initialized to zero.

The rules:

  • Global or static arrays — all elements start at 0 automatically.
  • Local arrays — elements are uninitialized, containing whatever was in that memory before. Reading them is undefined behaviour.

To zero-initialize a local array, use an initializer with a single 0 or use memset:

int a[100] = {0};                        /* all zeros        */
#include <string.h>
int b[100];
memset(b, 0, sizeof(b));                 /* all zeros        */

Q20. Can an array hold elements of different data types?

Answer: No — an array holds one type. If you need mixed types, use an array of structs, or an array of void * pointers to separate objects.

Every element of an array has the same type. To hold mixed data, the standard C approach is an array of structs:

struct Student {
    char name[20];
    int  age;
    float marks;
};
struct Student class[30];   /* each element has name, age, marks */

The struct groups related fields of different types into one unit. An array of these structs is how you'd model a list of student records. An array of void * is a lower-level option — it can point to anything — but you lose type safety and often regret it later.

Frequently Asked Questions

Are arrays zero-indexed in C?

Yes — the first element is at index 0, the last is at index size-1. For int a[5], valid indexes are 0, 1, 2, 3, 4. Index 5 is out of bounds. This is why loop bounds use i < n rather than i <= n.

Can arrays hold different sizes of elements?

No — every element is the same type, so every element is the same size. Arrays of structs do group different fields into a fixed-size element, but each element is still that struct type, all the same size.

Why is accessing an array element so fast?

Because arrays sit in contiguous memory. The address of element a[i] is simply base_address + i * sizeof(element) — one multiplication and one addition, regardless of size. No searching, no walk-down-a-list. That constant-time access is the main reason arrays are the go-to tool for indexed data.

What's the maximum size of an array in C?

It depends on where the array lives and how much memory the system has. A local array is limited by the stack size (often 1–8 MB). A global array or one allocated with malloc is limited by available RAM. The C standard doesn't set a fixed maximum — just check sizeof and the system limits when sizing arrays carefully.

Should I use arrays or linked lists?

Arrays when you need fast indexed access, a known size, or a compact block of memory. Linked lists when you need frequent inserts or deletes and don't need random access. Arrays win on speed and simplicity for most day-to-day work; linked lists win when the data structure changes shape a lot.

💡 Tip: Array questions in interviews come down to four things: knowing the declaration syntax, understanding that indexes run 0 to size-1, being able to explain why an array name is not the same as a pointer (but behaves like one), and knowing that out-of-bounds access is undefined behaviour. If you can explain those four fluently with small examples, you're set for any array question.

📖 Related FAQ & MCQ Pages

  • Introduction to C FAQ
  • Operators in C FAQ
  • Conditional Statements FAQ
  • Loops in C FAQ
  • Functions in C FAQ
  • Structures in C FAQ
  • Pointers in C FAQ
  • Files in C FAQ
  • Storage Classes FAQ
  • Dynamic Memory FAQ

Previous Topic: -->> Loops in C FAQ   ||   Next topic: -->> Functions in C FAQ


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • Top 40 MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.