Search

6.6 — C-style strings

In lesson 4.4b -- An introduction to std::string, we defined a string as a collection of sequential characters, such as “Hello, world!”. Strings are the primary way in which we work with text in C++, and std::string makes working with strings in C++ easy.

Modern C++ supports two different types of strings: std::string (as part of the standard library), and C-style strings (natively, as inherited from the C language). It turns out that std::string is implemented using C-style strings. In this lesson, we’ll take a closer look at C-style strings.

C-style strings

A C-style string is simply an array of characters that uses a null terminator. A null terminator is a special character (‘\0’, ascii code 0) used to indicate the end of the string. More generically, A C-style string is called a null-terminated string.

To define a C-style string, simply declare a char array and initialize it with a string literal:

Although “string” only has 6 letters, C++ automatically adds a null terminator to the end of the string for us (we don’t need to include it ourselves). Consequently, myString is actually an array of length 7!

We can see the evidence of this in the following program, which prints out the length of the string, and then the ASCII values of all of the characters:

This produces the result:

string has 7 characters.
115 116 114 105 110 103 0

That 0 is the ASCII code of the null terminator that has been appended to the end of the string.

When declaring strings in this manner, it is a good idea to use [] and let the compiler calculate the length of the array. That way if you change the string later, you won’t have to manually adjust the array length.

One important point to note is that C-style strings follow all the same rules as arrays. This means you can initialize the string upon creation, but you can not assign values to it using the assignment operator after that!

Since C-style strings are arrays, you can use the [] operator to change individual characters in the string:

This program prints:

spring

When printing a C-style string, std::cout prints characters until it encounters the null terminator. If you accidentally overwrite the null terminator in a string (e.g. by assigning something to myString[6]), you’ll not only get all the characters in the string, but std::cout will just keep printing everything in adjacent memory slots until it happens to hit a 0!

Note that it’s fine if the array is larger than the string it contains:

In this case, the string “Alex” will be printed, and std::cout will stop at the null terminator. The rest of the characters in the array are ignored.

C-style strings and std::cin

There are many cases where we don’t know in advance how long our string is going to be. For example, consider the problem of writing a program where we need to ask the user to enter their name. How long is their name? We don’t know until they enter it!

In this case, we can declare an array larger than we need:

In the above program, we’ve allocated an array of 255 characters to name, guessing that the user will not enter this many characters. Although this is commonly seen in C/C++ programming, it is poor programming practice, because nothing is stopping the user from entering more than 255 characters (either unintentionally, or maliciously).

The recommended way of reading strings using cin is as follows:

This call to cin.getline() will read up to 254 characters into name (leaving room for the null terminator!). Any excess characters will be discarded. In this way, we guarantee that we will not overflow the array!

Manipulating C-style strings

C++ provides many functions to manipulate C-style strings as part of the <cstring> library. Here are a few of the most useful:

strcpy() allows you to copy a string to another string. More commonly, this is used to assign a value to a string:

However, strcpy() can easily cause array overflows if you’re not careful! In the following program, dest isn’t big enough to hold the entire string, so array overflow results.

Many programmers recommend using strncpy() instead, which allows you to specify the size of the buffer, and ensures overflow doesn’t occur. Unfortunately, strncpy() doesn’t ensure strings are null terminated, which still leaves plenty of room for array overflow.

In C++11, strcpy_s() is preferred, which adds a new parameter to define the size of the destination. However, not all compilers support this function, and to use it, you have to define __STDC_WANT_LIB_EXT1__ with integer value 1.

Because not all compiler support strcpy_s(), strlcpy() is a popular alternative -- even though it’s non-standard, and thus not included in a lot of compilers. It also has it’s own set of issues. In short, there’s no universally recommended solution here if you need to copy C-style string.

Another useful function is the strlen() function, which returns the length of the C-style string (without the null terminator).

The above example prints:

My name is: Alex
Alex has 4 letters.
Alex has 20 characters in the array.

Note the difference between strlen() and std::size(). strlen() prints the number of characters before the null terminator, whereas std::size (or the sizeof() trick) returns the size of the entire array, regardless of what’s in it.

