0% found this document useful (0 votes)
10 views1 page

Ict 1

The document explains the use of strings and arrays in programming, detailing how to declare, access, and modify them. It also covers various string functions such as strlen(), strcat(), strcpy(), and strcmp(), which perform specific operations on strings. Additionally, it discusses the importance of escape characters and the sizeof operator for determining array sizes and memory allocation.

Uploaded by

Daren Mendiola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Ict 1

The document explains the use of strings and arrays in programming, detailing how to declare, access, and modify them. It also covers various string functions such as strlen(), strcat(), strcpy(), and strcmp(), which perform specific operations on strings. Additionally, it discusses the importance of escape characters and the sizeof operator for determining array sizes and memory allocation.

Uploaded by

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

String and Array

Oftentimes, the backslash behind the character is needed in order to prevent


Strings are used for storing text characters. Its format specifier is %s confusion in the code, as a majority of the characters that require a backslash
have a preset function and cannot be placed randomly.
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value. To declare/create an array, define the data type
and specify the name of the array followed by square brackets []. Square brackets help
access array elements

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

To access array elements access strings, refer to its index number (Indexes are used to
access elements in an array). Array indexes start with 0; [0] is the first element, [1] String Functions
is the second element, etc.

int myNumbers[] = {25, 50, 75, 100}; There are multiple string functions that perform specific tasks. To use them, you
printf("%d", myNumbers[0]); must include <string.h> header file in your program.
The result should be 25, as it is the first element.

For example, to get the string length, you can use the strlen() function
To change the value of an element modify strings, refer to its index number:

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


myNumbers[0] = 33;
printf("%d", myNumbers[0]);
The result should be 33, as it replaced the previous first element, 25. To get the length of a string or array, you’d use the sizeof function instead. It
behaves differently from strlen() as sizeof counts the \0 character as well, and will
Another way to create an array is to specify its size, and add elements later: also return the memory size and not the actual string length.

// 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 can show the amount of array elements in advance, letting the To concatenate strings, you can use the strcat() function:
program set enough storage for said elements. The size of the array isn’t
changeable after creation.

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

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


printf("%lu", sizeof(myNumbers));
The result should be 16 (reason listed below). To copy strings, you can use the strcpy() function:

The size differs depending on its data type, char is 1, int is 2 or 4, float is 4,
double is 8. So, if there are four array elements in an array, you multiply that by
the data type of the elements in the array (assuming there aren’t multiple data
types).

But, if you only want the amount of elements in an array:

int myNumbers[] = {25, 50, 75, 100}; To compare strings, you can use the strcmp() function. It returns 0 if the two
int length = sizeof(myNumbers) strings are equal, otherwise, a value that isn’t 0:
/ sizeof(myNumbers[0]);

printf("%d", length);
The result should be 4, as there are four array elements in the given array.

Escape Characters

You might also like