Printing Text

If you were to compile and run the template.c example (written in the previous section), it should work, but it won't do much. To take this up one notch, you'll have the application print a message when it's run. This example will teach you how to create a simple Hello, world! program, a staple of any programming text.

To print text in C, use the printf() function:

printf("I could catch a monkey.");

The printf() function is a printing function with formatting capability (hence, the f). Throughout the rest of the book, beginning with Chapter 3, “Working with Numbers,” you'll see how to format text with printf(), but for the time being, it will be used in its most basic way.

Before getting into an example, you should revisit the exact syntax of printf(). The parentheses hold the arguments the function takes, the first (and in these early examples, only) argument being a string of text. This text is enclosed within straight double quotation marks. Finally, the line concludes with a semicolon, as every statement in C must.

To print text in C

1.
Open template.c (Script 1.1) in your text editor.

2.
Within the main() function definition, before the return line, add the following (Script 1.2):

printf("Hello, world!");

Script 1.2. A simple Hello, world! example.


Function names are case-sensitive in C, so be certain to type this exactly.

3.
Create a new folder for your C files (Figure 1.1).

Figure 1.1. When you develop and program your own applications, keep your files organized in logical directories like these.


As you develop more complex applications, you'll want to get into the habit of keeping your files organized. As a suggestion, create a C_code folder for all of the code you'll be developing in this book. Then create a Ch01 folder within C_code and a hello folder within Ch01. This will be the repository for all of the files in this example.

4.
Save the file—within its proper folder—as hello.c.

So as not to overwrite the existing template, you should give this file a new name, one that is descriptive of what the file does.

✓ Tips

  • Another benefit of using an IDE is that it will normally organize your folders and all of your source files automatically for each project you do.

  • Statements in C, such as the use of the printf() function, can run over multiple lines. You can also put multiple statements on one line, as long as each is separated by a semicolon.

  • Make sure that your text editor does not use curly (also called smart) quotation marks—these can create problems for some compilers.

  • All of the examples in this book will print their messages to the command prompt window, where the application will be run. You can, using C, create conventional applications that print text within the context of a proper interface, but this is beyond the scope of the book as well as standard C, requiring a graphical user interface (GUI) builder, application programming interfaces (APIs), or other nefarious acronyms.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset