C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Arrays and Strings
  5. Initialization of Array

Initialization of Array

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

You can also omit the size:

int a[] = {1, 2, 3};  // size automatically becomes 3

Partial Initialization:

int a[5] = {1, 2};  // Remaining elements will be 0
int b[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

or:

int b[2][3] = {1, 2, 3, 4, 5, 6};  // Same result

If you don’t initialize explicitly:

int arr[5];
  • The content is garbage (undefined), unless declared globally or statically (which will default to zero).

Example Code (Initialization and Access):

#include <stdio.h>

int main() {
    int scores[5] = {90, 85, 78, 92, 88};

    printf("Student Scores:\n");
    for (int i = 0; i < 5; i++) {
        printf("Student %d: %d\n", i + 1, scores[i]);
    }

    return 0;
}

Output:

Student 1: 90
Student 2: 85
Student 3: 78
Student 4: 92
Student 5: 88

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *