Basic Structure of C program
All C programs consist of the following elements:
Explanations
preprocessor Refer below
main() Every program must have a main() function and it is a first function that will be called.
The main() function is the one to which control is passes when the program is executed.
{ } These are known as braces or curly brackets. The statements of the functions are enclosed in these curly brackets. The opening curly brackets indicates that a block of statements is about to begin. the closing curly brackets terminates the block of the statements.
Example
|
Explanation
//A first ....... It is a comments.#include <stdio.h> It is one of the preprocessor directives. Line begins with # are processed by the preprocessor before the program is compiled. This specific line tells the preprocessor to include in the program the contents of the standard input/output stream header file
printf Also called console output, instruct the computer to print on the screen the string of characters contained between the quotation marks.
\ This is called escape character.Some common escape sequences ie \n.
\n The escape sequence means newline.
return 0 This statements terminates the function containing it and return the specified value to that function caller. In this example, it will terminate main and return 0 to its caller.
Console Output (printf)
printf is an object of the stdio class and is said to be “tied to” or connected to the standard output device, normally the display screen.
|
Header File
Standard libraries (and most C libraries) typically come with what are called “Header files”. These files provide information to the C compiler about the inputs and results produce by each function in the libraries. Header files usually accessed by the #include directive.Preprocessor Directives
Preprocessing occurs before a program is compiled. Some possible actions are : inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives. Preprocessing refer to the first step in translating. All preprocesssor directives begin with #. Preprocessor directives are not C statements, so they do not end in (;). It is processed fully before compilation begins. You can utilize three major capabilities of the preprocessor to make your programs modular, more readable and easier to customize:1. #include
This directive is to insert the contents of a file into your program. With this, you can place common declarations in one location and use them in all source file through file inclusion. The result is a reduced risk of mismatches between declarations of variables and functions in separate program modules. If the file is enclosed in angle brackets ( < and > ) - used for standard header files.
Examples:
#include <stdio.h>
2. #define
This directive define macros that enable you to replace one string with another. You can use it to give meaningful names to numeric constants, thus improving constants, thus improving the readability of your source files.
Examples:
#define PI 3.149
3. With directives such as #if, #ifdef, #else and #endif, you can compile only selected portion of your program. You can use this feature to write source files with code for two nor more systems, but compile only those parts that apply to the computer system on which you compile the program. With this strategy , you can maintain multiple versions of a program using a single set of source files.
Example
Escape Sequence
An escape sequences always begins with the backslash \ and is followed by one or more special characters.Escape Sequence | Description |
| \n | Newline |
| \t | Horizontal tab |
| \a | Alert. Sound the system bell |
| \\ | Backslash. Used to print backslash character. |
| \” | Double quote. Used to print double quote character. |
Example
|
C programs can include comments that provide information to a person reading the program. Comments in C are preceded by the two characters / and * and are followed by the same characters * and / in reversed order. A most common feature of most programs written by professional programmers is that there is a program heading as shown below:
|
No comments:
Post a Comment