BASIC COMPONENT OF A C PROGRAM

Now lets begin exploring the C programming language. Before we start executing programs in C Turbo C/C++ Compiler we must know some basic component of a C program.

#include - The #include is known as a preprocessor directive and is used to tell the C preprocessor to find the stdio file with extension .h. <stdio.h> stand for standard input output stream header file and contains information for printf, scanf etc.

main() - Exectuion of a a program starts from a main() function. It defines the point from where the execution of the program starts. Anything written between opening curly brace and ending curly brace of main is executed.

printf() - This is the standard way of producing output. The functionality of printf() is referenced in stdio.h by the C compiler, thus it always work in the same way.

scanf() - This is the standard way of taking input from user. The functionality of scanf() is also referenced in stdio.h by the C compiler.

comments: Comments are information given by the program to make a program readable and easy to understand. It reduces the complexity of a program. Anything written as comments is ignored by the compiler.

There are two ways of writing comments:

1. Single line comment

Syntax:
//Your comments here

example:
 // Hello this is my first C program

2. Multiline comment  

Syntax:
/* Your comment
Your comment*/

example: 
/* This is my first C program
and i am very excited about it */


SYNTAX:

Note : It should be noted that some compiler does not include header file "conio.h". It is also not included in compiler of Linux or any other Unix based operating system. So in case using it displays an error remove this and the function associated with it i.e getch().

#include<stdio.h> //This tells the compiler about the input/output functions such as printf(), scanf() 
#include<conio.h> //It is used for getch() function
int  main() // It is the entry point of a program
{ // Progam begins with this curly braces
printf("Congratulation you successfully run your  first program"); // to print in the output screen
getch(); // to hold the output screen
return 0; // tell the OS that the program exited without error
} // Program end with this curly braces

OUTPUT:

Congratulation you successfully run your  first program

EXPLANATION:

When we execute this program (press Ctrl+F9 to execute in Turbo C or F9 in Dev C++).

NOTE: If you want to execute this program in Dev C++ then there is a minor change. Replace void main() with int main() as Dev C++ compiler does not support void as return type for main(). Also replace getch() with return 0.

return 0 means that the program is terminated successfully and the compiler is returning back the control to the computer

Now on execution the program will display "Congratulation you successfully run your  first program" (without double quotes) as output on monitor. Any string you pass within double quotes through printf is sent to console output (monitor) i.e it is displayed as it is on the monitor.

PROGRAM EXAMPLE:

#include<stdio.h>
int main()
{
printf("%d", 5+6); // %d is conversion character for integer
return 0;
}

OUTPUT:

11

EXPLANATION:

The above program contains only one statement to be executed within main() function. As this statement contains 5+6 without quotes, it will add these two numbers and pass the result in integer ( as indicated by conversion character %d ) to console window i.e 11 is sent to the monitor. So in this case the output is 11.

So whenever we use double quotes, compiler just prints the message ignoring what has been include there but when no quotes are used, actual values are processed according to instructions and executed accordingly.

Share this

Related Posts

Previous
Next Post »