Jump to: navigation, search

Introduction

A string in C is a contiguous sequence of characters in memory, terminated by and including a null character. The characters in strings are accessed through the char data type. There are also wide strings whose characters are accessed through wchar_t objects.

Strings are considered as arrays, but may be stored in a single char object, a string literal, a declared array, or in space allocated by malloc, calloc or realloc.

Depending on the execution character set of the C implementation, strings may contain multibyte characters. Examples of character sets that include multibyte characters include UTF-8 (Unicode), GB2312 (Simplified Chinese), Big5 (Traditional Chinese), Shift-JIS (Japanese) and EUC-KR (Korean). Modern distributions of Linux typically default to UTF-8 as the execution character set for C programs.

Example

/* The following three lines each define a string containing only a null character */

char *a    = "";   /*  a points to an empty string */
char  b[1] = {0};  /*  b is an array of 1 char, containing an empty string */
char  c    = 0;    /* &c points to an empty string */

References

The C Standard, 7.1.1 (C99 numbering)

Personal tools