Professional Documents
Culture Documents
PROGRAMMING CONSTRUCT - 3
07 Maret 2017
OUTLINE
Function
Pointer
File IO
OBJECTIVES
Mahasiswa mampu menganalisa dan menjelaskan
perilaku dari program sederhana yang melibatkan
pemakaian contruct programming dasar.
WHAT IS FUNCTION ?
Function is a set of statement which is designed to
perform some specific task.
Function gives functionality to programmer to use
one module(task) for more times rather then write
same code again and again. By using Function we
can divide the large program to many separate
modules based on their functionality.
Every C program can be a thought of the
collection of functions.
CLASSIFICATION OF FUNCTION
Library function
These are the in-built functions of C library.
These are already defined in header files.
e.g. printf( ); is a function which is used to print
output. It is defined instdio.hfile .
CLASSIFICATION OF FUNCTION
User defined function
Programmer can create their own function in C to
perform specific task.
e.g.
SOME CONCLUSION
Any C Program must contain at least one function.
If program contains only one function it must be
main( ).
There is no limit on the number of functions
present in a C program
Each function in a program is called in the
sequence specified by functions call in main( ).
After each function has done its thing, control
returns to main( ). When main( ) run out of
function calls program ends
C Program is a collection of one or more functions.
A function gets called when its name is followed by a
semicolon.
USING FUNCTIONS
The main functions and other library functions
does need to be declared and defined but the main
functions body need to be defined by the
programmer.
There are 3 components associated with functions
:
1. The Declaration
2. The function definition
3. The Calling Statement
TYPES OF FUNCTIONS
Function can be divided into 4 categories:
A function with no arguments and no return value
A function with no arguments and a return value
A function with an argument or arguments and
returning no value
A function with arguments and returning a values
POINTER
Variable that declared occupies a memory
according to it size
It has address for the location so it can be referred
later by CPU for manipulation
The * and & Operator
Int x= 10
x Memory location name
10 Value at memory location
76858 Memory location address
We can use the address which also point the same
value.
EXAMPLE
EXAMPLE
FILE I/O
FILE MANAGEMENT
We have been using the functions such as printf
and scanf to read and write data. This works fine
as long as data is small.
But it has two major problems:
It becomes cumbersome and time consuming to handle
large volume of data through terminals.
The entire data is lost when either the program is
terminated or the computer is turned off .
So it is necessary to have more flexible approach
where data can be stored on the disks and read
whenever necessary, without destroying data.
FILE MANAGEMENT
This concept is called files.
Files: A file is a place on the disk where a group of
related data is stored.
FILE is a structure declared in stdio.h . We have to
use file pointer, a pointer variable that points to a
structure FILE.
C FILE MANAGEMENT
C supports a number o functions that have the
ability to perform basic file operations:
Creating a file
Opening a file
Reading data from a file
Writing data to a file
Closing a file
Renaming a file
Deleting a file
FILES IN C
In C, each file is simply a sequential stream of bytes. C
imposes no structure on a file.
A file must first be opened properly before it can be
accessed for reading or writing. When a file is opened,
a stream is associated with the file.
Successfully opening a file returns a pointer to (i.e., the
address of) a file structure, which contains a file
descriptor and a file control block.
FILES IN C
The statement:
FILE *fptr1, *fptr2 ;
declares that fptr1 and fptr2 are pointer variables of
type FILE. They will be assigned the address of a file
descriptor, that is, an area of memory that will be
associated with an input or output stream.
OPENING FILES
The statement:
fptr1 = fopen ( "mydata", "r" ) ;
would open the file mydata for input (reading).
The statement:
fptr2 = fopen ("results", "w" ) ;
would open the file results for output (writing).
Once the files are open, they stay open until you close
them or end the program (which will close all files.)
int a, b ;
FILE *fptr1, *fptr2 ;
fptr1 = fopen ( "mydata", "r" ) ;
fscanf ( fptr1, "%d%d", &a, &b) ;
END OF FILE
The end-of-file indicator informs the program when
there are no more data (no more bytes) to be
processed.
There are a number of ways to test for the end-of-file
condition. One is to use the feof function which returns
a true or false condition:
fscanf (fptr1, "%d", &var) ;
if ( feof (fptr1) )
{
printf ("End-of-file encountered.\n);
}
END OF FILE
There are a number of ways to test for the end-of-file
condition. Another way is to use the value returned by
the fscanf function:
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == EOF )
{
printf ("End-of-file encountered.\n) ;
}
WRITING TO FILES
Likewise in a similar way, in the following segment of C
language code:
int a = 5, b = 20 ;
FILE *fptr2 ;
fptr2 = fopen ( "results", "w" ) ;
fprintf ( fptr2, "%d %d\n", a, b ) ;
CLOSING FILES
The statements:
fclose ( fptr1 ) ;
fclose ( fptr2 ) ;
will close the files and release the file descriptor space
and I/O buffer memory.
printf ("%6.2f,%2d,%5.2f\n", e, f, g) ;
}
12345678901234567890
****************************
13.72 5 6.68
13.72, 5, 6.68
REFERENCE