Other useful functions:
strcat() -- Appends one string to another (dangerous)
strncat() -- Appends one string to another (with buffer length check)
strcmp() -- Compare two strings (returns 0 if equal)
strncmp() -- Compare two strings up to a specific number of characters (returns 0 if equal)

Here’s an example program using some of the concepts in this lesson:

Don’t use C-style strings

It is important to know about C-style strings because they are used in a lot of code. However, now that we’ve explained how they work, we’re going to recommend that you avoid them altogether whenever possible! Unless you have a specific, compelling reason to use C-style strings, use std::string (defined in the <string> header) instead. std::string is easier, safer, and more flexible. In the rare case that you do need to work with fixed buffer sizes and C-style strings (e.g. for memory-limited devices), we’d recommend using a well-tested 3rd party string library designed for the purpose instead.

Rule: Use std::string instead of C-style string

6.7 -- Introduction to pointers
Index
6.5 -- Multidimensional Arrays

158 comments to 6.6 — C-style strings

  • Dimbo1911

    I have a question. How come when you overflow the array (the example with strcpy()), it still prints out the array as it should (I can print out the string in the dest same as in the source)? The length of the dest (strlen) after copying is equal to the actual length of the dest + the length of the string that was copied, but the sizeof dest still returns declared length of the string. Is this dependent on the machine and/or the compiler? (Windows 7x64 and Code::Blocks 17.12) Does the compiler allocate the array dynamically? Also, what happens with the strlen, why is it the original length + lenght of the copied string, not just the length of the copied string OR the original length? Thank you for your time

    • @std::strcpy doesn't check bounds, it copies as much as you tell it to. If you tell it to write to memory you're not supposed to access, behavior is undefined.
      @sizeof returns the length you declared the array with, it can't change at run-time.
      @std::strlen loops through the string until it finds the 0-terminator. Again, without bounds checks, if it accesses memory after the reserved length, behavior is undefined.

      • Dimbo1911

        Oh, ok, so if I got it right, it is a matter of pure luck if the program will output what we gave it if we write out of bounds? Like, if some other program overwrites the memory we accesed by going out of bounds, and wrote over the /0 terminator, the program will keep outputing random garbage, untill it, by chance, finds first /0 terminator?
        Thanks for the answer :)

        • > some other program overwrites the memory we accesed
          That can't happen, at least not on the major OSs. But other variables or code in your program could conflict with the memory you're trying to access.
          @std::cout prints everything until it finds the 0-terminator, or your program crashes, because it's accessing memory which it's not allowed to access, whichever comes first.

  • oliver

    When I try to convert std::size(array) into an integer type, vb gives me an error and says that in order to convert from 'size_t' to' int' I need to do a narrowing conversion. However in your code you did not have to do a narrowing conversion. I changed my c++ version to c++17, and also tried changing it to the latest update, but the error message still came up. Could anyone please explain this to me? Thanks

    • Use a @static_cast.
      Narrowing conversions usually cause a warning, you probably set your compiler up to treat warnings as errors (This is a good thing).

  • TheBeginer

    Hello, can you please help me with this code:

    The problem is, when I read the string from the file, it repeats a character at the end.
    e.g. If I store "welcome" in the file, it prints "welcomee" on the screen although the file contains the string "welcome".

    • * Line 8, 9, 12, 22: Initialize your variables with brace initializers.
      * Line 23: Booleans are false/true.
      * Line 14-19: Should be a for-loop.
      * Don't use "using namespace".
      * Missing printed trailing line feed.
      * Line 18: Use ++prefix unless you need postfix++.
      * Magic number: 80. Use @std::size.

      You're not writing a null-terminator to the file.

  • What I wonder now is this... Is there a way to convert a C-string into a std::string and vice versa.... Like this pseudo code:

    I'm not sure if I would ever actually need this, but just in case ;)

  • Alamin Sheikh

    std::
    why its important ??

  • The first line only supports C style string variable as its first argument.
    The second line only supports string variable as its second argument.
    Are the getline functions same in both lines.
    I am confused with it.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]