0% found this document useful (0 votes)
13 views3 pages

Arrays

Arrays allow storing multiple values in a single variable. They are declared with a data type and size, and values are accessed by index. Common array operations include accessing elements, changing elements, looping through arrays, setting the array size, and getting the array length. String functions allow operating on strings like getting the length, concatenating, and comparing strings.

Uploaded by

jimmy mlelwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Arrays

Arrays allow storing multiple values in a single variable. They are declared with a data type and size, and values are accessed by index. Common array operations include accessing elements, changing elements, looping through arrays, setting the array size, and getting the array length. String functions allow operating on strings like getting the length, concatenating, and comparing strings.

Uploaded by

jimmy mlelwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

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


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:
Change an Array Element
To change the value of a specific element, refer to the index number:
Loop Through an Array
You can loop through the array elements with the for loop.
The following example outputs all elements in the myNumbers array:

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


int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

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;

Get Array Size or Length


To get the size of an array, you can use the sizeof operator:
Example

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


printf("%d", sizeof(myNumbers)); // Prints 20

Why did the result show 20 instead of 5, when the array contains 5 elements?
- It is because the sizeof operator returns the size of a type in bytes.
You learned from the Data Types that an int type is usually 4 bytes, so from the
example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
Knowing the memory size of an array is great when you are working with larger
programs that require good memory management.
String Functions
C also has many useful string functions, which can be used to perform certain
operations on strings.
To use them, you must include the <string.h> header file in your program:
#include <string.h>
String Length
For example, to get the length of a string, you can use the strlen() function:
Example

char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


printf("%d", strlen(alphabet));

Concatenate Strings
To concatenate (combine) two strings, you can use the strcat() function:
Example

char str1[20] = "Hello ";


char str2[] = "World!";

// Concatenate str2 to str1 (result is stored in str1)


strcat(str1, str2);

// Print str1
printf("%s", str1);

Compare Strings
To compare two strings, you can use the strcmp() function.
It returns 0 if the two strings are equal, otherwise a value that is not 0:
Example

char str1[] = "Hello";


char str2[] = "Hello";
char str3[] = "Hi";

// Compare str1 and str2, and print the result


printf("%d\n", strcmp(str1, str2)); // Returns 0 (the strings are equal)

// Compare str1 and str3, and print the result


printf("%d\n", strcmp(str1, str3)); // Returns -1 (the strings are not equal)

You might also like