CSPC-COMPUTER SYSTEMS
PROGRAMMING IN ‘C’
ANKUR SRIVASTAVA
DEPARTMENT OF COMPUTER SCIENCE
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
1
POINTERS: INTRODUCTION, DECLARATION, APPLICATIONS FILE HANDLING.
STANDARD C PREPROCESSORS, DEFINING AND CALLING MACROS.
CONDITIONAL COMPILATION, PASSING VALUES TO THE COMPILER.
 UNIT-5 TOPICS
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
2
UNIT-5 POINTERS
• In a generic sense, a “pointer” is anything that tells us where
something can be found.
– Addresses in the phone book
– URLs for webpages
-- Road signs
What actually ptr is?
 ptr is a variable storing an address
 ptr is NOT storing the actual value of i
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
3
EXAMPLE OF POINTER
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
4
 int i = 5; ptr
 int *ptr;
 ptr = &i;
 printf(“i = %dn”, i);
 printf(“*ptr = %dn”, *ptr);
 printf(“ptr = %pn”, ptr);
Output:
i = 5
*ptr = 5
ptr = effff5e0
address of i
5i
value of ptr
= address of
i
in memory
POINTER APPLICATIONS IN C PROGRAMMING
Passing Parameter by Reference
Accessing Array element
Dynamic Memory Allocation :
Reducing size of parameter
Some other pointer applications :
--Passing Strings to function
--Provides effective way of implementing the different data
structures such as tree, graph, linked list
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
5
FILE HANDLING: GOALS
By the end of this unit we should understand …
 … how to open a file to write to it.
 … how to open a file to read from it.
 … how to open a file to append data to it.
 … how to read strings from a file.
 … how to write strings to a file.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
6
DEFINITION
 A file is a collection of related data that a computers treats as a
single unit.
 Computers store files to secondary storage so that the contents
of files remain intact when a computer shuts down.
 When a computer reads a file, it copies the file from the storage
device to memory; when it writes to a file, it transfers data from
memory to the storage device.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
7
OPERATIONS ON FILE
 Opening a file
 Reading data from a file
 Writing data to a file
 Closing a file
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
8
OPENING A FILE
 A file must be “opened” before it can be used.
FILE *fp;
:
fp = fopen (filename, mode);
 fp is declared as a pointer to the data type FILE.
 filename is a string - specifies the name of the file.
 fopen returns a pointer to the file which is used in all
subsequent file operations.
 mode is a string which specifies the purpose of opening the
file:
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
9
EXAMPLES
““r”r” :: open the file for reading only
““w”w” :: open the file for writing only
““a”a” :: open the file for appending data to it.
FILE *in, *out ;
in = fopen (“mydata.dat”, “r”) ;
out = fopen (“result.dat”, “w”);
FILE *empl ;
char filename[25];
scanf (“%s”, filename);
empl = fopen (filename, “r”) ;
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
10
CLOSING A FILE
 After all operations on a file have been completed, it must be closed.
 Ensures that all file data stored in memory buffers are properly written to
the file.
 General format: fclose (file_pointer) ;
FILE *xyz ;
xyz = fopen (“test”, “w”) ;
…….
fclose (xyz) ;
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
11
STANDARD C PREPROCESSORS
 The C preprocessor executes before a program is compiled.
 Some actions it performs are the inclusion of other files in the file being compiled,
definition of symbolic constants and macros, conditional compilation of program code
and conditional execution of preprocessor directives.
 Preprocessor directives begin with # and only white-space characters and comments
may appear before a preprocessor directive on a line.
 Macro definition
 #define, #undef
 File inclusion
 #include
 Conditional Compilation
 #if, #ifdef, #ifndef, #elseif, #else
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
12
PREPROCESSOR DIRECTIVES
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
13
#INCLUDE PREPROCESSOR DIRECTIVE
 The #include preprocessor directive has been used
throughout this text.
 The #include directive causes a copy of a specified file to
be included in place of the directive.
 The two forms of the #include directive are:
 #include <filename>
#include "filename"
 The difference between these is the location the preprocessor
begins searches for the file to be included.
 If the file name is enclosed in quotes, the preprocessor starts
searches in the same directory as the file being compiled for
the file to be included (and may search other locations, too).
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
14
#DEFINE PREPROCESSOR DIRECTIVE: SYMBOLIC
CONSTANTS
 The #define directive creates symbolic constants—constants represented as symbols—and
