Scope and Preprocessor
Scope and Preprocessor
PROPERTIES OF A VARIABLE
SCOPE
The scope of variable determines over what region of the program a variable is actually available for
use (‘active’).
VISIBLITY
LONGETIVITY
Longevity refers to the period during which a variable retains a given value during execution of a
program (‘alive’).
1. Internal (local)
2. External (global).
STORAGE CLASS
AUTOMATIC
STATIC
EXTERNAL
REGISTER
AUTOMATIC VARIABLES
The variables without any storage class specification are called as auto variables. They are called
automatic because their memory space is automatically allocated as the variable is declared.
Syntax:
Example:
//SCOPE OF M IS
WITHIN MAIN
//SCOPE OF M IS
WITHIN
FUNCTION1
//SCOPE OF M IS
WITHIN FUNCTION2
EXTERNAL VARIABLES
Variables that are both alive and active throughout the entire program are known as external
variables.
//external declaration of y inside the functions informs the compiler that a is an integer type defined
somewhere else in the program.
//glob.c
STATIC VARIABLES
Static variables are initialized only once. The variables will not be reinitialized.
Static storage specifier cannot be used as the arguments for this functions.
REGISTER VARIABLES
Ans: B
Ans: C
Ans: D
Ans: D
Ans: A
Ans: C
Ans: B
Ans: A
Ans: B
Ans: B
Ans: C
Ans: A
Ans: A
Ans: A
Ans: A
Ans: B
Ans: C
Preprocessor
• A Preprocessor is a macro processor program that automatically processes our source program by
the C compiler before it is passed to the compilation.
• A C Preprocessor is just a text substitution tool. We'll refer to the C Preprocessor as CPP.
• The Preprocessor provides the ability for the inclusion of header files, macro expansions,
conditional compilation, and line control.
• As it is a text substitution tool, it instructs the compiler to do required pre-processing before the
actual compilation.
• A preprocessor is such a great convenience that virtually all C programmers rely on it.
The C preprocessor
• The preprocessor offers several features called preprocessor directives that begin with a (#)
symbol.
• The directives can be placed anywhere in a program but are most often placed at the beginning of
a program, before the first function definition.
• They are,
1 __DATE__
2 __TIME__
3 __FILE__
4 __LINE__
5 __STDC__
current line : 7
current time : 1
Macro expansion
#define UPPER 25
main( )
int i ;
printf ( "\n%d", i ) ;
• In this program instead of writing 25 in the for loop we are writing it in the form of UPPER, which
has already been defined before main( ) through the statement,
#define UPPER 25
• This statement is called ‘macro definition’ or more commonly, just a ‘macro’. During preprocessing,
the preprocessor replaces every occurrence of UPPER in the program with 25.
main( )
float r = 6.25 ;
float area ;
area = PI * r * r ;
• UPPER and PI in the above programs are often called ‘macro templates’, whereas, 25 and 3.1415
are called their corresponding ‘macro expansions’.
12.57
File Inclusion
#include
• The contents of another file to be compiled as if they actually appeared in place of the #include
directive.
• The way this substitution is performed is simple, the Preprocessor removes the directive and
substitutes the contents of the named file.
•#include <filename.h>
•#include “filename.h”
Conditional compilation
These Conditional Compilation Directives allow us to include certain portion of the code depending
upon the output of constant expression
#if Expression
Statement 1
#elif Expression
Statement 2
#else
Statement n
#endif
✔ Expression allows only constant expression
✔ Result of the Expression is TRUE , then Block of Statement between #if and #endif is followed
✔ Result of the Expression is FALSE , then Block of Statement between #if and #endif is skipped
✔ Result of the Expression is Non-Zero , then Block of Statement between #if and #endif is followed
✔ Result of the Expression is Zero , then Block of Statement between #if and #endif is skipped
Number is Even
if will be decided at compile time, a "normal"
#pragma Directive
This directive is a special purpose directive and is used to turn on or off some features.
This type of directives are compiler-specific i.e., they vary from compiler to compiler. Some
These directives helps us to specify the functions that are needed to run before program
startup( before the control passes to main()) and just before program exit (just before the control
returns from main()).
Inside func1()
Inside main()
Inside func2()
Ans: B
Ans: A
Ans: D
Ans: A
Ans: A
Ans: D
Ans: D
Ans: B
Ans: B
Ans: C
Ans: D