Sunday, February 13, 2011

Inline function

Inline functions are a crucial function in C + + and is often used with classes. Inline functions are functions where the call is made to all inline functions. The actual code that will be placed in the calling program. In a program called a function in the program jumps to the address of the function and when it reaches the end of the function it returns. This jump is actually much more involved and time consuming. But when an inline function is called the compiler replaces the call with the function code. So, in fact, at this point there will be no function calls, only the code of the function. No need to jump to different addresses. The general format of inline function is:

inline datatype function_name(arguments)

Example:
The following code creates and calls an inline function:
#include<iostream.h>
inline int average(int a, int b)
{
   return (a + b) / 2;
}
void main()

{
   int result = average(12, 14);

   cout << "The average of number 12, 14 is " << result << "\n";
   getch();

}
It 's very important to know that line is specified in the request, not a command for the translator. If for various reasons, the compiler is able to meet the demand, the operation has been translated into normal operation.

Thursday, February 10, 2011

What is macros in C

A macro is a fragment of code that took its name. When we use the name of the macro-program, which replaces the contents of the macro. You can specify any valid identifier as a macro, even if AC is the key word. There are two types of macros. One is a macro-like object, and the second is a function-like macro. Macro-object as the parameters of the function-like macros do not. The general syntax to declare a symbol as a macro for each of:
For object-like macros:
 #define <identifier> <replacement token list>
For example:
#include<stdio.h>
#include<conio.h>
#define sum a+b
void main()
{
Clrscr();
int a=5,b=10,c;
c=sum; //if we give the semicolon (;)when we define the macro; then we cannot give the semicolon when we write the name of identifier. If we give the semicolon when we define the macro, then we give the semicolon when we write the name of identifier in program
printf(“The sum of a+b is: %d”,c);
getch();}
For function-like macros:
#define <identifier> (<parameter list>) <replacement token list>
For example:   
#include<stdio.h>
#include<conio.h>
#define square(x) x*x;
Void main()
{
Clrscr();
int i=2,j;
j=square(i) //Here is no semicolon because I give the semicolon when I define the macros.
printf(“The value of j is: %d”,j);
getch();
}
 The #undef directive removes the definition of a macro.
Written by arnob;