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

174 comments to 6.6 — C-style strings

  • BP

    Hi,
    I have a quick question. Say I was writing code to play a game of hangman.
    I'm guessing for that game, I will need to compare every single letter of a word.
    How would I do this using std::string?

    Thanks!

    • Use a for-loop. Since you didn't post any code, this is what I suppose you're stuck at

      I don't want to spoil you, so I'm not posting the rest of the code. If you have another question, feel free to ask. If you just want to see the start of 1 possible implementation without trying yourself, have a look here
      https://godbolt.org/z/jB60I6

      • BP

        Hi thanks for responding!

        I am not really coding that game, it was just an example in my mind.
        I was wondering how to look at the single letters in a string without c-style strings.
        And to be honest, I don't get most of your example.

        I'm guessing std::string::size_type is a special type of string?
        and if strSolution is the word in std::string type then what is strSolution.length()? is it just the length of that word?
        Are you jumping letter with ui?
        Is this material explained later in this tutorial?

        Thanks!

        • Chapter 17 is all about `std::string`. (Watch out, it's an old chapter with bad practices).

          `std::string::size_type` is the type used by `std::string` for indexing. It's some unsigned integer type.

          `std::string::length` (same as `std::string::size`) returns the length of the string.
          "Hello" -> 5
          "BP" -> 2

          ui starts at 0 and goes all the way up to the string's length minus 1. For example, given the string "Hello", ui goes 0, 1, 2, 3, 4. (5 is an invalid index).

          `std::string`'s individual characters can be accessed just like those of a C-style string, using `str[index]`.

          • BP

            So since I still need to learn lot's or stuff before chapter 17, I thought I would just for the fun of it try using C-style strings for hangman.

            It is a bit ugly, and spaghetti like, but here is my attempt.
            I hope it is a bit ok...

            Thanks for all your help!

            • The only place where you used a C-style string is line 63, which is only required for the fixed-size array. Other than that your code is already using `std::string`.
              You should be able to understand chapter 17, I don't know why Alex decided to wait until the end.

              Some suggestions:

              - Limit your lines to 80 characters in length for better readability on small displays.
              - Line 19+: I don't think char extraction can fail.
              - Line 69: That `false` doesn't do anything. The entire array is initialized to `false` by using empty curly brackets.
              - Line 98 has no effect. `lettersGuessed` isn't used after here.
              - You could remove the "word"/"letter" test by always reading a string and checking its length. If it's 1, you run the letter code. If it's > 1, you run the word code.

              • BP

                How would I check the length of a array if it's size should be fixed?
                It doesn't matter what I put in the machine the size is fixed.
                I guess this can be solved by using non fixed arrays?
                I'll probably see those in a few chapters.
                Thanks again for your pointers!

              • Alex

                The core lessons of this tutorial are focused on broad understanding. Originally, the lessons at the end were going to be a deeper dive into some of the more common classes of the standard library -- but I didn't get far in that approach.

  • SEEMA DHONDIBHAU NARAD

    #include <iostream>
    int main()
    {
        char name[2]; // declare array large enough to hold 255 characters
        std::cout << "Enter your name: ";//mangesh
        std::cin.getline(name, 3);
        std::cout << "You entered: " << name << '\n';

        return 0;
    }

  • Stani

    Thank you so much for this wonderful tutorial! I finally feel I can learn c++!
    My question is the following: I wanted to change the value of one element of the string as:
    char ss[] = "feast";
    ss[0] = "b";
    std::cout << ss;

    which gives me the error: invalid conversion from 'const char*' to 'char' [-fpermissive]|

    but when I switch it to:
    ss[0] = 'b';

    everything compiles fine. Why?

  • Alireza

    Hello and good morning,

    why does an array outputs its address when we're using array(itself, without braces), and @string which is C-style string does not ?!
    Skim below to understand.

  • 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]