macros—operations defined as symbols.
 The #define directive format is
 #define identifier replacement-text
 When this line appears in a file, all subsequent occurrences of identifier that do not appear in
string literals will be replaced by replacement-text automatically before the program is compiled.
 Consider the following macro definition with one argument for the area of a circle:
 #define CIRCLE_AREA ( x ) ( ( PI ) * ( x ) * ( x ) )
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
15
#ERROR AND #PRAGMA PREPROCESSOR DIRECTIVES:
 The #error directive
 #error tokens
prints an implementation-dependent message including the tokens
specified in the directive.
 The tokens are sequences of characters separated by spaces.
 For example,
 #error 1 - Out of range error
contains 6 tokens.
 When a #error directive is processed on some systems, the tokens in
the directive are displayed as an error message, preprocessing stops and
the program does not compile.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
16
PREPROCESSOR DIRECTIVES
Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for a macro definition
#endif Specifies the end of #if
#ifndef Tests whether a macro is not defined
#if Test a compile time condition
#else Specifies alternatives when #if test fails
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
17## token pasting It combines two arguments
TYPES OF MACROS
 Macro substitution is a process where an identifier in a program is
replaced by a predefined string composed of one or more tokens.
 These directives can be divided into three categories:
 Macro substitution directives
 File inclusion directives
 Compiler control directives
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
18
Simple MS
Argumented MS
Nested MS
MACRO SUBSTITUTION
 General form:
#define identifier string
1. SIMPLE MS:
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
19
MACRO IDENTIFIER STRING
#define COUNT 200
#define FALSE 0
#define SUBJECTS 5
#define PI 3.1415
#define CAPITAL “LUCKNOW”
MACRO SUBSTITUTION
 2. Argumented MS
 The preprocessor permits us to define more complex & more useful
form of replacements.
 It takes the form:
#define identifier (f1, f2,………….fn) string
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
20
MACRO IDENTIFIER STRING
#define MAX(a, b) (((a)>(b))?(a):(b))
#define MIN (a, b) (((a)<(b))?(a):(b))
#define ABS(x) (((x)>(0))?(x):(-x))
#define STREQ(s1, s2) (strcmp((s1,)(s2))==0)
#define STRGT(s1, s2) (strcmp((s1,)(s2))>0)
MACRO SUBSTITUTION
 Nested MS:
We can also use one macro in the definition of another macro.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
21
MACRO IDENTIFIER STRING
#define M 5
#define N M+1
#define SQUARE(x) ((x)* (x))
#define CUBE(x) (SQUARE (x) *(x))
#define SIXTH(x) (CUBE (x) * CUBE (x))
CONDITIONAL COMPILATION:
 Conditional compilation enables you to control the execution of
preprocessor directives and the compilation of program code.
 Each of the conditional preprocessor directives evaluates a constant
integer expression.
 Cast expressions, sizeof expressions and enumeration constants cannot
be evaluated in preprocessor directives.
 The conditional preprocessor construct is much like the if selection
statement.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
22
CONDITIONAL COMPILATION
 Structure similar to if
