SlideShare a Scribd company logo
1 of 24
Dynamic Memory Allocation
The process of allocating memory at run time is
known as dynamic memory allocation. Although c
does not inherently have this facility there are four
library routines which allow this functions, which can
be used to allocate and free memory during the
program execution.
Dynamic Memory Allocation Functions
malloc()
A block mf memory may be allocated using the
function malloc. The malloc function reserves a block
of memory of specified size and returns a pointer of
type void. This means that we can assign it to any
type of pointer. It takes the following form:
ptr=(cast-type*)malloc(byte-size);
ptr is a pointer of type cast-type the malloc
returns a pointer (of cast type) to an area of
memory with size byte-size
malloc() Example
Example:
x=(int*)malloc(100*sizeof(int));
On successful execution of this statement a memory
equivalent to 100 times the area of int bytes is
reserved and the address of the first byte of
memory allocated is assigned to the pointer x of
type int.
Calloc
Calloc is another memory allocation function that is
normally used to request multiple blocks of storage
each of the same size and then sets all bytes to zero.
The general form of calloc is:
ptr=(cast-type*) calloc(n,elem-size);
Calloc(Contd.)
calloc() allocates contiguous space for n blocks each
size of elements size bytes. All bytes are initialized to
zero and a pointer to the first byte of the allocated
region is returned. If there is not enough space a null
pointer is returned.
free()
Compile time storage of a variable is allocated and
released by the system in accordance with its storage
class. With the dynamic runtime allocation, it is our
responsibility to release the space when it is not
required.
free(ptr);
ptr is a pointer that has been created by using malloc
or calloc
realloc
The memory allocated by using calloc or malloc might
be insufficient or excess sometimes in both the
situations we can change the memory size already
allocated with the help of the function realloc. This
process is called reallocation of memory. The general
statement of reallocation of memory is :
ptr=realloc(ptr,newsize);
FILE HANDLING
Introduction
Files are places where data can be stored
permanently.
Some programs expect the same set of data to be
fed as input every time it is run.
Cumbersome.
Better if the data are kept in a file, and the program
reads from the file.
Programs generating large volumes of output.
Difficult to view on the screen.
Better to store them in a file for later viewing/
processing
Basic File Operations
Opening a file
Reading data from a file
Writing data to a file
Closing a file
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:
“r” :: open the file for reading only
“w” :: open the file for writing only
“a” :: open the file for appending data to it
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) ;
Read/Write Operations on Files
The simplest file input-output (I/O) function are getc and
putc.
getc is used to read a character from a file and return it.
char ch; FILE *fp;
…..
ch = getc (fp) ;
getc will return an end-of-file marker EOF, when the end of the
file has been reached.
putc is used to write a character to a file.
char ch; FILE *fp;
……
putc (c, fp) ;
main() {
FILE *in, *out ;
char c ;
in = fopen (“infile.dat”, “r”) ;
out = fopen (“outfile.dat”, “w”) ;
while ((c = getc (in)) != EOF)
putc (toupper (c), out);
fclose (in) ;
fclose (out) ;
}
Basic operations of files(Contd.)
We can also use the file versions of scanf and printf,
called fscanf and fprintf.
General format:
fscanf (file_pointer, control_string, list) ;
fprintf (file_pointer, control_string, list) ;
Examples:
fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ;
fprintf (out, “nThe result is: %d”, xyz) ;
Command line argument
Command line arguments are parameters supplied to a
program, when the program is invoked.
How do these parameters get into the program?
Every C program has a main function.
main can take two arguments conventionally called argc and
argv.
Information regarding command line arguments are passed to
the program through argc and argv.
INTRODUCTION TO C
PREPROCESSOR
C Preprocessor
Overview
Preprocessor Directives
Conditional Compilation
Overview
 Six phases to execute C:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
C Preprocessor
All preprocessor directives begin with #
Possible actions
Inclusion of other files
Definition of symbolic constants & macros
Conditional compilation of program code
Conditional compilation of preprocessor directives
Preprocessor Directives
#define for symbolic constants
#define identifier text
 Creates symbolic constants
 The “identifier” is replaced by “text” in the program
Example
#define PI 3.14
area = PI * radius * radius;
 Replaced by “area = 3.14 * radius * radius” by
preprocessor before compilation
Conditional Compilation
Controls the execution of preprocessor directives
& compilation of code
Define NULL, if it hasn’t been defined yet
#if !defined(NULL)
#define NULL 0
#endif
Use to comment out code (for comments)
#if 0
code prevented from compiling
#endif
THANK YOU

More Related Content

What's hot (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
File handling-c
File handling-cFile handling-c
File handling-c
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Structure in C
Structure in CStructure in C
Structure in C
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
User defined function in c
User defined function in cUser defined function in c
User defined function in c
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 

Similar to Memory allocation in c

EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in CTushar B Kute
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILESHarish Kamat
 
File Management in C
File Management in CFile Management in C
File Management in CPaurav Shah
 
C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)leonard horobet-stoian
 
Unit_V_Files handling in c programming language.pptx
Unit_V_Files handling in c programming language.pptxUnit_V_Files handling in c programming language.pptx
Unit_V_Files handling in c programming language.pptxraushankumarthakur7
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in cMugdhaSharma11
 
File management
File managementFile management
File managementsumathiv9
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfsangeeta borde
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C ProgrammingRavindraSalunke3
 

