2.12. Strings

There is no separate string type. A string is an array of characters.

The end of the string is indicated by a null character '\0' in the array. Because of the null character at the end, strings are said to be null-terminated.


String constants

A string constant, such as "abc", is an initialized array of four bytes: 'a', 'b', 'c' and '\0'.

Constant "abc" has type const char*. You can't change it.


String library

The following are available if you include <stdio.h>.

strlen(s)

This returns the length of null-terminated string s, not counting the null character.

strncpy(dest, src, n)

Copy null-terminated string src into array dest. At most n bytes are copied from src. You must ensure that array dest has at least n bytes. If src does not have a null character in its first n bytes, then dest will not be null-terminated.

strncat(dest, src, n)

Concatenate src onto the end of dest. No more than n characters are copied from s, plus a null character at the end, so array dest must have at least strlen(dest) + n + 1 bytes available.

strcmp(s, t)

This compares null-terminated string s and t. strcmp(s, t) is Alphabetical ordering is according to ASCII codes.