Thursday, 6 September 2018

Function , compilation and execution

About #include and main()

In C and C++ languages, the symbol of # is called pound and any statement which begins with # is called as pre-processor directives.

Some of the pre-processor directives are -

#include
#define
#undef
#if
#else
#elif
#endif
#ifdef
#ifndef

Amongst all of them, the most commonly use directive is the #include directive which is called as file inclusion directive.Whenever the processor find these directives in our programme this take the following actions -
1. It reads the name of the file mentioned in angular bracket.
2. It copies the entire coding of the mentioned header file and paste it in our programme in place of the #include statement.

Due to this number of statement in our program are increased and a separate copy of our program gets created called as expanded source code. This expanded source code is then converted into the machine code by compiler.

What is a function and why do we write main()?

In C programming, a function is a set of statements having a particular name followed  by
 pareldisis "()" and every C program contains one and more functions.

Amongst these functions the most important is the function main(). This is because whenever we execute our C program, its  execution always begins from the function main().

Can we compile a C program without main()?

YES,we can compile a C program even if it is not having the function main() but we can never execute a C program without main().This is because compilation of a program always starts from the first line but its execution which is done by operating system , always begins from main().So we can say that function main() is the entry point of execution of our program.

Compiling a C program-

The second step in programming is to compile our programs. Compilation means conversion of the program from source code to the machine code.Whenever we compile a C program the compiler take two actions-
1. It checks our program for syntax error.
2. If no syntax error is present, the most compiler successfully converts our program to machine code.But if our program contain even a single error , the compilation step and machine code is not generated.



Execution of C program-

To run a c program in TURBO IDE we have two options-
1.Using the run menu from menu bar.
2.Using keyboard shortcut alt+R or ctrl+F9.

Whenever we run a program in TURBO IDE , the output gets displayed on the console window but to see this out, we have to open the console window and this can be done using another key combination called alt+F5.

Using the function getch()

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello User");
getch();
}


The function getch() stands for get character and is available inside the headerfile conio.h .
Whenever we call the function getch() in our program C language takes two actions-
1.The execute pauses.
2.The console window gets opened up.
So if we call getch() at the end then we don't have to use alt+F5. There is another very important use of getch() which is to accept hidden inputs.

























No comments:

Post a Comment