#include <stdio.h> /*This is a very simple program */ void main() { (void) printf(ÒSuccess\nÓ); }Text version for compilation.
It is very straight forward, but it shows several important components, used in all programs. Until we have these basics, we cannot do interesting things.
The first line is called an include directive. It must contain a '#' character at its very beginning, followed by the word ÒincludeÓ (with no intervening space), followed by a name of a collection of predefined features (stdio.h in this case) enclosed in angle brackets. Since we want to write something with this program, this directive must be given. It imports a library of standard input and output features for use in the program.
The next line is blank. Blank lines are ignored by the compiler, but we use them to make our programs clearer to humans. We shall ignore other blank lines in this explanation, but you should use them where they help to separate sections of a program.Many editors are set up to lay your programs out in a helpful way.
The next line contains some English words enclosed in bracketting "/*" and "*/" punctuation. In C, but not C++, this constitutes a comment. Any sequence in a program, which is not enclosed by double quotes and which is bracketted in this way, with anything in between, is ignored by the compiler. Comments are enormously helpful in following what is going on when reading a program. Your tutor will expect detailed comments in all coursework handed in.
In C++ the convention for comments is to have the comment start with "//" and end at the end of that line. Note that such a comment may begin anywhere in the line, but then continues until the end of line. Thus, while C comments may be placed almost anywhere, C++ comments are either the whole of a line or the end of a line. Also note that while a C comment, being bracketed, may extend over several lines, a C++ comment ends when its line ends.
Most compilers will accept both forms.
The first significant line after the program statement is the one containing the word "main". This tells the compiler that the initial actions of this program are about to be given.
It is always preceded by either the word void, meaning that no return code will be generated, or the word int, meaning that a value will be returned for checking by the runtime system.
It is always going to be followed by parentheses. In this case they are empty, indicating that the program will ignore any runtime arguments. The word void might be given inside them to make it clear that no arguments are to be used. The alternative form is to include the argument specifiers int argc, char*[] argv, which are the number of runtime arguments given and an array of that many pointers to strings containing those arguments.
The curly brackets or braces which enclose the rest of the program indicate that the following part of the program is to be considered as a whole. It is called a program block. In this case, the block is the sequence of actions associated with the name main. A block can contain declaration statements, action statements and comments.
The only real action statement here is a call on a function, "printf", which is part of the stdio library. A function call invokes some action or actions. It is called by writing its name as a statement. Many functions are used with parameters. These provide additional information required by each call statement for the function and affect what exactly the procedure does this time. Even where no parameters are being passed to a function, empty parentheses must be written.
Warning: If you miss the empty parentheses in a function call, the result is still legal and the compiler will not generate an error message. The result will be that the function will not be called. Strictly speaking the function name without parenthese following is a pointer to that function. An expression in C can be given wherever a statement is expected and so there is no error in just writing a pointer expression.
Here printf is followed by one parameter, consisting of a sequence of characters enclosed in double quotes, i.e. ""Success\n"". Such a sequence is called a string. The end of the string contains "\n", which tells the function to start a new line of output after the preceding character. Such escapes are used to control the formatting of output and do not appear in the output themselves. Thus printf will write the string "Success" onto the screen and move on to the start of a new line.
#include<stdlib.h> #include<stdio.h> /*This is a less simple program */ int main(int argc, char* argv[]) { if (getchar()=='a') printf("Success\n"); else printf("Failure\n"); return EXIT_SUCCESS; }Text version for compilation.