For understanding a program of C/C++ we need to know some basics of it. Lets start with our basics of a computer. The contents we are having in a computer is RAM (memory), Hard-disk-drive (HDD), Motherboard, keyboards, monitors, mouse, etc. For storing anything in a computer we have storage areas like RAM, HDD, CD/DVD/BRD (blueray DVDs), flash memory etc.
In C/C++ the program files (with extension .c or .cpp) are stored in HDD and at the run time the program is stored in the RAM. From there the RAM sends the info to the processor/core wherein the arithmetic logic operations are done by the ALU(Arithmetic Logic Unit). After operation there has to some storage area so as to store the results, so the results are stored again in the memory (RAM) and then the o/p is send to the screen or file or the database.
Now we know how the CPU works for a program to get a desired output, but do you know how a .c/.cpp file is processed? The processing in a C/C++ programming is done with the help of Assembler, Compiler, Loader and Linker. Here we'll discuss about assembler and compiler later on we'll come to Loader and Linker. Assembler and Compiler are basically used for converting the c/cpp file to the assembly file (with extension .asm) and binary object (with extension .obj) respectively and at run time of the program it is then converted into .exe file or the executing file which further gives the output.
In colleges/ schools you must have heard of the word Library which is place where there is a collection of books, magazines, etc. Similarly C/C++ programming do have a library which is not a collection of books rather it is a collection of functions and is of two types static and dynamic. Static library (.lib extension files are static libraries) or Statically-Linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, thereby producing an object file. Whereas Dynamic library or Dynamic-link library or simply DLL (.dll files in your computer) is in actuality a special loader that loads externally shared libraries into a running process and then binds those shared libraries dynamically to the running process. .dll file also have a helping hand or a loader which is a .lib file (note: here the file extension .lib is not a static library file). It helps the .dll file to load the header files at the time of execution of the program.
Now lets have a look on the first line of a C/C++ program
#include<stdio.h>
Herein why we are using 'hash (#)','include','stdio.h' or what is the need of doing this. Hash (#) is preprocessing directive which gives a order to the main function or the entry point of the program (remember it doesn't request it just give an order to the function), since the processing of the code/program starts form the main(). Include command is used copy and paste the contents of the header file "stdio.h" for the current file. Stdio is a standard input/output header file. A header file contains forward declarations of classes, subroutines, variables and other identifiers. '< >' symbols means that the header file is located in the search path.
Moving ahead to the next line is a main function line and is written as
void main()
int main()
here void and int are the return datatypes i.e. the function main() would either return a void datatype (or null) or an integer datatype. Now the question arises why do we need to define main function, who calls it and to whom it returns the value, so the answer is at the time of execution of the code/program main function is used by run time library as the exit code of the process. The environment or compiler or the system calls the function and it returns the values to the entities who called the main function i.e. the compiler.
Functions are used to performing repetitive tasks or we can say that the functions are those set of instructions which provides us the better efficiency of a program and thereby reducing the time as it could be used in various places of the code/program. For making your own function in a program then one must follow the following operations
- Function Declaration or Prototype or headers
- Function Definition
- Function Call
Lets see a small code of the function and a function calling
#include<stdio.h>
int add(int,int) // Function Declaration or Prototype or Header
int/*return datatype*/ add/*function name*/ (int a, int b)/*function signature*/;
{
return(a+b);//Function Definition
}
void main()
{
printf("adding a+b= ",&add(5,6)); //Function call
}
Here the green line shows the function declaration/prototype/header, the yellow line shows the function definition and the red line shows calling of a function.
For using a function a user must have to declare a function. The function declaration contains the name of function, Return type of the function and also the number of arguments that a user will take for performing the operation.
The Function Prototyping Contain
- Return type of a function
- Name of Function
- Argument List
Function call has to be filled with the parameters as in this case the parameters are integer type and since there are two parameters declared so it is mandatory to fill add function with two parameters during a function call.
No comments:
Post a Comment