#if !defined( NULL )
#define NULL 0
#endif
 Every #if must end with #endif
 #ifdef short for #if defined( name )
 #ifndef short for #if !defined( name )
 Other statements
 #elif – equivalent of else if in an if structure
 #else – equivalent of else in an if structure
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
23
PASSING VALUES TO THE COMPILER
CALL BY VALUE CALL BY REFERENCE
Formal parameters are local variables
to callee.
Values of the actual parameters are
copied to the formal parameters when
function is called.
Conceptually simple.
Values of actual parameters are
protected.
Costly copying if parameters are large.
Can only return 1 value.
Formal parameters are pointers to
actual parameters.
Every use of the formal parameter is
implicitly dereferenced.
Actual parameters must have l-values.
(If not, either prohibit, or create
temporary unnamed variable in
caller).
Aliasing problem.
Can’t use in remote procedure call.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
24
CALL BY VALUE & CALL BY REFERENCE
08/11/18
ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
25
#include<stdio.h>
void interchange(int number1, int number2)
{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
}
int main() {
int num1 = 50, num2 = 70;
interchange(num1, num2);
printf(“n Number 1 : %d”, num1);
printf(“n Number 2 : %d”,num2);
return(0); }
OUTPUT
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
26
Number 1 : 50
Number 2 : 70
FILE HANDLING FUNCTIONS
File handling functions Description
fopen () fopen () function creates a new file or opens an existing file.
fclose () fclose () function closes an opened file.
getw () getw () function reads an integer from file.
putw () putw () functions writes an integer to file.
fgetc () fgetc () function reads a character from file.
fputc () fputc () functions write a character to file.
gets () gets () function reads line from keyboard.
puts () puts () function writes line to o/p screen.
fgets () fgets () function reads string from a file, one line at a time.
fputs () fputs () function writes string to a file.
feof () feof () function finds end of file.
fgetchar () fgetchar () function reads a character from keyboard. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
27
File handling functions Description
fprintf () fprintf () function writes formatted data to a file.
fscanf () fscanf () function reads formatted data from a file.
fputchar ()
fputchar () function writes a character onto the output screen
from keyboard input.
fseek () fseek () function moves file pointer position to given location.
SEEK_SET
SEEK_SET moves file pointer position to the beginning of the
file.
SEEK_CUR SEEK_CUR moves file pointer position to given location.
SEEK_END SEEK_END moves file pointer position to the end of file.
ftell () ftell () function gives current position of file pointer.
rewind ()
rewind () function moves file pointer position to the beginning
of the file.
fflush () fflush () function flushes a file.
08/11/18
ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
28
getc () getc () function reads character from file.
getch () getch () function reads character from keyboard.
getche ()
getche () function reads character from keyboard and echoes to
o/p screen.
getchar () getchar () function reads character from keyboard.
putc () putc () function writes a character to file.
putchar () putchar () function writes a character to screen.
printf () printf () function writes formatted data to screen.
sprinf () sprinf () function writes formatted output to string.
scanf () scanf () function reads formatted data from keyboard.
remove () remove () function deletes a file. 08/11/18
ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
29

More Related Content

PPTX
7 compiler lab
PPTX
4 compiler lab - Syntax Ana
PPTX
Compiler Engineering Lab#1
PPTX
KEY
Unit 1 cd
PDF
Cd lab manual
PDF
Cs6660 compiler design
7 compiler lab
4 compiler lab - Syntax Ana
Compiler Engineering Lab#1
Unit 1 cd
Cd lab manual
Cs6660 compiler design

What's hot (20)

PDF
Declare Your Language: What is a Compiler?
PPTX
Structure of the compiler
PPTX
Compiler Construction
PPTX
Compiler Design
PDF
Different phases of a compiler
PPT
Compiler Design Tutorial
PPTX
Flex (fast lexical analyzer generator )
DOC
Lex tool manual
PDF
Lecture2 general structure of a compiler
PPT
Lexical Analyzers and Parsers
PPTX
Phases of compiler
PDF
Compilers Design
PPT
Lexical Analysis
PDF
Principles of compiler design
PPTX
Error detection recovery
PPTX
Compiler Engineering Lab#3
PDF
Compiler Design Lecture Notes
PPTX
System Programming Unit III
PPT
Chap 1-language processor
Declare Your Language: What is a Compiler?
Structure of the compiler
Compiler Construction
Compiler Design
Different phases of a compiler
Compiler Design Tutorial
Flex (fast lexical analyzer generator )
Lex tool manual
Lecture2 general structure of a compiler
Lexical Analyzers and Parsers
Phases of compiler
Compilers Design
Lexical Analysis
Principles of compiler design
Error detection recovery
Compiler Engineering Lab#3
Compiler Design Lecture Notes
System Programming Unit III
Chap 1-language processor
Ad

Similar to Unit 5 cspc (20)

PPTX
Preprocessor
PPTX
Preprocessor directives in c laguage
PPTX
Preprocessor directives in c language
PPTX
UNIT 4A-preprocessor.pptx for c language and basic knowledge
PPT
PreProcessorDirective.ppt
PDF
6 preprocessor macro header
DOCX
Unit 5 quesn b ans5
PPTX
PDF
Introduction to Preprocessors
PDF
ANSI C Macros
PDF
Unit 5 quesn b ans5
PDF
05 -working_with_the_preproce
PDF
Unit 5 Part 1 Macros
PDF
Module 05 Preprocessor and Macros in C
PPTX
1 - Preprocessor.pptx
PPTX
Preprocessor
PDF
C Programming Tutorial - www.infomtec.com
PPTX
introduction of c langauge(I unit)
PDF
Preprocessor
Preprocessor
Preprocessor directives in c laguage
Preprocessor directives in c language
UNIT 4A-preprocessor.pptx for c language and basic knowledge
PreProcessorDirective.ppt
6 preprocessor macro header
Unit 5 quesn b ans5
Introduction to Preprocessors
ANSI C Macros
Unit 5 quesn b ans5
05 -working_with_the_preproce
Unit 5 Part 1 Macros
Module 05 Preprocessor and Macros in C
1 - Preprocessor.pptx
Preprocessor
C Programming Tutorial - www.infomtec.com
introduction of c langauge(I unit)
Preprocessor
Ad

