SlideShare a Scribd company logo
1 of 21
File Handling
Spring 2012 Programming and Data Structure 1
File
• Discrete storage unit for data in the form of a
stream of bytes.
• Durable: stored in non-volatile memory.
• Starting end, sequence of bytes, and end of
stream (or end of file).
• Sequential access of data by a pointer
performing read / write / deletion / insertion.
• Meta-data (information about the file) before
the stream of actual data.
Spring 2012 Programming and Data Structure 2
Spring 2012 Programming and Data Structure 3
40 65 87 90 24 67 89 90 0 0
Head Tail
File Pointer
Meta Data
File handling in C
• In C we use FILE * to represent a pointer to a file.
• fopen is used to open a file. It returns the special value
NULL to indicate that it couldn't open the file.
Spring 2012 Programming and Data Structure 4
FILE *fptr;
char filename[]= "file2.dat";
fptr= fopen (filename,"w");
if (fptr == NULL) {
printf (“ERROR IN FILE CREATION”);
/* DO SOMETHING */
}
Modes for opening files
• The second argument of fopen is the mode
in which we open the file. There are three
• "r" opens a file for reading
• "w" creates a file for writing - and writes over
all previous contents (deletes the file so be
careful!)
• "a" opens a file for appending - writing on the
end of the file
• “rb” read binary file (raw bytes)
• “wb” write binary file
Spring 2012 Programming and Data Structure 5
The exit() function
• Sometimes error checking means we want
an "emergency exit" from a program. We
want it to stop dead.
• In main we can use "return" to stop.
• In functions we can use exit to do this.
• Exit is part of the stdlib.h library
Spring 2012 Programming and Data Structure 6
exit(-1);
in a function is exactly the same as
return -1;
in the main routine
Usage of exit( )
FILE *fptr;
char filename[]= "file2.dat";
fptr= fopen (filename,"w");
if (fptr == NULL) {
printf (“ERROR IN FILE CREATION”);
/* Do something */
exit(-1);
}
Spring 2012 Programming and Data Structure 7
Writing to a file using fprintf( )
• fprintf( ) works just like printf and sprintf
except that its first argument is a file pointer.
Spring 2012 Programming and Data Structure 8
FILE *fptr;
fptr= fopen ("file.dat","w");
/* Check it's open */
fprintf (fptr,"Hello World!n");
Reading Data Using fscanf( )
FILE *fptr;
fptr= fopen (“input.dat”,“r”);
/* Check it's open */
if (fptr==NULL)
{
printf(“Error in opening file n”);
}
fscanf(fptr,“%d%d”,&x,&y);
Spring 2012 Programming and Data Structure 9
•We also read data from a file using fscanf( ).
20 30
input.dat
x=20
y=30
Reading lines from a file using
fgets( )
We can read a string using fgets ( ).
Spring 2012 Programming and Data Structure 10
FILE *fptr;
char line [1000];
/* Open file and check it is open */
while (fgets(line,1000,fptr) != NULL) {
printf ("Read line %sn",line);
}
fgets( ) takes 3 arguments, a string, a maximum
number of characters to read and a file pointer.
It returns NULL if there is an error (such as EOF).
Closing a file
• We can close a file simply using fclose( ) and
the file pointer.
Spring 2012 Programming and Data Structure 11
FILE *fptr;
char filename[]= "myfile.dat";
fptr= fopen (filename,"w");
if (fptr == NULL) {
printf ("Cannot open file to write!n");
exit(-1);
}
fprintf (fptr,"Hello World of filing!n");
fclose (fptr);
Opening
Access
closing
Three special streams
• Three special file streams are defined in the
<stdio.h> header
• stdin reads input from the keyboard
• stdout send output to the screen
• stderr prints errors to an error device
(usually also the screen)
• What might this do?
Spring 2012 Programming and Data Structure 12
fprintf (stdout,"Hello World!n");
An example program
Spring 2012 Programming and Data Structure 13
#include <stdio.h>
main()
{
int i;
fprintf(stdout,"Give value of i n");
fscanf(stdin,"%d",&i);
fprintf(stdout,"Value of i=%d n",i);
fprintf(stderr,"No error: But an example to
show error message.n");
}
Give value of i
15
Value of i=15
No error: But an example to show error message.
Display on
The screen
Input File & Output File redirection
• One may redirect the input and output files to
other files (other than stdin and stdout).
• Usage: Suppose the executable file is a.out
Spring 2012 Programming and Data Structure 14
$ ./a.out <in.dat >out.dat
15
in.dat
Give value of i
Value of i=15
out.dat
No error: But an example to show error message.
Display
screen
Reading and Writing a character
• A character reading/writing is equivalent to
reading/writing a byte.
int getchar( );
int fgetc(FILE *fp);
int putchar(int c);
int fputc(int c, FILE *fp);
• Example:
char c;
c=getchar( );
putchar(c);
Spring 2012 Programming and Data Structure 15
Example: use of getchar() and
putchar()
Spring 2012 Programming and Data Structure 16
#include <stdio.h>
main()
{
int c;
printf("Type text and press return to see it again n");
printf("For exiting press <CTRL D> n");
while((c=getchar( ))!=EOF) putchar(c);
}
End of file
Command Line Arguments
• Command line arguments may be passed by
specifying them under main( ).
int main(int argc, char *argv[ ]);
Spring 2012 Programming and Data Structure 17
Argument
Count Array of Strings
as command line
arguments including
the command itself.
Example: Reading command line arguments
Spring 2012 Programming and Data Structure 18
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
FILE *ifp,*ofp;
int i,c;
char src_file[100],dst_file[100];
if(argc!=3){
printf("Usage: ./a.out <src_file> <dst_file> n");
exit(0);
}
else{
strcpy(src_file,argv[1]);
strcpy(dst_file,argv[2]);
}
Example: Contd.
Spring 2012 Programming and Data Structure 19
if((ifp=fopen(src_file,"r"))==NULL)
{
printf("File does not exist.n");
exit(0);
}
if((ofp=fopen(dst_file,"w"))==NULL)
{
printf("File not created.n");
exit(0);
}
while((c=getc(ifp))!=EOF){
putc(c,ofp);
}
fclose(ifp);
fclose(ofp);
}
./a.out s.dat d.dat
argc=3
./a.out
s.dat
d.dat
argv
Getting numbers from strings
• Once we've got a string with a number in it
(either from a file or from the user typing)
we can use atoi or atof to convert it to a
number
• The functions are part of stdlib.h
Spring 2012 Programming and Data Structure 20
char numberstring[]= "3.14";
int i;
double pi;
pi= atof (numberstring);
i= atoi ("12");
Both of these functions return 0 if they have a problem
Example: Averaging from Command
Line
Spring 2012 Programming and Data Structure 21
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
float sum=0;
int i,num;
num=argc-1;
for(i=1;i<=num;i++)
sum+=atof(argv[i]);
printf("Average=%f n",sum/(float) num);
}
$ ./a.out 45 239 123
Average=135.666667

