Programming in C

(Barry) #1
Character Strings, Structures, and Arrays 219

or newlines.The compiler automatically concatenates adjacent strings together.
Therefore, writing the strings


"one" "two" "three"


is syntactically equivalent to writing the single string


"onetwothree"


So, the lettersarray can also be set to the letters of the alphabet by writing


char letters[] =
{ "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" };


Finally, the three printfcalls


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


all pass a singleargument to printfbecause the compiler concatenates the strings
together in the second and third calls.


Character Strings, Structures, and Arrays


You can combine the basic elements of the C programming language to form very pow-
erful programming constructs in many ways. In Chapter 9, “Working with Structures,”
for example, you saw how you could easily define an array of structures. Program 10.9
further illustrates the notion of arrays of structures, combined with the variable-length
character string.
Suppose you want to write a computer program that acts like a dictionary. If you had
such a program, you could use it whenever you came across a word whose meaning was
not clear.You could type the word into the program, and the program could then auto-
matically “look up” the word inside the dictionary and tell you its definition.
If you contemplate developing such a program, one of the first thoughts that comes
to mind is the representation of the word and its definition inside the computer.
Obviously, because the word and its definition are logically related, the notion of a struc-
ture comes immediately to mind.You can define a structure called entry,for example, to
hold the word and its definition:


struct entry
{
char word[15];
char definition[50];
};

Free download pdf