|
|
|
|
C string
|
| |
|
| |
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.
In C programs, strings are usually handled with string pointers, which hold the memory location of the first character of the string.

Discussion
Ask a question about 'C string'
Start a new discussion about 'C string'
Answer questions from other users
|
Encyclopedia
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.
In C programs, strings are usually handled with string pointers, which hold the memory location of the first character of the string. The length of the string is not stored, and is instead calculated using strlen, which counts the number of characters, starting at the pointer's memory location, before a null character is reached.
In the C++ programming language, C strings are used in addition to another representation of character sequences, the stdstring container found in the Standard Template Library (STL). The Microsoft Foundation Class Library (MFC) provides its own string class for C++, called a CString, which internally represents the string as a C string, but does not require the programmer to handle memory allocation issues.
The null-termination characteristic has historically created security problems related to the length of the string. If the null character is not correctly accounted for, any following non-related memory area may also be processed as a part of the character sequence. This can lead to program crashes or leakage of program internal information to attackers or non-understanding users. It may also cause a buffer overflow.
C String header The C standard library named string.h ( 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.
Functions included in | Operation | Function | Description | | Copying |
|---|
memcpy | Copies a block of memory | memmove | Move block of memory | strcpy | Copy string | strncpy | Copy n number characters from string | | Concatenation |
|---|
strcat | Concatenate strings | strncat | Append n number of characters from string | | Comparison |
|---|
memcmp | Compare two blocks of memory | strcmp | Compare two strings | strcoll | Compare two strings using locale | strncmp | Compare first n characters of two strings | strxfrm | Transform string using locale | | Searching |
|---|
memchr | Locate character in block of memory | strchr | Locate first occurrence of character in string | strcspn | Get span until character in string | strpbrk | Locate character in string | strrchr | Locate last occurrence of character in string | strspn | Get span of character set in string | strstr | Locate substring | strtok | Split string into tokens | | Other |
|---|
memset | Fill block of memory | strerror | Get pointer to error message string | strlen | Get string length | |
Trivia
C strings are exactly equivalent to the strings created by the .ASCIZ directive implemented by the PDP-11 and VAX macroassembly languages.
See also
|
| |
|
|