🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #47

C Arrays


Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].

To insert values to it, use a comma-separated list inside curly braces, and make sure all values are of the same data type:

int myNumbers[] = {25,
50, 75, 100};

We have now created a variable that holds an array of four integers.


Access the Elements of an Array

To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

This statement accesses the value of the first element [0] in myNumbers:

Example

  int myNumbers[] = {25, 50, 75, 100};
  printf("%d", myNumbers[0]);

// Outputs 25

Change an Array Element

To change the value of a specific element, refer to the index number:

Example

myNumbers[0] = 33;

Example

  int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

  printf("%d", myNumbers[0]);

  // Now outputs 33 instead of 25

Set Array Size

Another common way to create arrays, is to specify the size of the array, and add elements later:

Example

  // Declare an array of four integers:
int myNumbers[4];

// Add
  elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
  myNumbers[3] = 100;

Using this method, you should know the number of array elements in advance, in order for the program to store enough memory.

You are not able to change the size of the array after creation.


Avoid Mixing Data Types

It is important to note that all elements in an array must be of the same data type.

This means you cannot mix different types of values, like integers and floating point numbers, in the same array:

Example

int myArray[] = {25, 50, 75,
3.15, 5.99};

In the example above, the values 3.15 and 5.99 will be truncated to 3 and 5. In some cases it might also result in an error, so it is important to always make sure that the elements in the array are of the same type.

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Arrays – FAQs

Quick answers about learning C Arrays in C.

This free note from CodingNow 2.0 explains C Arrays in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Arrays, is 100% free with no signup required.
With focused practice, most students grasp C Arrays in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now