Tuesday, March 1, 2011

Printing funky text using C program part 2

Please enter the following source code, DBLQUOTE.C. The resulting program is another “printf() displays something” example. But this time, what’s dis­played contains a double quote. Can you do that? This source code is your experiment for the day:
#include <stdio.h>
int main()
{
printf(“He said, “Ta da! I am a text string.””);
return(0);
}

Type the source code exactly as it appears, including the double quotes — four in all. (You notice right away that something is wrong if your editor color-codes quoted text. But work with me here.)

Save the source code file to disk as DBLQUOTE.C. Compile and run the preceding program — if you can. Chances are that you encounter one of these errors instead:

dblequote.c: In function ‘main’:
dblequote.c:5: parse error before “Ta” or
dblequote.c: In function ‘main’:
dblequote.c:6: syntax error before “Ta”

The printf() function requires a text string enclosed in double quotes. Your compiler knows that. After the second double quote in the string was encoun­tered (before the word Ta), the compiler expected something else — something other than “Ta.” Therefore, an error was generated.

Obviously, there is a need to use the double-quote character in a string of text. The question is how to pass that character along to printf() without it ruin­ing the rest of your day. The answer is to use an escape sequence. In the olden days, programmers would have simply gone without certain char­acters. Rather than trip up a string with a double quote, they would have used two single quotes. Some ancient programmers who don’t know about escape sequences still use these tricks.

 

Welcom to reproduce this passage. Please indicate the author and the source.
Author: Joe          Source:Seo Research Blog
Partner:dvd to flv  converter from dvd ripper

No comments:

Post a Comment