Page 17 - Programming Microcontrollers in C
P. 17

2      Chapter 1  Introduction to C

                              This program contains all of the elements of a C program. Note
                          first that C is a “free form” language. Spaces, carriage returns, tabs,
                          and so forth are for the programmer’s convenience and are ignored
                          by the compiler. The first line of the program

                   #include <stdio.h>
                          is called a preprocessor command. Preprocessor commands are iden­
                          tified by the # at the beginning of the line. In this case, #include
                          tells the preprocessor to open the file stdio.h and read it into the
                          program to be compiled with the remainder of the program. The file
                          name is surrounded by angle brackets < >. These delimiters tell the
                          compiler to search for the file in a region designated by the operating
                          system as SET INCLUDE. Had the file name been delimited by
                          double quotes, “ “, the operating system would have searched only
                          the default directory for the file. The default directory is, of course,
                          the directory from which you are operating.
                              The next line of the program is a definition for a function named
                          main. In ANSI C, as opposed to classic C, each function definition
                          must inform the compiler of the return type from the function, and
                          the type of the function’s arguments. In this case, the function main
                          has to return an integer and it expects no arguments. The type int
                          preceding the function name indicates that it returns an integer and
                          that no arguments to the function are expected.
                              The line following the function definition contains an opening
                          brace {. This brace designates the beginning of a block or a com­
                          pound statement. The next line of the program contains a function
                          call to the function printf(). This function is made available to
                          the program by the inclusion of the header file stdio.h, and it is a
                          function that writes a message to the computer terminal screen. In
                          this case, the message to be sent to the screen is

                   Microcontrollers run the world!
                          The escape character ‘\n’ at the end of the message informs the
                          program to insert a new line at that point. The complete message
                          including the new line escape character is enclosed in double quotes.
                          These double quotes identify a string, and the string is the argument
                          to the function printf(). Note that the statement beginning with
                          printf is closed with a semicolon. In C, every statement is termi­
                          nated with a semicolon.
   12   13   14   15   16   17   18   19   20   21   22