SlideShare a Scribd company logo
1 of 24
Download to read offline
C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My Mother and Father
Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.
                                     Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS PREPROCESSOR
Preprocessor
• Preprocessor processes source program before
  it is passed to compiler.

         preprocessor    Compiler


• Produce a source code file with the
  preprocessing commands properly sorted out.
Preprocessor Directives
• Preprocessor commands are known as
  directives.
• Preprocessor provides certain features.
• These features are also known as preprocessor
  directives.
• Preprocessor directives start with # sign.
        #include <stdio.h>
Preprocessor Directives...
• Preprocessor directives can be placed any
  where in the source program.
• Note: Place it at start of the program.
• Each preprocessor directive must be on it’s
  own line.
#include <stdio.h>   #include <stdio.h> #include <conio.h>
#include <conio.h>
Preprocessor Directives
•   Macro Expansion
•   File Inclusion
•   Conditional compilation
•   Miscellaneous directives
Macro Expansion
• #define directive is known as macro expansion.
• Definition:
         #define PI 3.1415
• General Form:
  – #define macro_template macro_expansion
  – #define macro_name char_sequence
Macro Expansion...
• Preprocessor search for macro definition.
• After finding #define directive it search entire
  program for macro_template.
• Replace     each     macro_template        with
  macro_expansion.
• Best Practice: Use capital letters for macro
  template.
• Do not use semicolon ‘ ; ’
Macros, Why?
• To write efficient programs.
• To increase readiabiality of programs.
• Variable vs macro_template
  – Compiler can generate faster and compact code for
    constant than it can for variables.
  – When you are dealing with a constant, why use
    variable.
  – A variable may change in the program.
Macros, Where?
• Replace operator:
      #define AND &&
      #define OR ||

• Replace condition:
      #define EXCELLENT (a>=75)
• Replace statement:
     #define ALERT printf(“Security Alert”);
Macros...
• Defined macro name can be used as a part of
  definition of other macro name.
         #define MIN 1
         #define MAX 9
         #define MIDDLE (MAX-MIN)/2

• No text substitution occur if the identifier is
  within a quoted string.
Macros with Arguments
• Macros can have arguments, same as functions
     #define ISEXCELLENT(x) (x >= 75)
     #define ISLOWER(x) (x>=97 && x<=122)

• Note: Space between Macro and it’s arguments.
           ISLOWER(x)     ISLOWER (x)
Macros with Arguments...
• Macros expansions should be enclosed within
  paranthesis.
       #define ISLOWER(x) (x>=97 && x<=122)
       if(!ISLOWER(‘a’));

• Use ‘’ to split macro in multiple line.
       #define HLINE for(i=0; i < 40; i++)
                      printf(“_”);
Macros vs Functions
           MACROS                       FUNCTIONS
Just the replacement of the   Passing arguments, doing
code.                         calculation, returning results.
                              (More serious work).
Macros make the program run   Function calls and return make
faster.                       the program slow.
Increase the program size     Make program smaller and
                              compact.
File Inclusion
• causes one file to be included in another.
       #include <filename> //OR
       #include “filename”
• <filename> : search the directory on current
  directory only.
• “filename” : search the directory on current
  directory and specified directories as specified
  in the include search path.
Why File Inclusion?
• Divide a program in multiple files.
  – Each file contains related functions.
• Some functions or macros are required in each
  program
  – Put them in a file (Library).
  – Include them in program that need them.
• Nested includes: Included file may have more
  included files in it.
Conditional Compilation
• Write single program to run on different
  environments.
  – #ifdef – if defined
  – #endif – end if
  – #else – else
  – #ifndef – in not defined
  – #if – if
  – #elif – else if
#ifdef & #endif
• General form:
      #ifdef macroname
              statement sequence
      #endif

• if macroname has been defined using #define
  the code between #ifdef & #endif will execute.
#else
• Use #else with #ifdef same as else with if.
• General-form:
        #ifdef macroname
                statement sequence
        #else
                statement sequence
        #endif
