+ All Categories
Home > Education > CS201- Introduction to Programming- Lecture 23

CS201- Introduction to Programming- Lecture 23

Date post: 18-Dec-2014
Category:
Upload: bilal-ahmed
View: 12 times
Download: 1 times
Share this document with a friend
Description:
Virtual University Course CS201- Introduction to Programming Lecture No 23 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Popular Tags:
37
Introduction to Programming Introduction to Programming Lecture 23 Lecture 23 Preprocessors and Preprocessors and Header files Header files
Transcript
Page 1: CS201- Introduction to Programming- Lecture 23

Introduction to Introduction to ProgrammingProgramming

Lecture 23Lecture 23

Preprocessors and Preprocessors and Header filesHeader files

Page 2: CS201- Introduction to Programming- Lecture 23

Today’s LectureToday’s Lecture

Preprocessor DirectivesPreprocessor Directives Header FilesHeader Files MacrosMacros

Page 3: CS201- Introduction to Programming- Lecture 23

PreprocessPreprocessoror

Page 4: CS201- Introduction to Programming- Lecture 23

#include <iostream.h>#include <iostream.h>

Page 5: CS201- Introduction to Programming- Lecture 23

##

Page 6: CS201- Introduction to Programming- Lecture 23

#include#include #include <iostream.h>#include <iostream.h> #include <stdlib.h>#include <stdlib.h> #include <fstream.h>#include <fstream.h> #include <stream.h>#include <stream.h>

Page 7: CS201- Introduction to Programming- Lecture 23

5:10 to 5:23

Page 8: CS201- Introduction to Programming- Lecture 23

5:24 to 5:41

Page 9: CS201- Introduction to Programming- Lecture 23

Re-locatable Re-locatable ExecutableExecutable

Page 10: CS201- Introduction to Programming- Lecture 23

MyHeader.MyHeader.hh

Page 11: CS201- Introduction to Programming- Lecture 23

#include “MyHeader.h”#include “MyHeader.h”

Page 12: CS201- Introduction to Programming- Lecture 23

#include #include <iostream.h><iostream.h>

Page 13: CS201- Introduction to Programming- Lecture 23
Page 14: CS201- Introduction to Programming- Lecture 23

#define#define

Page 15: CS201- Introduction to Programming- Lecture 23

#define PI #define PI 3.1415923.141592

Page 16: CS201- Introduction to Programming- Lecture 23

Preprocessor Preprocessor DirectivesDirectives #if#if #else#else #endif#endif #elif#elif #ifdef#ifdef #ifndef#ifndef #error#error

Page 17: CS201- Introduction to Programming- Lecture 23

Preprocessor Preprocessor DirectivesDirectives

#ident#ident #import#import #line#line #machine#machine #system#system #warning#warning

Page 18: CS201- Introduction to Programming- Lecture 23

Example Example

#ifdef PI#ifdef PI

Page 19: CS201- Introduction to Programming- Lecture 23

# define PI# define PI

# ifdef PI# ifdef PI

Page 20: CS201- Introduction to Programming- Lecture 23

# define DEBUG# define DEBUG

# ifdef DEBUG# ifdef DEBUG

Page 21: CS201- Introduction to Programming- Lecture 23

Conditional Conditional CompilationCompilation

Macro Macro TranslationTranslation

Page 22: CS201- Introduction to Programming- Lecture 23

#undef #undef DEBUGDEBUG

Page 23: CS201- Introduction to Programming- Lecture 23

#include #include <conio.h><conio.h>

Consol Input Output Consol Input Output

   

 

Page 24: CS201- Introduction to Programming- Lecture 23

#include #include <conio.h><conio.h>#include #include

<conio.c><conio.c>

Page 25: CS201- Introduction to Programming- Lecture 23

int getche int getche ( ) ;( ) ;

Page 26: CS201- Introduction to Programming- Lecture 23

Get Character Get Character With EchoWith Echo

Page 27: CS201- Introduction to Programming- Lecture 23

#ifdef __cplusplus#ifdef __cplusplusextern "C" {extern "C" {#endif#endif

- -- -- -- -

#ifdef __cplusplus#ifdef __cplusplus}}#endif#endif

Page 28: CS201- Introduction to Programming- Lecture 23

MacroMacro

Page 29: CS201- Introduction to Programming- Lecture 23

#define SQUARE ( X ) X * X#define SQUARE ( X ) X * Xmain ( )main ( ){{

int i = 5 , j ;int i = 5 , j ; ::

}}

Example 1Example 1

j =j =SQUARE ( i ) ; SQUARE ( i ) ; i * i ;i * i ;

Page 30: CS201- Introduction to Programming- Lecture 23

Example 2 Example 2 ##define SQUARE ( X ) X * Xdefine SQUARE ( X ) X * Xmain ( )main ( ){{

int i = 5 , j = 10 , k ;int i = 5 , j = 10 , k ;::

k =k =}}

SQUARE ( i + j ) ; SQUARE ( i + j ) ; i + j * i + j ;i + j * i + j ;

Page 31: CS201- Introduction to Programming- Lecture 23

Example 3Example 3#define SQUARE(X) #define SQUARE(X)

(X)*(X)(X)*(X)

main ( )main ( )

{{

int i = 5 , j = 10 , k ;int i = 5 , j = 10 , k ;

k =k =

}}

SQUARE ( i + j ) ;SQUARE ( i + j ) ;( i + j ) * ( i + j ) ;( i + j ) * ( i + j ) ;

Page 32: CS201- Introduction to Programming- Lecture 23

OverheadOverhead

Page 33: CS201- Introduction to Programming- Lecture 23

Code BloatCode Bloat

Page 34: CS201- Introduction to Programming- Lecture 23

Example 4Example 4#define PI 3.14159#define PI 3.14159#define CIRCLEAREA ( X )#define CIRCLEAREA ( X )main ( )main ( ){{

float radius ;float radius ;cout << “ Enter radius of Circle Area : ” ;cout << “ Enter radius of Circle Area : ” ;cin >> radius ;cin >> radius ;cout << “ Area of Circle Area is ” cout << “ Area of Circle Area is ”

<<<<

}}

PI * X * XPI * X * X( ( PI ) * ( X ) * ( X ) )( ( PI ) * ( X ) * ( X ) )

CIRCLEAREA ( radius ) ;CIRCLEAREA ( radius ) ;( ( PI ) * ( radius ) * ( radius ) ) ;( ( PI ) * ( radius ) * ( radius ) ) ;CIRCLEAREA ( 2 * radius ) ;CIRCLEAREA ( 2 * radius ) ;

Page 35: CS201- Introduction to Programming- Lecture 23

Header FilesHeader Files

Page 36: CS201- Introduction to Programming- Lecture 23

Header FileHeader File #include #include <iostream.h><iostream.h>

#include <stdlib.h>#include <stdlib.h> #include <stdio.h>#include <stdio.h> #include <string.h>#include <string.h>

Page 37: CS201- Introduction to Programming- Lecture 23

In Next LectureIn Next Lecture

Memory AllocationMemory Allocation


Recommended