Programming in C

(Barry) #1

10 Character Strings


10 Character Strings


NOW,YOUARE READY TO TAKEa look at character strings in more detail.You were
first introduced to character strings in Chapter 3, “Compiling and Running Your First
Program,” when you wrote your first C program. In the statement


printf ("Programming in C is fun.\n");


the argument that is passed to the printffunction is the character string


"Programming in C is fun.\n"


The double quotation marks are used to delimit the character string, which can contain
any combinations of letters, numbers, or special characters, other than a double quotation
mark. But as you shall see shortly, it is even possible to include a double quotation mark
inside a character string.
When introduced to the data type char,you learned that a variable that is declared to
be of this type can contain only a singlecharacter.To assign a single character to such a
variable, the character is enclosed within a pair of single quotation marks.Thus, the
assignment


plusSign = '+';


has the effect of assigning the character '+'to the variable plusSign, assuming it has
been appropriately declared. In addition, you learned that there isa distinction made
between the single quotation and double quotation marks, and that if plusSignis
declared to be of type char, then the statement


plusSign = "+";


is incorrect. Be certain you remember that single quotation and double quotation marks
are used to create two different types of constants in C.

Free download pdf