#ifndef
• #ifndef is just opposite to #ifdef.
       #ifndef __file_h
               #define __file_h

• #if directive test whether an expression
  evaluates to nonzero value or not.
• #elif used same as else if.
Where conditional compilation?
  • Instead of comments.
      – Nested comments not allowed in C.
/*                                        #ifdef BLOCK
for(i =0; i<=students;i++)                for(i =0; i<=students;i++)
            count++; /*total students*/               count++; /*total students*/
printf(“%d”,count);                       printf(“%d”,count);
*/                                        #endif
Where conditional compilation?
• Run the same code on different environment.
       #ifdef WINDOWS
               statement sequence
       #else
               statemet sequence
       #endif
Where conditional compilation?
• To avoide multiple declaration error.


                   #                 #
                        <                 <

                   >                 >
                                     #
                                          <

                                     >
To get complete benefit of this tutorial solve all the quiz on
                 www.learnbywatch.com

        For any problem in this tutorial mail me at
              yogendra@learnbywatch.com
                  with the subject “C”

               For Other information mail at
                 info@learnbywatch.com

More Related Content

What's hot

Inline function
Inline functionInline function
Inline function
Tech_MX
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 

What's hot (20)

Inline function
Inline functionInline function
Inline function
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
Modular programming
Modular programmingModular programming
Modular programming
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 

Viewers also liked

System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
Manoj Patil
 

Viewers also liked (20)

Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
 
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
 
The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessor
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
Macro
MacroMacro
Macro
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
C++ Preprocessor Directives
C++ Preprocessor DirectivesC++ Preprocessor Directives
C++ Preprocessor Directives
 
Web Project Presentation - JoinPakForces
Web Project Presentation - JoinPakForcesWeb Project Presentation - JoinPakForces
Web Project Presentation - JoinPakForces
 
Compiler
Compiler Compiler
Compiler
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
 
What is c
What is cWhat is c
What is c
 
Embedded C workshop
Embedded C workshopEmbedded C workshop
Embedded C workshop
 
C string
C stringC string
C string
 
C programming - String
C programming - StringC programming - String
C programming - String
 
String in c
String in cString in c
String in c
 
Assembler
AssemblerAssembler
Assembler
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
 
Translators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreterTranslators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreter
 

Similar to Preprocessor

05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproce
Hector Garzo
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Yasuko Ohba
 

Similar to Preprocessor (20)

C programming session6
C programming  session6C programming  session6
C programming session6
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproce
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
Chtp413
Chtp413Chtp413
Chtp413
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
Preprocesser in c
Preprocesser in cPreprocesser in c
Preprocesser in c
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdf
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
 
Python for Physical Science.pdf
Python for Physical Science.pdfPython for Physical Science.pdf
Python for Physical Science.pdf
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
 

More from Learn By Watch

More from Learn By Watch (20)

Tutorial 9 fm
Tutorial 9 fmTutorial 9 fm
Tutorial 9 fm
 
Phase modulation
Phase modulationPhase modulation
Phase modulation
 
Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detector
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detector
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong method
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator method
 
Carson's rule
Carson's ruleCarson's rule
Carson's rule
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfm
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfm
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signal
 
Angle modulation
Angle modulationAngle modulation
Angle modulation
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexing
 
Vsb modulation
Vsb modulationVsb modulation
Vsb modulation
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detector
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination method
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination method
 
Ssb modulation
Ssb modulationSsb modulation
Ssb modulation
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiver
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qam
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detector
 

