C++ Arrays and Strings

Arrays and Strings in C++

Arrays and strings are two of the most commonly used data types in C++. An array is a collection of related data items that are stored in sequential order, while a string is a sequence of characters.

Let’s look at an example of declaring and using an array:

#include <iostream>

int main()
{
    int numbers[5] = {1, 2, 3, 4, 5};
 
    for (int i = 0; i < 5; i++)
    {
        std::cout << numbers[i] << std::endl;
    }
 
    return 0;
}

Output

1
2
3
4
5

In this example, we have declared an array of five integers and initialized it with the numbers 1 through 5. We have then used a for loop to print out each element of the array.

Leave a Reply