Ques : Write a simple C program

, , No Comments

 From the above sections, you have become familiar with a programming language

and structure of a C program. Its now time to write a simple C program. This program
will illustrate how to print out the message “This is a C program”.

Ex : Write a program to print a message on the screen.

/*Program to print a message*/

#include <stdio.h>                      /* header file*/

main()                                        /* main function*/

{

printf("This is a C program\n"); /* output statement*/

}

Though the program is very simple, a few points must be noted.

Every C program contains a function called main(). This is the starting point of the program. This is the point from where the execution begins. It will usually call other functions to help perform its job. Some functions we have to write and others are used from the standard libraries.

#include <stdio.h> is a reference to a special file called stdio.h which contains information that must be included in the program at the time when it is compiled. The inclusion of this required information will be handled automatically by the compiler. You will find it at the beginning of almost every C program. Basically, all the statements starting with # in a C program are called preprocessor directives. These will be considered in the later units. Just remember, that this statement allows you to use some predefined functions such as, printf(), in this case.

main() declares the start of the function, while the two curly brackets { } show the
start and finish of the function. Curly brackets in C are used to group statements
together as a function, or in the body of a loop or a block. Such a grouping is known
as a compound statement or a block. Every statement within a function ends with a
terminator semicolon (;).

printf(“This is a C program\n”); prints the words on the screen. The text to be
printed is enclosed in double quotes. The \n at the end of the text tells the program to
print a new line as part of the output. That means now if we give a second printf()
statement, it will be printed in the next line.

Comments may appear anywhere within a program, as long as they are placed within
the delimiters /* and */. Such comments are helpful in identifying the program’s
principal features or in explaining the underlying logic of various program features.

While useful for teaching, such a simple program has few practical uses. Let us
consider something rather more practical. Let us look into the example that describes
complete program development life cycle given below:

0 टिप्पणियाँ:

Post a Comment