Preprocessor

  • 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My Mother and Father
  • 2. Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS PREPROCESSOR
  • 3. Preprocessor • Preprocessor processes source program before it is passed to compiler. preprocessor Compiler • Produce a source code file with the preprocessing commands properly sorted out.
  • 4. Preprocessor Directives • Preprocessor commands are known as directives. • Preprocessor provides certain features. • These features are also known as preprocessor directives. • Preprocessor directives start with # sign. #include <stdio.h>
  • 5. Preprocessor Directives... • Preprocessor directives can be placed any where in the source program. • Note: Place it at start of the program. • Each preprocessor directive must be on it’s own line. #include <stdio.h> #include <stdio.h> #include <conio.h> #include <conio.h>
  • 6. Preprocessor Directives • Macro Expansion • File Inclusion • Conditional compilation • Miscellaneous directives
  • 7. Macro Expansion • #define directive is known as macro expansion. • Definition: #define PI 3.1415 • General Form: – #define macro_template macro_expansion – #define macro_name char_sequence
  • 8. Macro Expansion... • Preprocessor search for macro definition. • After finding #define directive it search entire program for macro_template. • Replace each macro_template with macro_expansion. • Best Practice: Use capital letters for macro template. • Do not use semicolon ‘ ; ’
  • 9. Macros, Why? • To write efficient programs. • To increase readiabiality of programs. • Variable vs macro_template – Compiler can generate faster and compact code for constant than it can for variables. – When you are dealing with a constant, why use variable. – A variable may change in the program.
  • 10. Macros, Where? • Replace operator: #define AND && #define OR || • Replace condition: #define EXCELLENT (a>=75) • Replace statement: #define ALERT printf(“Security Alert”);
  • 11. Macros... • Defined macro name can be used as a part of definition of other macro name. #define MIN 1 #define MAX 9 #define MIDDLE (MAX-MIN)/2 • No text substitution occur if the identifier is within a quoted string.
  • 12. Macros with Arguments • Macros can have arguments, same as functions #define ISEXCELLENT(x) (x >= 75) #define ISLOWER(x) (x>=97 && x<=122) • Note: Space between Macro and it’s arguments. ISLOWER(x) ISLOWER (x)
  • 13. Macros with Arguments... • Macros expansions should be enclosed within paranthesis. #define ISLOWER(x) (x>=97 && x<=122) if(!ISLOWER(‘a’)); • Use ‘’ to split macro in multiple line. #define HLINE for(i=0; i < 40; i++) printf(“_”);
  • 14. Macros vs Functions MACROS FUNCTIONS Just the replacement of the Passing arguments, doing code. calculation, returning results. (More serious work). Macros make the program run Function calls and return make faster. the program slow. Increase the program size Make program smaller and compact.
  • 15. File Inclusion • causes one file to be included in another. #include <filename> //OR #include “filename” • <filename> : search the directory on current directory only. • “filename” : search the directory on current directory and specified directories as specified in the include search path.
  • 16. Why File Inclusion? • Divide a program in multiple files. – Each file contains related functions. • Some functions or macros are required in each program – Put them in a file (Library). – Include them in program that need them. • Nested includes: Included file may have more included files in it.
  • 17. Conditional Compilation • Write single program to run on different environments. – #ifdef – if defined – #endif – end if – #else – else – #ifndef – in not defined – #if – if – #elif – else if
  • 18. #ifdef & #endif • General form: #ifdef macroname statement sequence #endif • if macroname has been defined using #define the code between #ifdef & #endif will execute.
  • 19. #else • Use #else with #ifdef same as else with if. • General-form: #ifdef macroname statement sequence #else statement sequence #endif
  • 20. #ifndef • #ifndef is just opposite to #ifdef. #ifndef __file_h #define __file_h • #if directive test whether an expression evaluates to nonzero value or not. • #elif used same as else if.
  • 21. Where conditional compilation? • Instead of comments. – Nested comments not allowed in C. /* #ifdef BLOCK for(i =0; i<=students;i++) for(i =0; i<=students;i++) count++; /*total students*/ count++; /*total students*/ printf(“%d”,count); printf(“%d”,count); */ #endif
  • 22. Where conditional compilation? • Run the same code on different environment. #ifdef WINDOWS statement sequence #else statemet sequence #endif
  • 23. Where conditional compilation? • To avoide multiple declaration error. # # < < > > # < >
  • 24. To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com