1 of 17

1

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

​

2 of 17

2

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)

​

​

3 of 17

3

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

}

Example:

4 of 17

4

5 of 17

5

6 of 17

6

#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/698a5a2b68da57910db370a5d8d8f6b6.c

File is Mar 2 2021

File is 1

File is 8

7 of 17

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

8 of 17

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

9 of 17

9

#include <stdio.h>

int main( )

{

auto int j = 1;

{

auto int j= 2;

{

auto int j = 3;

printf ( " %d ", j);

}

printf ( "\t %d ",j);

}

printf( "%d\n", j);}

Output:

3 2 1

10 of 17

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

11 of 17

11

#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*/}

}

12 of 17

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

13 of 17

13

#include <stdio.h>

void next();

static int counter = 7; /* global variable */

int main()

{

while(counter<10)

{

next();

counter++;

}

return 0;

}

void next( )

{ /* function definition */

static int iteration = 13; /* local static variable */

iteration ++;

printf("iteration=%d and counter= %d\n", iteration, counter);

}

Output:

iteration=14 and counter= 7

iteration=15 and counter= 8

iteration=16 and counter= 9

14 of 17

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

15 of 17

15

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;

16 of 17

16

int fun1(int n) {

static int i= 0;

if (n > 0) {

++i;

fun1(n-1);

}

return (i);

}

int fun2(int n) {

static int i= 0;

if (n>0) {

i = i+ fun1 (n) ;

fun2(n-1) ;

}

return (i);

}

The return value of fun2(5) is

Output: 55

17 of 17

17