06 preprocessors
TRANSCRIPT
-
8/22/2019 06 PreProcessors
1/5
3.PreProcessors:
Preprocessors are the part of C++ programming which are compiled before the
compilation of the main programs.It is also known as Preprocessors compilation anddthe time taken to compile the preprocessors is kenown as PreCompilation Time.
Name Function of the preprocessor
#defineYou can use the #define directive to give a meaningful name to a
constant in your program.
#include
The #include directive tells the preprocessor to treat the contents of
a specified file as if those contents had appeared in the source
program at the point where the directive appears. You can organizeconstant and macro definitions into include files and then use
#include directives to add these definitions to any source file.Include files are also useful for incorporating declarations ofexternal variables and complex data types. You only need to define
and name the types once in an include file created for that purpose.
#errorError directives produce compiler-time error messages.
#ifdef & #ifndefThe #ifdefand #ifndefdirectives perform the same task as the #ifdirective when it is used with defined( identifier).
#if,#elif,#else,#endif
The #ifdirective, with the #elif, #else, and #endifdirectives,
controls compilation of portions of a source file. If the expression
you write (after the #if) has a nonzero value, the line groupimmediately following the #ifdirective is retained in the translation
unit.
#pragma
The #pragma directives offer a way for each compiler to offer
machine- and operating-system-specific features while retaining
overall compatibility with the C and C++ languages. Pragmas are
machine- or operating-system-specific by definition, and are usuallydifferent for every compiler.
#importThe #import directive is used to incorporate information from a type
library. The content of the type library is converted into C++ classes,mostly describing the COM interfaces.
#line The #line directive tells the preprocessor to change the compilers
internally stored line number and filename to a given line number
and filename. The compiler uses the line number and filename torefer to errors that it finds during compilation. The line number
usually refers to the current input line, and the filename refers to the
current input file. The line number is incremented after each line is
-
8/22/2019 06 PreProcessors
2/5
processed.
#undefAs its name implies, the #undefdirective removes (undefines) aname previously created with #define
4.Variables:
What are Variables?
Variable is a name location in memory that is user to hold a value that may bemodified by the program.All variables must be declared before can be used.
Syntax:
;
Where are Vaiables declared?
Variables will be declared in three basc places:
Location of declarationsName of the
variable
inside FunctionsLocal variables
In the definition of functionparameters
Formal parameters
Outside of all functionsGlobal Variables
a.Local Variables:
variables that are declared inside a function are called localvariables.In some cases C/C+
+ literature,these variables are referred to as automatic variables.
Local variabels may be referenced only by statements that are inside the block in which
the variables are declared.In other words,local variables are not known outside their own
code block.
Example:
-
8/22/2019 06 PreProcessors
3/5
void func1(void)
{
int x;x=10;
}
void func1(void)
{int x;
x=-199;
}
In the above example x is declared twice in function func1() and func2().The x in the
func1() has no bearing on or relationship to the x in func2().This is because each x is
only known to the code within the same block as the variable declaration.
b.Formal Parameters:
If a function is to use arguments, it must declare variables that will accept the values ofthe arguments. These variables are called theformal parameters of the functions. They
behave like any other local variables inside the functions. As shown in the following
example, their declaration occurs after the function name and inside the parenthesis.
Example:
/*Return 1 if c is a part of string s*/
int is_in*char *s , char c)
{while (*s)
if(*s == c)
return 1;
else s++;
return 0;
}
The function is_in() has two parameters: s and c.This functions returns 1 if the character
specified in c is containded within the string s;if it is not.
c.Global Variables:
Unlike local variables,global variables are known are known throughout the program andmay be used by any piece of code.Also,they willhold heir value throughout the programs
execution.You create gloable variables by declaring them outside of any functions.any
expression can access them.
-
8/22/2019 06 PreProcessors
4/5
Example:
#include o:p>
int count;
void finc1(void);
void func2(void);
int main(void)
{
count = 100;
func1();
return 0;
}
void func1(void)
{
int temp;
temp=count;
func2();
cout
-
8/22/2019 06 PreProcessors
5/5
putchar(.);
}
http://counter.fateback.com/