0% found this document useful (0 votes)
2 views

Scope and Preprocessor

The document discusses the properties of variables in programming, including scope, visibility, longevity, and the distinction between local and global variables. It explains storage classes such as automatic, static, external, and register variables, detailing their characteristics and usage. Additionally, it covers the C preprocessor, its directives, and features like macro expansion and file inclusion, emphasizing its role in enhancing code management and compilation.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Scope and Preprocessor

The document discusses the properties of variables in programming, including scope, visibility, longevity, and the distinction between local and global variables. It explains storage classes such as automatic, static, external, and register variables, detailing their characteristics and usage. Additionally, it covers the C preprocessor, its directives, and features like macro expansion and file inclusion, emphasizing its role in enhancing code management and compilation.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Week 16 – Scope & Visibility 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

The visibility refers to the accessibility of a variable from the memory.

LONGETIVITY

Longevity refers to the period during which a variable retains a given value during execution of a
program (‘alive’).

LOCAL AND GLOBAL VARIABLES

The variables may also be broadly categorized, depending on the

place of their declaration, as:

1. Internal (local)

2. External (global).

LOCAL Variable declared within a particular function, or block

GLOBAL Variables declared outside of any function.

STORAGE CLASS

The storage class of a variable determines

􀀀 Variable is stored in the memory or CPU register.

􀀀 What the initial value of the Variable

􀀀 What is the Scope of the Variable

􀀀 What is the linkage of a Variable

TYPES OF STORAGE CLASSES

AUTOMATIC

STATIC

EXTERNAL

REGISTER
AUTOMATIC VARIABLES

􀀀 auto is the keyword to declare 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.

􀀀 Auto variables are stored in main memory.

􀀀 Scope of auto variable is local lifetime.

􀀀 Auto variable will have a garbage value as initial value

􀀀 The storage class auto cannot be used with global variables.

SYNTAX AND EXAMPLE

Syntax:

auto datatype var1,var2,…varN;

Example:

auto int a,b; OR int a,b;

auto float x,y; OR float x,y;

//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 variables are declared outside a function.

􀀀 They are also known as global variables.

􀀀 Global variables can be accessed by any function in the program.


Extern variable

//Extern keyword to declare ‘a’

//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.

􀀀 Default value of static variable is ‘zero’(static variables are initialized to 0)

􀀀 The value of a static variable are permanent within function.

􀀀 the value of the variable will be retained throughout the program.

􀀀 Static variable are stored in main memory.

􀀀 It can also be used as both local and global variable

􀀀 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 two most frequently used features are,

#include - Includes the contents of a file during compilation.

#define - To replace a token by an arbitrary sequence of characters

Why Preprocessor are needed in c?

• 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 directives and descriptions

• #define - Substitutes a preprocessor macro.

• #include - Inserts a particular header from another file.

• #undef - Undefines a preprocessor macro.

• #ifdef - Returns true if this macro is defined.

• #ifndef - Returns true if this macro is not defined.

• #if - Tests if a compile time condition is true.

• #else - The alternative for #if.

• #elif - #else and #if in one statement.

• #endif - Ends preprocessor conditional.

• #error - Prints error message on stderr.

• #pragma - Issues special commands to the compiler, using a standardized method


Features of 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,

•(a) Macro expansion

- Macros with Arguments

- Macros versus Functions

•(b) File inclusion

•(c) Conditional compilation

•(d) Miscellaneous directives

The Predefined Macros

S . No. Macro & Description

1 __DATE__

The current date as a character literal in "MMM DD YYYY" format.

2 __TIME__

The current time as a character literal in "HH:MM:SS" format.

3 __FILE__

This contains the current filename as a string literal.

4 __LINE__

This contains the current line number as a decimal constant.

5 __STDC__

Defined as 1 when the compiler complies with the ANSI standard.


current date : Jan 12 2025

current time : 09:12:47

current line : 7

current FILE : cpp.c

current time : 1

Macro expansion

#define UPPER 25

main( )

int i ;

for ( i = 1 ; i <= UPPER ; 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.

• Here is another example of macro definition.


#define PI 3.1415

main( )

float r = 6.25 ;

float area ;

area = PI * r * r ;

printf ( "\nArea of circle = %f", area ) ;

• 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.

• Compiler allows two different types of #include’s,

• first is standard library header include, syntax of which is as follows,

•#include <filename.h>

• Other type of #include compiler supports is called local include

•#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

✔ #endif is the end of #if statement

Number is Even
if will be decided at compile time, a "normal"

if will be decided at run time.


#error None of YES and NO defined!

#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

of the #pragma directives are discussed below:

#pragma startup and #pragma exit

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

You might also like