More from BBDITM LUCKNOW (18)

PPT
Unit 4 cspc
PPT
Unit3 cspc
PPT
Cse ppt 2018
PPT
Binary system ppt
PPT
Unit 4 ca-input-output
PPTX
Unit 3 ca-memory
PPT
Unit 2 ca- control unit
PPTX
Unit 1 ca-introduction
PPT
Bnf and ambiquity
PPT
Minimization of dfa
PPTX
Passescd
PDF
Compiler unit 4
PDF
Compiler unit 2&3
PDF
Compiler unit 1
PDF
Compiler unit 5
PDF
Cspc final
PPT
Cd2 [autosaved]
PPTX
Validation based protocol
Unit 4 cspc
Unit3 cspc
Cse ppt 2018
Binary system ppt
Unit 4 ca-input-output
Unit 3 ca-memory
Unit 2 ca- control unit
Unit 1 ca-introduction
Bnf and ambiquity
Minimization of dfa
Passescd
Compiler unit 4
Compiler unit 2&3
Compiler unit 1
Compiler unit 5
Cspc final
Cd2 [autosaved]
Validation based protocol

Recently uploaded (20)

PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
Project_Mgmt_Institute_-Marc Marc Marc .pdf
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PPTX
Wireless sensor networks (WSN) SRM unit 2
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PDF
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
PPTX
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
PPT
Programmable Logic Controller PLC and Industrial Automation
PPTX
Solar energy pdf of gitam songa hemant k
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
PDF
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
PDF
Cryptography and Network Security-Module-I.pdf
PPTX
Micro1New.ppt.pptx the mai themes of micfrobiology
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PPTX
Principal presentation for NAAC (1).pptx
PPTX
Agentic Artificial Intelligence (Agentic AI).pptx
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Project_Mgmt_Institute_-Marc Marc Marc .pdf
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Wireless sensor networks (WSN) SRM unit 2
MLpara ingenieira CIVIL, meca Y AMBIENTAL
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
distributed database system" (DDBS) is often used to refer to both the distri...
MAD Unit - 3 User Interface and Data Management (Diploma IT)
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
Programmable Logic Controller PLC and Industrial Automation
Solar energy pdf of gitam songa hemant k
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
Cryptography and Network Security-Module-I.pdf
Micro1New.ppt.pptx the mai themes of micfrobiology
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Principal presentation for NAAC (1).pptx
Agentic Artificial Intelligence (Agentic AI).pptx

