In computing, a C string is a character sequence stored as a one-dimensional character array and terminated with a null character ('\0', called NUL in ASCII). The name refers to the ubiquitous C programming language which uses this string representation. Alternative names are ASCIIZ and null-terminated string .

The length of a C string is found by searching for the (first) NUL byte. This takes O(n) (linear) time with respect to the string length, and it also means a string cannot contain the NUL byte. At the time C (and the languages that it was derived from) were developed, memory was extremely limited, and using only one byte of overhead to store the length of a string was imperative. The only popular alternative (often called a "Pascal string") used one byte to store the length. This allowed the string to contain NUL and made finding the length take O(1) (constant) time, but it limited the length to 255 bytes. This length limitation proved to be far more restrictive than the limitations of C strings.

On modern systems the memory usage is less of a concern, and a larger value can be used for the length (if you have vast numbers of short strings a hash table can be used to save memory instead). Most replacements for C strings (such as the C++ std::string container and the Qt string, use a 32-bit or more length. Thus NUL bytes can be placed in the string and finding the length is O(1).

Making a "copy" of a C string with any number of bytes removed from the start can be done by just moving the pointer, an O(1) constant time operation (far faster than any other string representation). Many pieces of software have taken advantage of this, making it difficult to change them to a new string style without serious speed impact.

The NUL termination has historically created security problems. A bug or malicious program can insert a NUL into the middle of a string, truncating it unexpectedly. A common bug was to not write the NUL at the end of a string (often not detected because there was a NUL already there), allowing leakage of program internal information, added to the end of the string. Due to the expense of finding the length, many programs did not bother before copying the string to a fixed-size buffer, causing a buffer overflow.

Many attempts have been made to make C string handling less error prone. These range from adding safer and more useful functions such as strdup and strlcpy , to entire wrappers to treat the string as an opaque object, such as the MFC CString class which internally represents the string as a C string, but does not require the programmer to handle memory allocation issues.

C String header

The C standard library named string.h ( <cstring> header in C++) is used to work with C strings. Confusion or programming errors arise when strings are treated as simple data types. Specific functions have to be employed for comparison and assignment such as strcpy for assignment instead of the standard = and strncmp instead of == for comparison.

Trivia

C strings are equivalent to the strings created by the .ASCIZ directive of the PDP-11 and VAX macroassembly languages and the ASCIZ directive of the MACRO-10 macro assembly language for the PDP-10.

See also

  • character string
  • strcpy
  • strcat
  • strlen
  • strlcpy

References


http:

C library character and string function problems

If you encounter problems at runtime with the C library character handling functions from ctype.h, e.g. isdigit(), toupper(), tolower() or string conversion functions from stdlib.h ...

...

string - C++ Reference

Unlike traditional c-strings, which are mere sequences of characters in ... of the basic_string class template, defined in as: typedef basic_string < char > string; Member functions

...

C string replace function

Below is a quick character array or 'string' replacement function. Usage as below: char *new = str_replace(old, "foo", "bar"); char * str_replace(char *str ...

...

String Functions in C Locale - GNU Gnulib

10.4 Character and String Functions in C Locale. The functions in this section are similar to the generic string functions from the standard C library, except that

...

cstring (string.h) - C++ Reference

This header file defines several functions to manipulate C strings and arrays. Functions Copying:

...

Comment on trim string function please - Dev Archives

Comment on trim string function please- C/C++. Visit Dev Archives to discuss Comment on trim string function please

...

A Little C Primer/The C sprintf Function - Wikibooks, collection of ...

There is also an "sscanf()" function that similarly mirrors "scanf()" functionality. [edit] C STRING FUNCTION LIBRARY. moved to A Little C Primer/C String Function Library

...

C string functions

C string functions #include (for C++ and namespace std, #include ) *note: size_t is generally defined as an unsigned short.

...

A Mid String Function for C and C+: Replicating one of the most useful ...

There are three ways to obtain a nice Mid-like function, depending on the language and portability choices the programmer has made. C, C++ and C++ STD.

...

A Little C Primer/C String Function Library - Wikibooks, collection of ...

The most important string functions are as follows: strlen() Get length of a string. strcpy() Copy one string to another. strcat() Link together (concatenate) two ...

...