C Declaration and Initialization

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Declaration and Initialization?

Declare an array with its element type and size. Provide an initializer list to set values; missing values are filled with zero. If you omit the size, the compiler counts the initializers. C99 designated initializers set specific indexes.

Remember
int a[5];                    /* uninitialized (locals) */
int b[5] = {1, 2};           /* 1 2 0 0 0   */
int c[] = {3, 4, 5};         /* size 3      */
int d[5] = {[1] = 7, [3] = 9};  /* 0 7 0 9 0 */
int z[100] = {0};            /* all zero    */

Cheat Sheet

Keep these facts handy while you work with Declaration and Initialization in C.

TopicDetail
Partial listRemaining elements become 0
Omit sizeCompiler counts the initializers
Designated[index] = value (C99)
Too many valuesCompile error

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Accessing Array Elements.

Example 1 — Four initialization styles

Print each array to see what the compiler filled in.

C
#include <stdio.h>

static void show(const char *name, const int *a, int n) {
    printf("%s:", name);
    for (int i = 0; i < n; i++) printf(" %d", a[i]);
    printf("\n");
}

int main(void) {
    int b[5] = {1, 2};
    int c[] = {3, 4, 5};
    int d[5] = {[1] = 7, [3] = 9};

    show("b", b, 5);
    show("c", c, 3);
    show("d", d, 5);
    printf("c has %zu elements\n", sizeof c / sizeof c[0]);
    return 0;
}

How It Works

  1. b supplies two values; the rest default to 0.
  2. c has no size, so it gets size 3.
  3. Designated initializers set only indexes 1 and 3.

Edge Cases & Pitfalls

Check these before relying on Declaration and Initialization in real programs.

local

Forgetting to initialize

Uninitialized local arrays hold indeterminate values. Use = {0} for zeros.

size

Using a non-constant size

In C89 the size must be a constant expression. C99 allows VLAs, but they cannot have initializers.

Key Takeaways

  • Braces: = {a, b, c} initializes.
  • Zero fill: unspecified elements are 0.
  • Designators: set sparse arrays clearly.

One line: Declare an array with its element type and size.

Frequently Asked Questions

Write int a[100] = {0};. The first element is 0 and the rest are zero-filled automatically.
Yes, when you provide an initializer. The compiler uses the number of initializers as the size.
Continue with "C Accessing Array Elements" next. Read and write elements with a[i], and see how it relates to pointer arithmetic. The C Arrays hub lists every topic in order.

Next: Accessing Array Elements

Read and write elements with a[i], and see how it relates to pointer arithmetic.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful