Friday, July 29, 2016

C Basics - Program Structure in C

Constants, Variables and keywords combined to form instructions. Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements.

A C "source program" is a collection of directives, pragmas, declarations, definitions, statement blocks, and functions. A C program basically consists of the following parts −
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments
Here is an elementary C program.

/*Sample program */
#include <stdio .h>
int main()
{ 
    /*display on output device*/
    printf("Hello World");

    return 0;
}

#include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation. C has a convention of using .h convention for header file and .c for source file. An include is the way to import the contents of one file into this source file. An .h header file contains list of functions you want to use in your program.

int main () is function name with return type.

Curly braces { } are generally used for the grouping of statements. The block of code inside the braces is function body.

Comment about the program should be enclosed within /* */. Comments are used to mention the purpose of the statement. Comments are not necessary. Any number of comments can be written at any place in the program. Comments cannot be nested. A comment can be split over more than one line.

All variables must be declared with respective data types before using. The statements in a program must appear in the same order in which we wish them to be executed. Every C statement must end with a ;. Thus ; acts as a statement terminator.

printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.

return 0; terminates the main() function and returns the value 0.



Related topics:
Intro of C   |   Overview of C   |   Features of C   |   Applications of C   |   The Setup for C   |   Source Program in C   |   Program Startup and Termination in C   |   First Program in C   |   Must Know Terms in C

List of topics: C Programming

No comments:

Post a Comment