Strings in C

C doesn’t have a string data type per se; instead, C uses an array of char elements to store a string.

A string constant is a set of characters placed between a pair of double quotes. Anything between double quotes is taken by the compiler to be a string.

#include <stdio.h>

int main(void){
    printf("This is a string. ");
    //newline character
    printf("As is this. \nBut this is on the second line.");
    //tab character
    printf("\nAnd this is \t on the \t third.");
    return 0;

Strings stored in memory end with the null character, . As strings in C are always terminated by a null character, the length of a string is always the number of characters, plus one. Strings are stored in arrays of type char.

Remember,the size of the char array must be large enough to hold the string of characters, plus one. We can also leave the array’s brackets empty and initialize the char array with the desired string, to have the size automatically set.

 #include <stdio.h>

int main(void){
    int i = 0;
    char strA[] = "I propose to move immediately upon your works.";
        //check if we have reached the null
        //termination characater
        if(strA[i] == ''){
        } else {
            //output the character
            //and increment the counter
            printf("%c ", strA[i++]);
    return 0;

The %s specification is used to output a null-terminated string. At wherever the %s appears in the format string argument, an output function will output successive characters from the char array until it hits the character.

#include <stdio.h>

int main(void){
    char str01[28] = "Nice boat!";
    char str02[128] = "It is better to die for the Emperor than live for yourself!";
    char str03[] = "My lab members are my allies, and the World is my enemy!";
    printf("String #1: %s\n", str01);
    printf("String #2: %s\n", str02);
    printf("String #3: %s\n", str03);
    return 0;

The main drawback to using mutable char arrays to hold a variety of strings is that you must make the char array long enough to hold the longest string that is expected. As arrays are, by definition, fixed length, we will probably end up wasting storage space much of the time.

Joining one string to the end of another is a often required; this is called appending a string.

#include <stdio.h>

int main(void){
    char strA[] = "Shaka, when the walls fell.";
    char strB[] = "Mirab, with sails unfurled.";
    int i, j;
    int length = sizeof(strA) + sizeof(strB);
    char strC[length];
    //copy first string
    i = 0;
        strC[i] = strA[i];
    //insert a space 
    //to go between the two strings.
    strC[i] = ' '; 
    //copy second string
    j = i + 1;
    i = 0;
      strC[j++] = strB[i++];
    strC[j] = '';
    printf("strA = %s\n", strA);
    printf("strB = %s\n", strB);
    printf("strC = %s\n", strC);
    return 0;

The sizeof() operator returns the number of bytes in the string, including the byte allocated to store the character. This gives us some padding – we can replace the character in the first string to a blank space character before we append the second string to it within the new string.

In the real world, however, we would want to use the strlen() function rather than sizeof(). Unlike sizeof(), the strlen() function does not count the null termination character in the string.

Why should we use strlen(), then? Well, for one reason, we ought to recall here that a string in C is nothing more than a char array, and when we pass an array to a function, it is passed by reference, which in the C context means that the called function has a copy of a pointer to the array, and not the array itself. This means that if we use the sizeof() operator on a string parameter, we will get back the size of a pointer on our system, and not the number of bytes stored in the string!

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

//function returns a pointer to a char
char * appendStr(char *strA, char *strB){
    int i, j;
    //sizeof wouldn't work here
    printf("Size of stringA[] in function: %d\n", sizeof(strA));
    printf("whereas strlen(stringA) gives us: %d\n", strlen(strA));
    //---let's us pointer arithmetic for fun
    char * returnChar = (char *)malloc(strlen(strA) + strlen(strB) + 2);

    i = 0;
    while(*(strA + i) != ''){
        *(returnChar + i) = *(strA + i);
    //put a space between the two
    *(returnChar + i) = ' ';
    //now  let's copy over the second string
    //we can used array index notation as well
    j = ++i;
    i = 0;
        returnChar[j++] = strB[i++];
    returnChar[j] = '';
    //since we are returning a value 
    //allocated on the heap
    //we will have to call free() in the calling function
    return returnChar;
} // end function --------------------

int main(void){
    char stringA[] = "Hi.";
    char stringB[] = "Nice boat!";
    char *buffer;
    printf("sizeof(stringA) = %d\n", sizeof(stringA));
    printf("strlen(stringA) = %d\n", strlen(stringA));
    //don't forget that an array name devolves to a pointer
    //when passed as an argument to a function
    buffer = appendStr(stringA, stringB);
    printf("\n%s\n", buffer);
    //don't forget to clean up!
    return 0;

Note that the strlen() function is declared in the string.h header file.

We can use a two-dimensional array of type char to store strings, where each row is used to hold a separate string. Thus, in a char array stringsArray[i][j], at any given point the i index will indicate which string we are using, and the j index will indicate which character in that string we are at.

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

int main(void){
    char stringArray[3][56] = {
                    "Valar morghulis",
                    "Don't Panic",
                    "Photons be free!"};
    char stringArrayTwo[][128] = {
                    "Nilbog is Goblin Spelled Backwards!",
                    "The morning sun has vanquished the horrible night.",
                    "You are technically correct -- the best kind of correct."
    printf("%s ", stringArray[0]);
    printf("...starts with: %c\n", stringArray[0][0]);
    printf("%s ", stringArrayTwo[1]);
    printf("...starts with: %c\n", stringArrayTwo[1][0]);
    return 0;

Note that when initializing an array of strings, we can omit the first value when initializing the string array, so that the compiler deduces for us the number of strings we are declaring.

I wrote a book on C, check it out:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s