Unit 5 cspc

  • 1. CSPC-COMPUTER SYSTEMS PROGRAMMING IN ‘C’ ANKUR SRIVASTAVA DEPARTMENT OF COMPUTER SCIENCE 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 1
  • 2. POINTERS: INTRODUCTION, DECLARATION, APPLICATIONS FILE HANDLING. STANDARD C PREPROCESSORS, DEFINING AND CALLING MACROS. CONDITIONAL COMPILATION, PASSING VALUES TO THE COMPILER.  UNIT-5 TOPICS 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 2
  • 3. UNIT-5 POINTERS • In a generic sense, a “pointer” is anything that tells us where something can be found. – Addresses in the phone book – URLs for webpages -- Road signs What actually ptr is?  ptr is a variable storing an address  ptr is NOT storing the actual value of i 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 3
  • 4. EXAMPLE OF POINTER 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 4  int i = 5; ptr  int *ptr;  ptr = &i;  printf(“i = %dn”, i);  printf(“*ptr = %dn”, *ptr);  printf(“ptr = %pn”, ptr); Output: i = 5 *ptr = 5 ptr = effff5e0 address of i 5i value of ptr = address of i in memory
  • 5. POINTER APPLICATIONS IN C PROGRAMMING Passing Parameter by Reference Accessing Array element Dynamic Memory Allocation : Reducing size of parameter Some other pointer applications : --Passing Strings to function --Provides effective way of implementing the different data structures such as tree, graph, linked list 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 5
  • 6. FILE HANDLING: GOALS By the end of this unit we should understand …  … how to open a file to write to it.  … how to open a file to read from it.  … how to open a file to append data to it.  … how to read strings from a file.  … how to write strings to a file. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 6
  • 7. DEFINITION  A file is a collection of related data that a computers treats as a single unit.  Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down.  When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 7
  • 8. OPERATIONS ON FILE  Opening a file  Reading data from a file  Writing data to a file  Closing a file 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 8
  • 9. OPENING A FILE  A file must be “opened” before it can be used. FILE *fp; : fp = fopen (filename, mode);  fp is declared as a pointer to the data type FILE.  filename is a string - specifies the name of the file.  fopen returns a pointer to the file which is used in all subsequent file operations.  mode is a string which specifies the purpose of opening the file: 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 9
  • 10. EXAMPLES ““r”r” :: open the file for reading only ““w”w” :: open the file for writing only ““a”a” :: open the file for appending data to it. FILE *in, *out ; in = fopen (“mydata.dat”, “r”) ; out = fopen (“result.dat”, “w”); FILE *empl ; char filename[25]; scanf (“%s”, filename); empl = fopen (filename, “r”) ; 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 10
  • 11. CLOSING A FILE  After all operations on a file have been completed, it must be closed.  Ensures that all file data stored in memory buffers are properly written to the file.  General format: fclose (file_pointer) ; FILE *xyz ; xyz = fopen (“test”, “w”) ; ……. fclose (xyz) ; 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 11
  • 12. STANDARD C PREPROCESSORS  The C preprocessor executes before a program is compiled.  Some actions it performs are the inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives.  Preprocessor directives begin with # and only white-space characters and comments may appear before a preprocessor directive on a line.  Macro definition  #define, #undef  File inclusion  #include  Conditional Compilation  #if, #ifdef, #ifndef, #elseif, #else 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 12
  • 13. PREPROCESSOR DIRECTIVES 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 13
  • 14. #INCLUDE PREPROCESSOR DIRECTIVE  The #include preprocessor directive has been used throughout this text.  The #include directive causes a copy of a specified file to be included in place of the directive.  The two forms of the #include directive are:  #include <filename> #include "filename"  The difference between these is the location the preprocessor begins searches for the file to be included.  If the file name is enclosed in quotes, the preprocessor starts searches in the same directory as the file being compiled for the file to be included (and may search other locations, too). 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 14
  • 15. #DEFINE PREPROCESSOR DIRECTIVE: SYMBOLIC CONSTANTS  The #define directive creates symbolic constants—constants represented as symbols—and macros—operations defined as symbols.  The #define directive format is  #define identifier replacement-text  When this line appears in a file, all subsequent occurrences of identifier that do not appear in string literals will be replaced by replacement-text automatically before the program is compiled.  Consider the following macro definition with one argument for the area of a circle:  #define CIRCLE_AREA ( x ) ( ( PI ) * ( x ) * ( x ) ) 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 15
  • 16. #ERROR AND #PRAGMA PREPROCESSOR DIRECTIVES:  The #error directive  #error tokens prints an implementation-dependent message including the tokens specified in the directive.  The tokens are sequences of characters separated by spaces.  For example,  #error 1 - Out of range error contains 6 tokens.  When a #error directive is processed on some systems, the tokens in the directive are displayed as an error message, preprocessing stops and the program does not compile. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 16
  • 17. PREPROCESSOR DIRECTIVES Directive Function #define Defines a macro substitution #undef Undefines a macro #include Specifies the files to be included #ifdef Test for a macro definition #endif Specifies the end of #if #ifndef Tests whether a macro is not defined #if Test a compile time condition #else Specifies alternatives when #if test fails 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 17## token pasting It combines two arguments
  • 18. TYPES OF MACROS  Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens.  These directives can be divided into three categories:  Macro substitution directives  File inclusion directives  Compiler control directives 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 18 Simple MS Argumented MS Nested MS
  • 19. MACRO SUBSTITUTION  General form: #define identifier string 1. SIMPLE MS: 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 19 MACRO IDENTIFIER STRING #define COUNT 200 #define FALSE 0 #define SUBJECTS 5 #define PI 3.1415 #define CAPITAL “LUCKNOW”
  • 20. MACRO SUBSTITUTION  2. Argumented MS  The preprocessor permits us to define more complex & more useful form of replacements.  It takes the form: #define identifier (f1, f2,………….fn) string 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 20 MACRO IDENTIFIER STRING #define MAX(a, b) (((a)>(b))?(a):(b)) #define MIN (a, b) (((a)<(b))?(a):(b)) #define ABS(x) (((x)>(0))?(x):(-x)) #define STREQ(s1, s2) (strcmp((s1,)(s2))==0) #define STRGT(s1, s2) (strcmp((s1,)(s2))>0)
  • 21. MACRO SUBSTITUTION  Nested MS: We can also use one macro in the definition of another macro. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 21 MACRO IDENTIFIER STRING #define M 5 #define N M+1 #define SQUARE(x) ((x)* (x)) #define CUBE(x) (SQUARE (x) *(x)) #define SIXTH(x) (CUBE (x) * CUBE (x))
  • 22. CONDITIONAL COMPILATION:  Conditional compilation enables you to control the execution of preprocessor directives and the compilation of program code.  Each of the conditional preprocessor directives evaluates a constant integer expression.  Cast expressions, sizeof expressions and enumeration constants cannot be evaluated in preprocessor directives.  The conditional preprocessor construct is much like the if selection statement. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 22
  • 23. CONDITIONAL COMPILATION  Structure similar to if #if !defined( NULL ) #define NULL 0 #endif  Every #if must end with #endif  #ifdef short for #if defined( name )  #ifndef short for #if !defined( name )  Other statements  #elif – equivalent of else if in an if structure  #else – equivalent of else in an if structure 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 23
  • 24. PASSING VALUES TO THE COMPILER CALL BY VALUE CALL BY REFERENCE Formal parameters are local variables to callee. Values of the actual parameters are copied to the formal parameters when function is called. Conceptually simple. Values of actual parameters are protected. Costly copying if parameters are large. Can only return 1 value. Formal parameters are pointers to actual parameters. Every use of the formal parameter is implicitly dereferenced. Actual parameters must have l-values. (If not, either prohibit, or create temporary unnamed variable in caller). Aliasing problem. Can’t use in remote procedure call. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 24
  • 25. CALL BY VALUE & CALL BY REFERENCE 08/11/18 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 25 #include<stdio.h> void interchange(int number1, int number2) { int temp; temp = number1; number1 = number2; number2 = temp; } int main() { int num1 = 50, num2 = 70; interchange(num1, num2); printf(“n Number 1 : %d”, num1); printf(“n Number 2 : %d”,num2); return(0); }
  • 26. OUTPUT 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 26 Number 1 : 50 Number 2 : 70
  • 27. FILE HANDLING FUNCTIONS File handling functions Description fopen () fopen () function creates a new file or opens an existing file. fclose () fclose () function closes an opened file. getw () getw () function reads an integer from file. putw () putw () functions writes an integer to file. fgetc () fgetc () function reads a character from file. fputc () fputc () functions write a character to file. gets () gets () function reads line from keyboard. puts () puts () function writes line to o/p screen. fgets () fgets () function reads string from a file, one line at a time. fputs () fputs () function writes string to a file. feof () feof () function finds end of file. fgetchar () fgetchar () function reads a character from keyboard. 08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 27
  • 28. File handling functions Description fprintf () fprintf () function writes formatted data to a file. fscanf () fscanf () function reads formatted data from a file. fputchar () fputchar () function writes a character onto the output screen from keyboard input. fseek () fseek () function moves file pointer position to given location. SEEK_SET SEEK_SET moves file pointer position to the beginning of the file. SEEK_CUR SEEK_CUR moves file pointer position to given location. SEEK_END SEEK_END moves file pointer position to the end of file. ftell () ftell () function gives current position of file pointer. rewind () rewind () function moves file pointer position to the beginning of the file. fflush () fflush () function flushes a file. 08/11/18 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 28
  • 29. getc () getc () function reads character from file. getch () getch () function reads character from keyboard. getche () getche () function reads character from keyboard and echoes to o/p screen. getchar () getchar () function reads character from keyboard. putc () putc () function writes a character to file. putchar () putchar () function writes a character to screen. printf () printf () function writes formatted data to screen. sprinf () sprinf () function writes formatted output to string. scanf () scanf () function reads formatted data from keyboard. remove () remove () function deletes a file. 08/11/18 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 29