Understanding White Space

The final concept covered in this chapter is that of white space. White space refers to blank lines, tabs, and spaces that add clarity to your coding, both during writing and running.

C, like many languages, is generally white space insensitive. This means that you can add blank lines to make your scripts easier to read. You can also add a little padding between a function call and its opening parentheses. Most important, you can use spaces to indent code, thereby indicating what code is a subset of what routines (for example, whether a line of code is part of a function or a conditional). This is a critical concept in making your code more approachable.

Finally, you'll learn how to add white space to the messages printed by an application. For example, to add a newline to the outputted message, print the newline character:

printf ("There will be a break here:
");

To demonstrate this, you'll write the Hello, world! example one last time, as a proper source file should look.

To add white space to your file

1.
Open hello3.c (Script 1.4) in your text editor or IDE.

2.
After the first comment, which ends on line 4, press Return or Enter once to add a blank line (Script 1.5).

Script 1.5. Add white space to make your code easier to read and comprehend.


This blank line will help to separate out the initial comment from the meat of the code.

3.
After the #include line, press Return or Enter again to add another blank line.

This blank line differentiates the includes section from the functions.

4.
After the initial function definition line, add another blank line.

5.
Insert four spaces before each of the function's main lines: printf(), getchar(), and return.

In order to mark that these lines are all part of the main() function, they'll be indented four spaces. This way, when glancing over the code, you can easily tell what sections belong with what functions. You'll see this method repeated frequently in this book.

6.
Change the print line to read as follows:

printf("Hello, world!
");

The addition of at the end of the print statement will create a break after that text, as if you pressed Return (or Enter). The end result will be a little bit clearer when the application is executed.

7.
Add another blank line before the closing curly brace.

8.
Save the file as hello4.c.

9.
Compile and run hello4.c using the command-line or IDE method (Figure 1.11).

Figure 1.11. While the extra spacing didn't affect the executed result, the newline character made the cursor appear on the line after the Hello, world! message.


✓ Tips

  • Despite the fact that we highly recommend adding copious amounts of white space to your scripts, our scripts won't be as spaced out, again in an effort to save valuable book space.

  • Certain characters when escaped (preceded by a backslash) have special meanings (e.g., is newline). You'll learn about several others over the course of this book.

  • The reason four spaces are used for indentation instead of a single tab is for compliance across all text editors.


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

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