[go: up one dir, main page]

0% found this document useful (0 votes)
11 views17 pages

Preprocessor in C

The document provides an overview of the C preprocessor, including its functions such as macro definitions and header file inclusions. It explains various storage classes in C, including auto, register, static, and extern, detailing their scope, lifetime, and usage. Additionally, it includes code examples to illustrate the concepts discussed.

Uploaded by

aman09052004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

Preprocessor in C

The document provides an overview of the C preprocessor, including its functions such as macro definitions and header file inclusions. It explains various storage classes in C, including auto, register, static, and extern, detailing their scope, lifetime, and usage. Additionally, it includes code examples to illustrate the concepts discussed.

Uploaded by

aman09052004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

• The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your

program before it is compiled.


• These transformations can be the inclusion of header file, macro expansions etc.
• All preprocessing directives begin with a # symbol. For example

# define PI 3.14

Some of the common uses of C preprocessor are:

Including Header Files: #include

#include <stdio.h>
Here stdio.h is a header file. The #include pre-processor directive replaces the above line
with the content of stdio.h header file.

Thats the reason you need to include header file before to use the function like scanf() and
printf().

1
Macros using #define

A macro is a fragment of code that is given a name. You can define a macro in C using the #define
preprocessor directive.

# define c 29979

Here, when we use c in our program it will be replaced by 29979.

Function like Macros

You can also define macros that work in a similar way like a function call. This is known as function-like
macros. For example,
#define circleArea(r) (3.1415*(r)*(r))

Every time the program encounters circleArea(argument), it is replaced by


(3.1415*(argument)*(argument)).

Suppose, we passed 5 as an argument then, it expands as below:


circleArea(5) expands to (3.1415*5*5)
2
Example:

#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)

int main() {
float radius, area;

printf("Enter the radius: ");


scanf("%f", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);

return 0;
}

3
4
5
#include <stdio.h>
int main()
{
printf("Current time: %s",__TIME__);
printf("\nFile is %s",__FILE__);
printf("\nFile is %s",__DATE__);
printf("\nFile is %d",__STDC__);
printf("\n File is %d",__LINE__);
}

Current time: 23:50:03


File is
/usr/share/IDE_PROGRAMS/C/other/698a5a2b68da57910db370a5d8d8f6b6/698a5a2b68d
a57910db370a5d8d8f6b6.c
File is Mar 2 2021
File is 1
File is 8

6
Storage Type in C:
• A storage class defines the scope (visibility) and
life-time of variables and/or functions within a C
Program. They precede the type that they modify. We
have four different storage classes in a C program −
• auto
• register
• static
• extern

7
The auto Storage Class
• The auto storage class is the default storage class for all local
variables.
• The variables defined using auto storage class are called as local
variables. Auto stands for automatic storage class. A variable is in
auto storage class by default if it is not explicitly specified.
• The scope of an auto variable is limited with the particular block
only. Once the control goes out of the block, the access is
destroyed. This means only the block in which the auto variable is
declared can access it.
• A keyword auto is used to define an auto storage class. By
default, an auto variable contains a garbage value.

• The example above defines two variables with in the same


storage class. 'auto' can only be used within functions, i.e., local
variables

8
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j); Output:
} 3 21
printf( "%d\n", j);}

9
Register Storage Class
• You can use the register storage class when you want to store
local variables within functions or blocks in CPU registers instead
of RAM to have quick access to these variables.
• The keyword register is used to declare a register storage class.
The variables declared using register storage class has lifespan
throughout the program.
• Register storage class are stored inside CPU registers instead of a
memory. Register has faster access than that of the main
memory.
• The variables declared using register storage class has no default
value. These variables are often declared at the beginning of a
program.

10
#include <stdio.h> /* function declaration */
main() {
{
register int weight;
int *ptr=&weight ;

/*it produces an error when the compilation occurs ,we cannot get a memory location when
dealing with CPU register*/}
}

11
Static Storage Class in C
• The static variables are used within function/ file as local static
variables. They can also be used as a global variable
• Keep in mind that static variable has a default initial value zero
and is initialized only once in its lifetime.
• Global variables are accessible throughout the file whereas static
variables are accessible only to the particular part of a code.

12
#include <stdio.h>
void next();
static int counter = 7; /* global variable */
int main()
{
while(counter<10)
{
next(); Output:
counter++; iteration=14 and counter= 7
} iteration=15 and counter= 8
return 0; iteration=16 and counter= 9

}
void next( )
{ /* function definition */
static int iteration = 13; /* local static variable
*/
iteration ++;
printf("iteration=%d and counter= %d\n",
iteration, counter);

}
13
Extern Storage Class in C
• Extern storage class is used when we have global functions or
variables which are shared between two or more files.
• Keyword extern is used to declaring a global variable or function
in another file to provide the reference of variable or function
which have been already defined in the original file.
• The variables defined using an extern keyword are called as
global variables. These variables are accessible throughout the
program. Notice that the extern variable cannot be initialized it
has already been defined in the original file.

14
Main.c

#include <stdio.h>
extern i;
main() {
printf("value of the external integer is = %d\n", i);
return 0;}

Orig.c
#include <stdio.h>
i=48;

15
int fun1(int n) {
static int i= 0;
if (n > 0) {
The return value of fun2(5) is
++i;
fun1(n-1);
}
return (i);
}
int fun2(int n) {
static int i= 0;
Output: 55
if (n>0) {
i = i+ fun1 (n) ;
fun2(n-1) ;
}
return (i);
}

16
17

You might also like