C Strings

There are no strings in C. Instead we have arrays of characters.

char c[] = "c string";

The compiler reads the string as a series of characters and copies them one by one in to the array. It appends the null character \0 at the end.

String Array

String Declaration

A string is declared as an array of characters.

char string1[5];
// or
char string2[];

String Initialisation

Strings can be initialised in a number of different ways.

char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};

All result the same

String Initialisation

Note that even though the second example, char c[50] = "abcd";, has a length of 50 the string terminator \0 is still placed in the fifth character.

Assigning Values To Strings

Character arrays cannot have values assigned to them in the usual way.

char c[100];
c = "C programming"; // error

Reading from input

We can use scanf() and gets(), as described on the next page.

strcpy()

The function strcpy() provided by the string library (#include <string.h>) provides a method for copying a string from one location to another.

strcpy() copies the second argument to the first argument. strcopy(destination, source);

#include <string.h>
char string1[50];
strcpy(string1, "Hello World");

There is also strncpy(destination, source, length) which will copy the first length characters from source to destination.

strcat()

Similarly c strings cannot by concatenated by simply using the + symbol. To concatenate two strings we need to use the strcat() function. strcat(destination, source)

#include <stdio.h>
#include <string.h>

int main () {
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");

   strcat(dest, src);

   printf("Final destination string : |%s|", dest);
   
   return(0);
}
Final destination string : |This is destinationThis is source|

We also have strncat(destination, source, length);

String Comparision strcmp()

To see if two strings are the same use strcmp(str1, str2). If the two strings are the same the function will return zero.

String Length strlen()

strlen(string) will return the length of a string. The length of a string is defined as the number of characters before the terminator symbol \0, not the size of the character array.

#include <stdio.h>
#include <string.h>

int main () {
   char string[50] = "Hello\0";

   printf("Length of string : %s is %d", string,strlen(string));
   
   return(0);
}

will produce

Length of string Hello is 5

A Warning About Strings

C trusts the programmer to know what they’re doing. If you string to write a string longer than the size of a character array to that array it will let you do so. While we define the size of a character array, c’s functions only care about the start of the array and will happily keep going as long as you ask them to. The compiler may warn you that you are exceeding the bounds but it will let you do it.

char short_string1[6]="aaaaa";
char short_string2[6]="bbbbb";
strcpy(short_string2,"Hello World");
printf("short string2 is %s\n", short_string2);
printf("short string1 is %s\n", short_string1);

will produce the unusual result

short string2 is Hello World
short string1 is World

Why? Well string1 and string2 were created next to each other in memory. string2 is immeadiately before string1 in memory so when we wrote too much information in to string2 we also overwrote the information in string1. (We will come back to why the two strings are the wrong way round later).
Note also that because we have filled up the entirety of string2 there is no room for a \0 terminator there. So when we printf() string2 we print until we find a \0 which doesn’t happen till the end of string1. printf() doesn’t care how long the character array it is given is, it starts printing at the beginning of the array and keeps going until it finds a \0.

The best plan is to make sure your character arrays are long enough and always ensure they end in a \0.