MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Array Initialization

Array Initialization in C

Arrays can be initialized at the time of declaration with values enclosed in braces and separated by commas.

Syntax. data-type array-name[size] = {val1, val2, ..., valn}; where,

val1: Value for the first element, val2: Value for the second element, valn: Value for the nth element.

Examples:

  1. int digits[10] = {1,2,3,4,5,6,7,8,9,10};
  2. int digits[] = {1,2,3,4,5,6,7,8,9,10};
  3. float temperature[10] = {31.2, 22.3, 41.4, 33.2, 23.3, 32.3, 41.1, 10.8, 11.3, 42.3}


Character Array Initialization:

Character arrays are used for strings in C, automatically including a null character \0 at the end.

Examples:

char thing[4] = "TIN"; // Equivalent to {'T', 'I', 'N', '\0'}

char thing[] = "TIN";  // Automatically includes '\0'


Initialization at declaration simplifies assigning values to arrays and handles strings with null termination automatically.

Report an issue

Reporting: Array Initialization (topic)

Related Posts