More Related Content

Similar to WK-12-13-file f classs 12 computer science from kv.

Similar to WK-12-13-file f classs 12 computer science from kv. (20)

File management
File managementFile management
File management
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Handout#01
Handout#01Handout#01
Handout#01
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
File Handling in C.pptx
File Handling in C.pptxFile Handling in C.pptx
File Handling in C.pptx
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
slides3_077.ppt
slides3_077.pptslides3_077.ppt
slides3_077.ppt
 
File in c
File in cFile in c
File in c
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File Management in C
File Management in CFile Management in C
File Management in C
 
Files in C
Files in CFiles in C
Files in C
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
File mangement
File mangementFile mangement
File mangement
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

WK-12-13-file f classs 12 computer science from kv.

  • 1. File Handling Spring 2012 Programming and Data Structure 1
  • 2. File • Discrete storage unit for data in the form of a stream of bytes. • Durable: stored in non-volatile memory. • Starting end, sequence of bytes, and end of stream (or end of file). • Sequential access of data by a pointer performing read / write / deletion / insertion. • Meta-data (information about the file) before the stream of actual data. Spring 2012 Programming and Data Structure 2
  • 3. Spring 2012 Programming and Data Structure 3 40 65 87 90 24 67 89 90 0 0 Head Tail File Pointer Meta Data
  • 4. File handling in C • In C we use FILE * to represent a pointer to a file. • fopen is used to open a file. It returns the special value NULL to indicate that it couldn't open the file. Spring 2012 Programming and Data Structure 4 FILE *fptr; char filename[]= "file2.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf (“ERROR IN FILE CREATION”); /* DO SOMETHING */ }
  • 5. Modes for opening files • The second argument of fopen is the mode in which we open the file. There are three • "r" opens a file for reading • "w" creates a file for writing - and writes over all previous contents (deletes the file so be careful!) • "a" opens a file for appending - writing on the end of the file • “rb” read binary file (raw bytes) • “wb” write binary file Spring 2012 Programming and Data Structure 5
  • 6. The exit() function • Sometimes error checking means we want an "emergency exit" from a program. We want it to stop dead. • In main we can use "return" to stop. • In functions we can use exit to do this. • Exit is part of the stdlib.h library Spring 2012 Programming and Data Structure 6 exit(-1); in a function is exactly the same as return -1; in the main routine
  • 7. Usage of exit( ) FILE *fptr; char filename[]= "file2.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf (“ERROR IN FILE CREATION”); /* Do something */ exit(-1); } Spring 2012 Programming and Data Structure 7
  • 8. Writing to a file using fprintf( ) • fprintf( ) works just like printf and sprintf except that its first argument is a file pointer. Spring 2012 Programming and Data Structure 8 FILE *fptr; fptr= fopen ("file.dat","w"); /* Check it's open */ fprintf (fptr,"Hello World!n");
  • 9. Reading Data Using fscanf( ) FILE *fptr; fptr= fopen (“input.dat”,“r”); /* Check it's open */ if (fptr==NULL) { printf(“Error in opening file n”); } fscanf(fptr,“%d%d”,&x,&y); Spring 2012 Programming and Data Structure 9 •We also read data from a file using fscanf( ). 20 30 input.dat x=20 y=30
  • 10. Reading lines from a file using fgets( ) We can read a string using fgets ( ). Spring 2012 Programming and Data Structure 10 FILE *fptr; char line [1000]; /* Open file and check it is open */ while (fgets(line,1000,fptr) != NULL) { printf ("Read line %sn",line); } fgets( ) takes 3 arguments, a string, a maximum number of characters to read and a file pointer. It returns NULL if there is an error (such as EOF).
  • 11. Closing a file • We can close a file simply using fclose( ) and the file pointer. Spring 2012 Programming and Data Structure 11 FILE *fptr; char filename[]= "myfile.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf ("Cannot open file to write!n"); exit(-1); } fprintf (fptr,"Hello World of filing!n"); fclose (fptr); Opening Access closing
  • 12. Three special streams • Three special file streams are defined in the <stdio.h> header • stdin reads input from the keyboard • stdout send output to the screen • stderr prints errors to an error device (usually also the screen) • What might this do? Spring 2012 Programming and Data Structure 12 fprintf (stdout,"Hello World!n");
  • 13. An example program Spring 2012 Programming and Data Structure 13 #include <stdio.h> main() { int i; fprintf(stdout,"Give value of i n"); fscanf(stdin,"%d",&i); fprintf(stdout,"Value of i=%d n",i); fprintf(stderr,"No error: But an example to show error message.n"); } Give value of i 15 Value of i=15 No error: But an example to show error message. Display on The screen
  • 14. Input File & Output File redirection • One may redirect the input and output files to other files (other than stdin and stdout). • Usage: Suppose the executable file is a.out Spring 2012 Programming and Data Structure 14 $ ./a.out <in.dat >out.dat 15 in.dat Give value of i Value of i=15 out.dat No error: But an example to show error message. Display screen
  • 15. Reading and Writing a character • A character reading/writing is equivalent to reading/writing a byte. int getchar( ); int fgetc(FILE *fp); int putchar(int c); int fputc(int c, FILE *fp); • Example: char c; c=getchar( ); putchar(c); Spring 2012 Programming and Data Structure 15
  • 16. Example: use of getchar() and putchar() Spring 2012 Programming and Data Structure 16 #include <stdio.h> main() { int c; printf("Type text and press return to see it again n"); printf("For exiting press <CTRL D> n"); while((c=getchar( ))!=EOF) putchar(c); } End of file
  • 17. Command Line Arguments • Command line arguments may be passed by specifying them under main( ). int main(int argc, char *argv[ ]); Spring 2012 Programming and Data Structure 17 Argument Count Array of Strings as command line arguments including the command itself.
  • 18. Example: Reading command line arguments Spring 2012 Programming and Data Structure 18 #include <stdio.h> #include <string.h> int main(int argc,char *argv[]) { FILE *ifp,*ofp; int i,c; char src_file[100],dst_file[100]; if(argc!=3){ printf("Usage: ./a.out <src_file> <dst_file> n"); exit(0); } else{ strcpy(src_file,argv[1]); strcpy(dst_file,argv[2]); }
  • 19. Example: Contd. Spring 2012 Programming and Data Structure 19 if((ifp=fopen(src_file,"r"))==NULL) { printf("File does not exist.n"); exit(0); } if((ofp=fopen(dst_file,"w"))==NULL) { printf("File not created.n"); exit(0); } while((c=getc(ifp))!=EOF){ putc(c,ofp); } fclose(ifp); fclose(ofp); } ./a.out s.dat d.dat argc=3 ./a.out s.dat d.dat argv
  • 20. Getting numbers from strings • Once we've got a string with a number in it (either from a file or from the user typing) we can use atoi or atof to convert it to a number • The functions are part of stdlib.h Spring 2012 Programming and Data Structure 20 char numberstring[]= "3.14"; int i; double pi; pi= atof (numberstring); i= atoi ("12"); Both of these functions return 0 if they have a problem
  • 21. Example: Averaging from Command Line Spring 2012 Programming and Data Structure 21 #include <stdio.h> #include <stdlib.h> int main(int argc,char *argv[]) { float sum=0; int i,num; num=argc-1; for(i=1;i<=num;i++) sum+=atof(argv[i]); printf("Average=%f n",sum/(float) num); } $ ./a.out 45 239 123 Average=135.666667