Similar to Memory allocation in c (20)

File mangement
File mangementFile mangement
File mangement
 
File management
File managementFile management
File management
 
Unit5
Unit5Unit5
Unit5
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Handout#01
Handout#01Handout#01
Handout#01
 
File Management in C
File Management in CFile Management in C
File Management in C
 
File Handling in C.pptx
File Handling in C.pptxFile Handling in C.pptx
File Handling in C.pptx
 
C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)
 
File management
File managementFile management
File management
 
File in C language
File in C languageFile in C language
File in C language
 
Unit_V_Files handling in c programming language.pptx
Unit_V_Files handling in c programming language.pptxUnit_V_Files handling in c programming language.pptx
Unit_V_Files handling in c programming language.pptx
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
 
File management
File managementFile management
File management
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 

More from Prabhu Govind

More from Prabhu Govind (20)

Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Unions in c
Unions in cUnions in c
Unions in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
Array & string
Array & stringArray & string
Array & string
 
Recursive For S-Teacher
Recursive For S-TeacherRecursive For S-Teacher
Recursive For S-Teacher
 
User defined Functions in C
User defined Functions in CUser defined Functions in C
User defined Functions in C
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Looping in C
Looping in CLooping in C
Looping in C
 
Branching in C
Branching in CBranching in C
Branching in C
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Operators in C
Operators in COperators in C
Operators in C
 
Statements in C
Statements in CStatements in C
Statements in C
 
Data types in C
Data types in CData types in C
Data types in C
 
Constants in C
Constants in CConstants in C
Constants in C
 
Variables_c
Variables_cVariables_c
Variables_c
 
Tokens_C
Tokens_CTokens_C
Tokens_C
 
Computer basics
Computer basicsComputer basics
Computer basics
 
Computer fundamentals
Computer fundamentalsComputer fundamentals
Computer fundamentals
 
computer hardware and software
computer hardware and softwarecomputer hardware and software
computer hardware and software
 

Recently uploaded

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Memory allocation in c

  • 1. Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. Although c does not inherently have this facility there are four library routines which allow this functions, which can be used to allocate and free memory during the program execution.
  • 3. malloc() A block mf memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form: ptr=(cast-type*)malloc(byte-size); ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with size byte-size
  • 4. malloc() Example Example: x=(int*)malloc(100*sizeof(int)); On successful execution of this statement a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int.
  • 5. Calloc Calloc is another memory allocation function that is normally used to request multiple blocks of storage each of the same size and then sets all bytes to zero. The general form of calloc is: ptr=(cast-type*) calloc(n,elem-size);
  • 6. Calloc(Contd.) calloc() allocates contiguous space for n blocks each size of elements size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space a null pointer is returned.
  • 7. free() Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is not required. free(ptr); ptr is a pointer that has been created by using malloc or calloc
  • 8. realloc The memory allocated by using calloc or malloc might be insufficient or excess sometimes in both the situations we can change the memory size already allocated with the help of the function realloc. This process is called reallocation of memory. The general statement of reallocation of memory is : ptr=realloc(ptr,newsize);
  • 10. Introduction Files are places where data can be stored permanently. Some programs expect the same set of data to be fed as input every time it is run. Cumbersome. Better if the data are kept in a file, and the program reads from the file. Programs generating large volumes of output. Difficult to view on the screen. Better to store them in a file for later viewing/ processing
  • 11. Basic File Operations Opening a file Reading data from a file Writing data to a file Closing a file
  • 12. 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: “r” :: open the file for reading only “w” :: open the file for writing only “a” :: open the file for appending data to it
  • 13. 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) ;
  • 14. Read/Write Operations on Files The simplest file input-output (I/O) function are getc and putc. getc is used to read a character from a file and return it. char ch; FILE *fp; ….. ch = getc (fp) ; getc will return an end-of-file marker EOF, when the end of the file has been reached. putc is used to write a character to a file. char ch; FILE *fp; …… putc (c, fp) ;
  • 15. main() { FILE *in, *out ; char c ; in = fopen (“infile.dat”, “r”) ; out = fopen (“outfile.dat”, “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ; }
  • 16. Basic operations of files(Contd.) We can also use the file versions of scanf and printf, called fscanf and fprintf. General format: fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ; Examples: fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ; fprintf (out, “nThe result is: %d”, xyz) ;
  • 17. Command line argument Command line arguments are parameters supplied to a program, when the program is invoked. How do these parameters get into the program? Every C program has a main function. main can take two arguments conventionally called argc and argv. Information regarding command line arguments are passed to the program through argc and argv.
  • 20. Overview  Six phases to execute C: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute
  • 21. C Preprocessor All preprocessor directives begin with # Possible actions Inclusion of other files Definition of symbolic constants & macros Conditional compilation of program code Conditional compilation of preprocessor directives
  • 22. Preprocessor Directives #define for symbolic constants #define identifier text  Creates symbolic constants  The “identifier” is replaced by “text” in the program Example #define PI 3.14 area = PI * radius * radius;  Replaced by “area = 3.14 * radius * radius” by preprocessor before compilation
  • 23. Conditional Compilation Controls the execution of preprocessor directives & compilation of code Define NULL, if it hasn’t been defined yet #if !defined(NULL) #define NULL 0 #endif Use to comment out code (for comments) #if 0 code prevented from compiling #endif