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

C Programming: Presented by A.M.Shyam Sundar

This document discusses C programming and the preprocessor. It introduces the preprocessor, which processes the source code before it is compiled. There are several types of preprocessor directives that begin with #, including #include, #define, #if, and #pragma. The #define directive can be used to define symbolic constants or macros. Macros allow code to be reused by substituting the macro call with its body. Nested macros contain one macro within another.

Uploaded by

shyam2289
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

C Programming: Presented by A.M.Shyam Sundar

This document discusses C programming and the preprocessor. It introduces the preprocessor, which processes the source code before it is compiled. There are several types of preprocessor directives that begin with #, including #include, #define, #if, and #pragma. The #define directive can be used to define symbolic constants or macros. Macros allow code to be reused by substituting the macro call with its body. Nested macros contain one macro within another.

Uploaded by

shyam2289
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

C PROGRAMMING

PRESENTED BY
A.M.SHYAM SUNDAR.

1
Objectives

 Introduction of preprocessor.
 Types of preprocessor directives.
 Defining a symbolic constants/macros.

2
Preprocessor

 Preprocessor is a program.

 Preprocessor processes the source program


before it is passed on to the compiler.

3
Compiling C Programs

C source code Preprocessor C Include files


files
.c files processed .h files
code

C compiler

Object code (.obj file)


Machine code .exe file
.obj files Executable
Libraries Linker
CM304.87 code 4
Preprocessor directives

 Collection of special statements that are called


directives.
 Executed by the pre-processor.
 Occurs before a program is compiled.
 Begin with #.
 Don’t end with semicolon.
 Can be placed anywhere in the program.
 Normally placed at the beginning of the program
or before any particular function.
CM304.87 5
Advantages

Easy to

 Develop program
 Read programs
 Modify programs
 Portable
 Reuse
6
Types of preprocessor directives

They are 12 preprocessor directives


1) #include
2) #define
3) #if
4) #ifdef
5) #ifndef
6) #else

CM304.87 7
Types of preprocessor directives
Contd..

7) #elif
8) #endif
9) #undef
10) #error
11) #line
12) #pragma

8
Types of preprocessor directives

Preprocessor Meaning
Directive(s)
#include Treats text in the file specified by filename as if it
appeared in the current file.
#define Defines a macro/Symbolic constant.
#if , #ifdef, #ifndef, #else, Conditional compilation directives.

#elif, and #endif


#undef Undefines a symbol.

#error Issues error message.

#line Causes the compiler to think that the line number of


the next source line is given by <constant>, and the
current input file is given by <identifier>.
#pragma Implementation-specific directives.

9
The #define Preprocessor Directive

 1)Symbolic Constants

 To define symbolic constants.


Symbolic constant
Syntax:  #define identifier replacementvalue

 When program is compiled, all occurrences of


symbolic constant will be replaced with

replacement value.

CM304.87 10
The #define Preprocessor Directive

Example: #define PI 3.14159

  #define FALSE 0

To undefine a macro

E.g. #undef FALSE

 A macro must be undefined before being


redefined to a different value.
CM304.87 11
Example

CM304.87 12
The # define preprocessor directive

2)Macros
 #define IDENTIFIER(arg1,agr2,….agrn) body
 agr1,agr2…agrn are arguments, body is the
body of the macro.
 A macro without arguments is treated like a
symbolic constant.
 When the macro is called, the call is replaced
by its body.
 Well suited for small functions and functions
are called frequently.
13
The # define preprocessor directive
Contd..

Function Macro
Function call is nothing but Macro call is nothing but
branching. substitution.
Program execution is slow. Program execution is fast.

Processing is done by Processing is done by


CPU. preprocessor.
Program size is not Program size is increased.
increased.
CM304.87 14
The # define preprocessor directive
Contd..

Example
body
Name argument

 #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )
 Calling Macro
 area = CIRCLE_AREA( 4 );
to become
 area = ( 3.14159 * ( 4 ) * ( 4 ) );

15
Example

CM304.87 16
Nested Macros

 Macro within Macro is called as


Nested Macro.

CM304.87 17
Example

CM304.87 18
An optimist is a person who sees a green light everywhere.
The pessimist sees only the red light. But the truly wise
person is color blind.

THANK YOU.

19

You might also like