Monday, April 28, 2025

C Tutorial - 13 - Arrays

What are Arrays in C programming 

In C programming, an array is a collection of elements of the same data type, which are stored in contiguous memory locations. An array is a data structure that allows you to store multiple values of the same data type in a single variable.

Arrays are declared using the following syntax:

data_type array_name[array_size];

Here, data_type is the type of data that the array will store, array_name is the name of the array, and array_size is the number of elements that the array will store. For example, to declare an array of integers with 10 elements, you would use the following code:

int my_array[10];

Once an array is declared, you can access its individual elements by their index, which is a zero-based integer value that represents the position of the element in the array. For example, to access the first element of the my_array array, you would use the following code:

int first_element = my_array[0];

You can also assign values to array elements using the index notation, like this:

my_array[0] = 42;
my_array[1] = 21;

Arrays are a powerful tool for working with collections of data in C programming, and are used extensively in many applications.

 

C Array Example

#include <stdio.h>

int main()
{

    int someNumbers[] = {5, 25, 100, 500};
    char someChars[] = {'A', 'B', 'C'};

    printf("Number : %d \n", someNumbers[0]);
    printf("Number : %d \n", someNumbers[1]);
    printf("Number : %d \n", someNumbers[3]);
    printf("Char : %c \n", someChars[0]);

    someNumbers[0] = 888;
    printf("Number : %d \n", someNumbers[0]);

    return 0;
}

Result:

Number : 5
Number : 25
Number : 500
Char : A
Number : 888

Process returned 0 (0x0)   execution time : 0.011 s
Press any key to continue.

This C source code demonstrates the usage of arrays in C programming.

The #include <stdio.h> line includes the standard input/output library, which contains the printf() function used for printing output to the console.

The int main() function is the entry point of the program, which returns an integer value to the operating system indicating whether the program executed successfully or not.

The program then declares two arrays: someNumbers and someChars. someNumbers is an array of integers initialized with the values 5, 25, 100, and 500, and someChars is an array of characters initialized with the values 'A', 'B', and 'C'.

The program then uses the printf() function to print the values of the array elements. The first three lines of code print the first, second, and fourth elements of someNumbers using the format string "Number : %d \n", which prints an integer value followed by a newline character. The fourth line of code prints the first element of someChars using the format string "Char : %c \n", which prints a single character followed by a newline character.

The program then modifies the value of the first element of someNumbers by assigning the value 888 to it using the assignment operator =. The final printf() call prints the new value of the first element of someNumbers.

Finally, the return 0; statement indicates that the program executed successfully, and returns the value 0 to the operating system.

 

C Array Elements and For Loop

#include <stdio.h>

int main()
{

    int someNumbers[] = {5, 25, 100, 500};
    char someChars[] = {'A', 'B', 'C'};

    int x = 0;

    for (x; x < 4; x++)
    {
        printf("Number : %d \n", someNumbers[x]);
    }

    return 0;
}

Result:

Number : 5
Number : 25
Number : 100
Number : 500

Process returned 0 (0x0)   execution time : 0.004 s
Press any key to continue.

This C source code demonstrates the usage of a for loop to iterate over the elements of an array and print their values to the console.

The #include <stdio.h> line includes the standard input/output library, which contains the printf() function used for printing output to the console.

The int main() function is the entry point of the program, which returns an integer value to the operating system indicating whether the program executed successfully or not.

The program then declares two arrays: someNumbers and someChars. someNumbers is an array of integers initialized with the values 5, 25, 100, and 500, and someChars is an array of characters initialized with the values 'A', 'B', and 'C'.

The program then declares an integer variable x and initializes it to 0.

The program then enters a for loop, which iterates over the elements of someNumbers using the loop variable x. The loop runs as long as x is less than 4, which is the number of elements in someNumbers. The loop increments x by 1 after each iteration.

Within the loop, the program uses the printf() function to print the value of each element of someNumbers using the format string "Number : %d \n", which prints an integer value followed by a newline character.

After the loop completes, the program reaches the return 0; statement, which indicates that the program executed successfully, and returns the value 0 to the operating system.

This program demonstrates how to use a for loop to iterate over the elements of an array and perform a task on each element, such as printing its value to the console.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...