Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Arrays:

Absolutely! Let's discuss arrays in C using simple language:

What's an Array?

Think of an array like a shelf with compartments. Each compartment can hold one thing, like a book. Similarly, an array in C is like a shelf where you can store multiple pieces of data, like numbers or characters.

Declaring an Array:

To create an array, you need to declare it. It's like telling the computer, "Hey, I want a shelf to store some things." Here's how you do it:

// Syntax: dataType arrayName[arraySize];
int numbers[5]; // This creates an array called 'numbers' with space for 5 integers.

Initializing an Array:

Once you have a shelf (array), you might want to put some things (values) in it. You can do that when you declare the array or later in your code

int numbers[5] = {10, 20, 30, 40, 50}; // Initializing the array with values.

Accessing Elements:

You can access items on the shelf (elements of the array) using their position, which we call an index. Remember, the first compartment is at position 0, the second at position 1, and so on.

int firstNumber = numbers[0]; // This gets the value in the first compartment.
int thirdNumber = numbers[2]; // This gets the value in the third compartment.

Example:

Let's say you have an array called scores to store the scores of five students. You can declare, initialize, and access it like this:

#include <stdio.h>

int main() {
// Declaring and initializing the array
int scores[5] = {85, 90, 75, 88, 92};

// Accessing and printing the values
printf("First student score: %d\n", scores[0]); // First student's score
printf("Third student score: %d\n", scores[2]); // Third student's score

return 0;
}

In this example, scores[0] represents the score of the first student, and scores[2] represents the score of the third student. Remember, the index starts from 0!



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java