Loading, please wait...

Characters and Strings

We introduce the C Standard Library functions that facilitate string and character processing. The functions enable programs to process characters, strings, lines of text and blocks of memory.

 

This section discusses the techniques used to develop editors, word processors, page layout software, computerized typesetting systems and other kinds of text-processing software. The text manipulations performed by formatted input/output functions like printf and scanf can be implemented using the functions discussed here.

 

Fundamentals of Strings and Characters

Characters are the fundamental building blocks of source programs. Every program is composed of a sequence of characters that when grouped together meaningfully is interpreted by the computer as a series of instructions used to accomplish a task. A program may contain character constants.

 

A character constant is an int value represented as a character in single quotes. The value of a character constant is the integer value of the character in the machine’s character set. For example, 'z' represents the integer value of z, and '\n' the integer value of newline.

 

A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals, or string constants, in C are written in double quotation marks as follows:

“Vaishnavi Sharma” (a name)

“29 Maruti Estate Elite Road” (a street address)

“Taj City, Uttar Pradesh” (a city and state)

“(201) 555-1212” (a telephone number)

A string in C is an array of characters ending in the null character ('\0'). A string is accessed via a pointer to the first character in the string. The value of a string is the address of its first character.

 

Thus, in C, it is appropriate to say that a string is a pointer—in fact, a pointer to the string’s first character. In this sense, strings are like arrays, because an array is also a pointer to its first element.

A character array or a variable of type char * can be initialized with a string in a definition.

The definitions

char color[] = "blue";

const char *colorPtr = "blue";

each initializes a variable to the string "blue". The first definition creates a 5-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'. The second definition creates pointer variable colorPtr that points to the string "blue" somewhere in memory.

 

The preceding array definition could also have been written

char color[] = { 'b', 'l', 'u', 'e', '\0' };

When defining a character array to contain a string, the array must be large enough to store the string and its terminating null character. The preceding definition automatically determines the size of the array based on the number of initializers in the initializer list.

 

A string can be stored in an array using scanf. For example, the following statement stores a string in character array word[20]:

scanf( "%s", word );