All Topics  
C string

 

   Email Print
   Bookmark   Link






 

C string



 
 
In computing, a C string is a character sequence stored as a one-dimensional character
Character (computing)

In computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written language form of a natural language....
 array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
 and terminated with a null character
Null character

The null character is a character with the value zero, present in the ASCII and Unicode character sets, and available in nearly all mainstream programming languages....
 ('\0', called NUL in ASCII
ASCII

American Standard Code for Information Interchange , is a coding standard that can be used for interchanging information, if the information is expressed mainly by the written form of English words....
). The name refers to the ubiquitous C programming language
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 which uses this string representation
String (computer science)

In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet....
. 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
Full Discussion Forum



Encyclopedia


In computing, a C string is a character sequence stored as a one-dimensional character
Character (computing)

In computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written language form of a natural language....
 array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
 and terminated with a null character
Null character

The null character is a character with the value zero, present in the ASCII and Unicode character sets, and available in nearly all mainstream programming languages....
 ('\0', called NUL in ASCII
ASCII

American Standard Code for Information Interchange , is a coding standard that can be used for interchanging information, if the information is expressed mainly by the written form of English words....
). The name refers to the ubiquitous C programming language
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 which uses this string representation
String (computer science)

In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet....
. 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
Strlen

In the C standard library, strlen is a String functions that determines the length of a character string....
, which counts the number of characters, starting at the pointer's memory location, before a null character is reached.

In the C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
, C strings are used in addition to another representation of character sequences, the stdstring
String (C++)

In the C++ programming language, the std::string class is a standard representation for a string of text. This class removes many of the problems introduced by C string by putting the onus of memory ownership on the string class rather than on the programmer....
container found in the Standard Template Library
Standard Template Library

The Standard Template Library is a Library partially included in the C++ C++ standard library. It provides Container s, iterators, algorithms, and Function objects....
 (STL). The Microsoft Foundation Class Library
Microsoft Foundation Class Library

The Microsoft Foundation Class Library is a Library that wrapper portions of the Windows API in C++ Class , including functionality that enables them to use a default application framework....
 (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
Computer insecurity

Many current computer systems have only limited security precautions in place. This computer insecurity article describes the current battlefield of computer security exploit s and defenses....
 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
Buffer overflow

In computer security and computer programming, a buffer overflow, or buffer overrun, is an Anomaly in software condition where a process attempts to store data beyond the boundaries of a fixed-length buffer ....
.

C String header


The C standard library
C standard library

The C standard library consists of a set of sections of the ISO C standard which describe a collection of header files and library routines used to implement common operations, such as input/output and character string handling, in the C ....
 named string.h
String.h

string.h is the header in the C standard library for the C programming language which contains Macro definitions, constants, and declarations of functions and types used not only for String handling but also various memory handling functions; the name is thus something of a misnomer....
( 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
Strcpy

The C offers a C standard library called strcpy, defined in the string.h header file, that allows null-terminated memory blocks to be copied from one location to another....
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
Strcpy

The C offers a C standard library called strcpy, defined in the string.h header file, that allows null-terminated memory blocks to be copied from one location to another....
Copy string
strncpy Copy n number characters from string
Concatenation
strcat
Strcat

In computing, the C offers a C standard library called strcat that allows one memory block to be appended to another memory block. Both memory blocks are required to be null-terminated....
Concatenate strings
strncat Append n number of characters from string
Comparison
memcmp Compare two blocks of memory
strcmp
Strcmp

In POSIX and in the programming language C , strcmp is a Subroutine in the C standard library that compares two C strings.The prototype according ISO/IEC 9899:1999, 7.21.4.2...
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
Strlen

In the C standard library, strlen is a String functions that determines the length of a character string....
Get string length


Trivia

C strings are exactly equivalent to the strings created by the .ASCIZ directive implemented by the PDP-11
PDP-11

The PDP-11 was a series of 16-bit minicomputers sold by Digital Equipment Corporation from 1970 into the 1990s. Though not explicitly conceived as successor to DEC's PDP-8 computer in the Programmed Data Processor series of computers , the PDP-11 replaced the PDP-8 in many Real-time computing....
 and VAX
VAX

VAX was an instruction set architecture developed by Digital Equipment Corporation in the mid-1970s. A 32-bit complex instruction set computer ISA, it was designed to extend or replace DEC's various Programmed Data Processor ISAs....
 macroassembly languages
Assembly language

An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture....
.

See also

  • character string
  • strcpy
    Strcpy

    The C offers a C standard library called strcpy, defined in the string.h header file, that allows null-terminated memory blocks to be copied from one location to another....
  • strcat
    Strcat

    In computing, the C offers a C standard library called strcat that allows one memory block to be appended to another memory block. Both memory blocks are required to be null-terminated....
  • strlen
    Strlen

    In the C standard library, strlen is a String functions that determines the length of a character string....
  • strlcpy
    Strlcpy

    The strlcpy function, developed by Todd C. Miller and Theo de Raadt for use in the C , is intended to replace the function strcpy and provide a simpler and more robust interface than strncpy....