Basic Syntax

Creating a C source file is simple, as long as you follow the standard syntax. A C document is just plain text—written in practically any text editor—with a .c file extension. The basic syntax of a minimal C file is

#include <stdio.h>
int main (void) {
   return 0;
}

The first line states that the stdio.h package should be included (this is a preprocessor directive, which you'll learn about in Chapter 8, “Using the C Preprocessor”). Including stdio.h has the same effect as making that file's contents part of this document (but without the extra typing or lines of code). The stdio.h file, which stands for standard input/output, brings fundamental functionality—such as the ability to take input from a keyboard and display output on the screen—to your C code.

The next three lines define a function called main(), which is always the first function called (or run, in layman's terms) by any C application. In this example, the function will return an integer value, as indicated by the preceding int. The function takes no arguments—values sent to a function for it to use—as indicated by the void. The contents of the function itself are placed between the opening and closing curly braces. For this minimal, downright pointless, example, all the function does is return the value 0.

Because this is the standard template for the vast majority of the examples in this book, you should quickly write this up as your first C document.

To create a C source file

1.
Open your text editor or integrated development environment (IDE).

C applications begin as plain-text source files. These can be written in any text editor that allows you to save files as plain text. Obviously some (like BBEdit for the Mac or Vi for Unix) are better than others. Some text editors, like TextEdit or WordPad, will always try to save files as Rich Text Format (.rtf) or with an extra extension. You should avoid using these editors for that very reason.

There are some free IDEs you can use, like Eclipse (for Windows, Unix, and Mac) or Xcode (Mac OS X). Or, you can always invest in a commercial IDE like Visual Studio (Windows) or CodeWarrior (Windows, Unix, and Mac). Among the many benefits of a formal IDE over a text editor are built-in debugging, compiling, and execution. For more information on the available tools, see Appendix A, “Installing and Using C Tools.”

For the purposes of this book, we highly recommend—and all of the examples have been tested using—Dev-C++ on Windows and Xcode on Mac OS X, both of which are free and easy to use.

2.
Create a new, blank text document.

3.
Type the required include line (Script 1.1):

#include <stdio.h>

Script 1.1. The basic structure of a minimal C source file.


This line should be the first thing entered into your text file. Notice that there are no blank lines before it or even blank spaces between the number sign and the word include. Also—and you'll come to see why this is important in time—note that the line does not end with a semicolon.

4.
Begin defining the main() function:

int main (void) {

Again, this line begins the definition of a user-defined function called main(). It takes no arguments (also called parameters) and will return an integer value. The opening curly brace marks the beginning of the function's contents.

5.
Add the return line to the function:

return 0;

All functions in C should return a value, even if it's just the number 0. This value is meant to reflect that no errors occurred. More complex functions might return a 1 to indicate that a problem occurred.

As you progress in your C learning, you'll have your functions do much, much more.

6.
Close the main() function:

}

Don't forget the closing curly brace. This marks the end of the main() function definition.

7.
Save the file as template.c.

Remember that C source files will always use the .c extension. This one will be called template, as it'll be the template for other examples.

✓ Tips

  • Some C source code files (although not those written in this book) will use the format

    main () {
    
    or
    
    void main () {
    

    to define the main() function. These variations are not C99 compliant and will work inconsistently across different compilers. For this reason this book uses the formal, standard

    int main (void) {
    
  • Different operating systems have various restrictions as to how long a file's name can be. A basename (for example, template without the .c extension) is limited to 8 characters in MS-DOS and 14 on some versions of Unix, but Windows, Mac OS X, and most operating systems allow for a reasonably long name.

  • Although Windows and Mac OS X are not case-sensitive when it comes to filenames, Unix is. Treating your files as if case-sensitivity matters is a good programming habit and makes your code more portable. Hence, in this example, it's important that you use template.c as opposed to template.C or Template.C.


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

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