The type of string used in the C programming language and most languages like it. This type of string is just a general concept and is not really hardwired into the language (although it is what the standard library expects in its input and output functions, and the double-quote operators use it-- but more on that later) but rather based simply on convention. It is named "C string" because it was first used in C, but it doesn't necessarily have to be a C thing. Like most things related to C, the concepts behind it give you an extremely high degree of flexibility as well as an environment where you can bugger things up rather horribly.

A C string (in any language) is stored in memory as a series of bytes representing characters (in C, this is a char array). The characters to be used begin at the first address possible (in C, address zero) and continue indefinitely. The end of the string is marked by a null terminator, a byte set equal to zero. (Null chars should be referred to in C as '\0'.) So when printf or cout is passed a string, it simply bangs everything out character by character until it reaches a null character, and then it stops.

If you are screwing with strings by hand in C, you need to be very aware of that null character, and make sure you don't accidentally leave it out. You also need to be aware that a four-letter string actually contains five characters because of the '\0'.

This is, by the way, the only difference in C between single quotes and double quotes when giving a series of chars; namely, the single chars are the literal, raw values of the chars, and the double quotes actually create a full C string with a null terminator. Thus 'k' is a char, whereas "k" is a 2-char array containing a k and a \0.

See also Pascal string

I beg to differ. You imply that C-style strings aren't built into the language of C, yet they absolutely are. Ever write a string literal in a C program?

That is: char * mystring = "This is a string literal.";

That string literal gets compiled into your binary as a series of 25 bytes, the last of which most definitely contains the null terminator byte.

The reason we can say that C-style strings are inherently built into the C language is the presence of the NUL character that implicitly follows a string literal. That is a C convention (other languages do it too, you are right) but we do say the notion of a C-style string is inherent in the language. Also, if you read the K & R C book, they definitely acknowledge that C-style strings are an inherent 'feature' of the C language.

Log in or registerto write something here or to contact authors.