Tuesday, 3 March 2015

An Example C Program


code is a C program which displays the message “Hello, World!” on the screen.

/* Program-2.1 - My First Program */

#include <stdio.h>
main()
{
printf("Hello World!");
}

The heart of this program is the function printf, which actually does the work. The C language is built from functions like printf that execute different tasks. The functions must be used in a framework which starts with the word main(), followed by the block containing the function(s) which is marked by braces ({}). The main() is a special function that is required in every C program.

The first line starting with characters /* and ending with characters */ is a comment. Comments are used by programmers to add remarks and explanations within the program. It is always a good practice to comment your code whenever possible. Comments are useful in program maintenance. Most of the programs that people write needs to be modified after several months or years. In such cases they may not remember why they have written a program in such a manner. In such cases comments will make a programmer’s life easy in understanding the source code and the reasons for writing them. Compiler ignores all the comments and they do not have any effect on the executable program.

Comments are of two types; single line comments and block comments. Single line comments start with two slashes {//} and all the text until the end of the line is considered a comment. Block comments start with characters /* and end with characters */. Any text between those characters is considered a block of comments.

Lines that start with a pound (#) symbol are called directives for the preprocessor. A directive is not a part of the actual program; it is used as a command to the preprocessor to direct the translation of the program. The directive #include appears in all programs as it refers to the standard input output header file (stdio.h). Here, the header file stdio.h includes information about the printf( ) function. When using more than one directive each must appear on a separate line. A header file includes data types, macros, function prototypes, inline functions and other common declarations. A header file does not include any implementation of the functions declared.

The C preprocessor is used to insert the function definitions into the source files and the actual library file
which contains the function implementation is linked at link time. There are prewritten libraries of functions such as printf() to help us. Some of these functions are very complex and long. Use of prewritten functions makes the programmers life easier and they also allow faster and error free development (since functions are used and tested by many programmers) development. The function printf is embedded into a statement whose end is marked by a semicolon (;). Semicolon indicates the end of a statement to the compiler.

The C language is case sensitive and all C programs are generally written in lowercase letters. Some of
the special words may be written in uppercase letters.

No comments:

Post a Comment