Saturday, 3 December 2016

C - Program Structure


Before we study the basic building blocks of the C programming language, let us look at a bare minimum C program structure so that we can take it as a reference in the upcoming chapters.

A C program basically consists of the following parts −
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments
Let us look at a simple code that would print the words "Hello World”:−
/* my first program in C */
#include <stdio.h>
#include <conio.h>
void main()
{
   /* used to clear the screen */
   clrscr();
   /* used to print any message on the screen */
   printf("Hello, World! \n");
   /* used to hold the screen */
   getch();
}

Points to Remember:
1.   C Language is strictly case-sensitive language. Therefore in C, ‘P’ and ‘p’ are two different things.
2.   Body of the Program is always written in {} after the main() function and every line/statement written ends with a ‘ ; ’ semicolon.
3.   Statements written in /*…*/ has no effect on code, they’re known as Comments. They’re written just for our own understanding.
4.   C programming file can be given any name but should be saved by extension “.C”. Ex: “Hello.c”.
Let us take a look at the various parts of the above program: −
=>Pre-processor:
#include, the first word of any C program. It is also known as pre-processor. The main work of pre-processor is to initialize the environment of program, i.e to link the program with the header file <stdio.h>.
=>Header file:
Header file is a collection of built-in functions that help us in our program. Header files contain definitions of functions and variables which can be incorporated into any C program by pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas like string handling, mathematical functions, and data conversion, printing and reading of variables.
To use any of the standard functions, the appropriate header file must be included. This is done at the beginning of the C source file.
For example, to use the printf() function in a program, the line #include <stdio.h> is responsible and to use the getch() function, the line #include <conio.h> is responsible.
=>main() function:
main() function is a function that must be used in every C program. A function is a sequence of statement required to perform a specific task. main() function starts the execution of C program. 
printf() is also a function which is used to print any message on the screen whether it may be hello world or your own name. The message that you wish to print is written inside brackets () in “ ” double-quotes.
Ex: printf(“Hello World”); printf(“My name is Mehul”);

Compile and Run:

There are many different ways to compile and run a C program. All that is required is a C compiler. We will recommend you to use Turbo C IDE that we’ve already downloaded and installed for c programming.

Step 1: Open Turbo C IDE(Integrated Development Environment) or Turbo C icon created on your desktop, click on File and then click on New.

Step 2: Write the above example as it is.

Step 3: Click on File menu and Click on Save or just press F2.

Step 4: Write the name of the file as “MyProgram.c” and press enter to save.

Step 5: Click on compile or press Alt+f9 to compile the code.

Step 6: Click on Run or press Ctrl+f9 to run the code.

Step 7: Output.

Video of Program:-

No comments:

Post a Comment