Javatpoint Logo
Javatpoint Logo

Difference Between Array and String in C

Arrays and strings are two extensively used data types in the C programming language, although they have significant differences in their features, usefulness, and applications. In this post, we will look at how arrays and strings differ in the C programming language.

Definition and Properties

An array is a collection of pieces of the same data type that are stored in memory in contiguous locations. An array's elements can be retrieved using an index spanning from 0 to the array's size minus one. Characters, integers, floating-point numbers, and structures can all be stored in arrays. When we declare an array in C, we must define its data type, size, and, if desired, the starting values of its entries.

A string is a series of characters that ends with a null character ('0'). Strings are represented in C as arrays of characters, with the final character being a null character. Strings can be defined and handled using the C library's string handling functions, such as strlen(), strcat(), strcpy(), and strcmp(). A string literal in C is surrounded in double quotes (" ") and is terminated by a null character by default.

Strings have the critical virtue of being immutable, which implies that we cannot change the individual characters of a string once it is produced. We can, however, make a new string by concatenating or copying existing ones. Arrays, on the other hand, are changeable, which means we can change the elements of an array using assignment statements or pointer arithmetic.

Size and Memory Allocation

One significant distinction between arrays and strings is that the size of an array is fixed at the moment of declaration and cannot be changed during programme execution, whereas the size of a string can fluctuate based on the number of characters it contains. An array of five integers, for example, has a fixed size of 20 bytes (assuming each integer is four bytes), whereas a string of five characters has a fixed size of six bytes (five characters plus the null character).

When declaring an array in C, we must indicate its size explicitly by providing a constant value or using an initialized variable. For example, we may define an integer array this way:

In C, on the other hand, we may skip the size and let the compiler allocate the appropriate memory automatically. For instance, we could declare a string as follows:

The compiler allocates six bytes of memory in this example for keeping the string "hello" (five characters plus the null character).

Accessing and Manipulating Elements

Individual elements in an array can be accessed and manipulated using their indexes. An array's first element has an index of 0, and its last element has an index of size minus one. A for loop can iterate over an array's elements and execute operations on them, such as sorting, searching, and printing. Here's an instance of accessing and updating an array element:

In this example, the third element of the array (with index 2) is changed to 10.

Individual characters in a string can be accessed using their indices, much like in an array. However, because strings are immutable, we cannot directly change their characters. We can instead make a new string by concatenating or copying existing ones. String manipulation routines provided by the C library include strlen(), strcat(), strcpy(), and strcmp().

Here is an example of manipulating a string using the strcpy() function:

In this example, the contents of str2 are copied to str1, overwriting the original contents of str1.

Null Termination

The null termination of strings is a significant difference between arrays and strings in C. A string must always finish with a null character ('0'), which signifies that the string has come to a conclusion. String handling methods would be unable to determine where the string terminates in the absence of a null character, resulting in undefined behaviour or even programme failures.

Arrays, on the other hand, do not require null termination since they can store any type of data, including binary data and non-printable characters. However, in arrays, null termination is occasionally used to indicate the conclusion of a sequence of elements, such as a list of letters or a data stack.

Applications

Depending on their qualities and functions, arrays and strings are employed in various applications in the C programming language. Arrays are helpful for storing a set number of pieces of the same data type, such as a grade list, a number matrix, or a set of flags. Arrays can also be used to build data structures like stacks, queues, and heaps.

Strings, on the other hand, are used to store text and character data like names, addresses, and messages. Strings are commonly used in input/output tasks like reading and writing to files or the terminal. Strings are extensively utilized in network programming and database management, where text is frequently sent or saved.

Here is an example C code that demonstrates the difference between arrays and strings:

C Program:

Output:

The third element of the array is 3
The third character of the string is l
The new value of the third element of the array is 10
The new value of the third character of the string is L
The first element of the copied array is 1
The copied string is heLlo

Explanation:

  • This code demonstrates the difference between arrays and strings in C. In the beginning, we declare an array arr of 5 integers with values 1, 2, 3, 4, and 5, and a string str with the value "hello". We then access and print the third element of the array using arr[2], which has the value 3, and the third character of the string using str[2], which has the value 'l'.
  • Next, we modify the third element of the array using arr[2] = 10, which changes its value from 3 to 10. We also modify the third character of the string using str[2] = 'L', which changes its value from 'l' to 'L'.
  • We then demonstrate how to copy arrays and strings using the memcpy() and strcpy() functions, respectively. We declare a new array arr_copy and use the memcpy() function to copy the contents of arr to arr_copy. We also declare a new string str_copy and use the strcpy() function to copy the contents of str to str_copy. We print the first element of arr_copy, which has the value 1, and the contents of str_copy, which is "helloL". Note that the modified character 'L' is also copied to str_copy.
  • Finally, we return 0 to indicate a successful termination of the program.

Conclusion

In conclusion, arrays and strings are two essential data types in C programming language, with distinct properties, functionalities, and applications. Arrays are collections of elements of the same data type, with a fixed size and mutable contents. Strings are sequences of characters terminated by a null character, with a variable size and immutable contents. Understanding the differences between arrays and strings is crucial for writing efficient and robust C programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA