The Liver & Gallbladder (Anatomy & Physiology).pptx
cpFunctions-Files.pptx
1. UNIT-5
FUNCTIONS
FILES
Prepared By:
M. Rajesh Reddy,
Assistant Professor,
Department of CSE
KKR & KSR INSTITUTE OF TECHNOLOGY & SCIENCES
(Autonomous)
(Approved by AICTE, Delhi , Permanently Affiliated to JNTUK, Kakinada,
Accredited by NAAC & NBA)
Vinjanampadu, Guntur, Andhra Pradesh
Subject: Programming for problem solving using C
Year/Sem: I B.Tech I Sem
Class: Civil & Mechanical
2. DESIGNING STRUCTURED PROGRAMS:
Breaking a complex problem into smaller parts is a
common practice.
The planning for solving a complex problem is first,
we must understand the problem as a whole; then
we must break it into simpler, understandable parts.
We call each of these parts of a program a module
and the process of subdividing a problem into
manageable parts is called top-down design.
In top-down design, a program is divided into a
main module and its related modules. Each module
is in turn divided into sub modules until the resulting
modules are understood without further division.
5 November
2023
2
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
3. The Main Module is known as a calling module because it
has sub modules. Each of the sub modules is known as a
called module.
But, because Modules 1, 2, and 3 also have sub modules,
they are also calling modules; they are both called and calling
modules.
Communication between modules in the structure chart is
allowed only through a calling module. If Module 1 needs to
send data to Module 2, the data must be passed through the
calling module, Main Module.
No communication can take place directly between modules
that do not have a calling called relationship.
5 November
2023
3
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
4. FUNCTIONS IN C:
A function is a group of statements that together perform
a task. Every C program has at least one function, which
is main() function.
A C program is made of one or more functions, one and
only one of which must he named main.
The execution of the program always starts and ends
with main, but it can call other functions to do special
tasks.
A function in C (including main) is an independent module
that will be called to do a specific task.
A called function receives control from a calling function.
When the called function completes its task, it returns
control to the calling function. It may or may not return a
value to the caller.
The function main is called by the operating system; main
in turn calls other functions. When main is complete,
control returns to the operating system.
5 November
2023
4
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
5. FUNCTIONS IN C:
In general, the purpose of a function is to receive zero or
more pieces of data, operate on them, and return at
most one piece of data.
5 November
2023
5
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
6. FUNCTIONS IN C:
A function can have a side effect. A function side effect is
an action that results in a change in the state of the
program.
If a side effect occurs, it occurs while the function is
executing and before the function returns.
The side effect can involve accepting data from outside
the program, sending data out of the program to the
monitor or a file, or changing the value of a variable in
the calling function.
5 November
2023
6
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
7. Why functions
When we write large complex programs, it becomes difficult
to keep track of the source code.
The job of functions is to divide the large program to many
separate modules based on their functionality.
Reusability
Readability
Advantages of Functions:
The function provides modularity.
C functions are used to avoid rewriting same logic/code
again and again.
We can call a function any number of times.
A large C program can easily debug when it is divided into
functions.
Separate function independently can be developed
according to the needs.
5 November
2023
7
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
8. “C‟ language supports two types of functions
1. Library functions.
2. User defined functions.
1. Library functions:
Library functions
are pre-defined set of functions. Their task is limited.
Library functions are not required to be written by us.
A user cannot understand the internal working of these functions.
The user can only use the functions but can‟t change or modify
them.
Ex: - printf(), scanf(), sqrt(), cos(), strcat().
2. User-defined functions:
The user defined function has to be developed by the user.
The functions defined by the user according to their requirements
are called as user-defined functions.
The user can modify the function according to the requirement.
The user certainly understands the internal working of the
function.
Ex: - main(), add(). sub(), mul(),div()
5 November
2023
8
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
TYPES OF FUNCTIONS:
9. 1. Function declaration
2. Function call
3. Function definition
Function definition:
When a function is defined, space is allocated for that function in
the memory. A function definition comprises two parts:
function header and
function body.
The general syntax of a function is
5 November
2023
9
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
STEPS IN CREATING USER-DEFINED FUNCTIONS:
return-type function-name (type varname1, type varname2, . . . , type varnameN)
{
body of the function;
return statement;
}
10. function header: it consists of three parts
return-type: The return-type specifies the type of data that the function
returns. A function may return any type of data except an array.
Function name: The function name is an identifier by which this function will
be known, and obeys the same naming rules as applied to variable names.
parameter list : It is a list of variable names and their associated types. The
parameters receive the values of the arguments when the function is called.
A function can be without parameters, in which case the parameter list is
empty. An empty parameter list can be explicitly specified as such by placing
the keyword void inside the parentheses.
function body:
The function body contains a collection of statements that define what the
function does.
The return Statement:
The return statement exits the called function and returns control back to the
calling function. Once a return statement is executed, no further instructions
within the functionare executed.
A single return value may be returned.
Parentheses are allowed but not required around the return value.
A function with a void return type will not have a return value after the
return statement.
More than one return statement may appear in a function, but only one return
statement will be executed by any given function call.
5 November
2023
10
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
11. Function declaration or prototype:
In C, all functions must be declared before they are
used. This is normally accomplished using a function
prototype.
The function prototype tells the compiler about the
function name, type of arguments to be passed and
return type.
The compiler will also catch differences between the
number of arguments used to call a function and the
number of parameters in the function definition
Syntax:
return_type function_name (type parm_name1, type
parm_name2, . . . , type parm_nameN);
The parameters or arguments used in function
definition or function prototype are called formal
parameters.
5 November
2023
11
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
12. Function call statement:
The function call statement invokes the function, which
means the program control passes to that of the function.
Once the function completes its task, the program control is
passed back to the calling environment
Syntax: function_name ( arguments list );
A function is called by using the function name, followed by
a set of parentheses containing the data to be passed to the
function.
The data passed to the function are referred to as the
"actual“ parameters. The variables within the function
which receive the passed data are referred to as the
"formal" parameters.
The formal parameters are local variables, which exist
during the execution of the function only, and are only
known by the called function.
To call a function which takes no arguments, use an empty
pair of parentheses.
5 November
2023
12
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
13. Example for User-Defined Function:
5 November
2023
13
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
• Functions must be both declared and defined.
• The Function declaration, which needs to be done before the function
call, gives the whole picture of the Function that needs to be defined
later.
• The declaration mentions the name of the function, the return type, and
the type and order of formal parameters.
#include <stdio.h>
//function declaration
int max(int x, int y);
int main()
{
int a = 10, b = 20,m;
//function call
m = max(a, b);
printf("m is %d", m);
return 0;
}
// function definition
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
Output: m is 20
14. A function name is used three times: for declaration, in
a call, and for definition.
5 November
2023
14
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
15. We classify the basic function designs by their
return values and their parameter lists into 4
types.
1. Function without arguments and without
return value
2. Function without arguments and with return
value
3. Function with arguments and without return
value
4. Function with arguments and with return value
5 November
2023
15
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
BASIC FUNCTION DESIGNS:
16. 1. Function without arguments and without return
value:
In this type, the calling function does not send any data
and it does not receive any data from the called function.
5 November
2023
16
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
#include<stdio.h>
void fact();
int main()
{
fact();
return 0;
}
void fact()
{
int i,f=1,n;
printf("Enter a Number: ");
scanf("%d",&n);
for(i=1; i<=n; i++)
f=f*i;
printf("n Factorial of %d is: %d",n,f);
}
Output:
Enter a Number: 5
Factorial of 5 is: 120
17. 2. Function without arguments and with return value:
In this type, the calling function does not send any data but it
receives data from the called function. .
It is One-way Communication between calling function and
called function
5 November
2023
17
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
#include<stdio.h>
int fact();
int main()
{
int f;
f=fact();
printf("n Factorial is: %d",f);
return 0;
}
int fact()
{
int i,f=1,n;
printf("Enter a Number : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
f=f*i;
return f;
}
Output:
Enter a number : 5
Factorial is: 120
18. 3. Function with arguments and without return value:
In this type, the calling function sends data to called function
and it does not receive any data from the called function.
It is One-way Communication between calling function and
called function
5 November
2023
18
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
#include<stdio.h>
void fact(int);
int main()
{
int num;
printf("Enter a Number: ");
scanf("%d",&num);
fact(num);
return 0;
}
void fact(int n)
{
int i,f=1;
for(i=1; i<=n; i++)
f=f*i;
printf("n Factorial of %d is: %d",n,f);
}
Output:
Enter a Number : 5
Factorial of 5 is: 120
19. 4. Function with arguments and with return value:
In this type, the calling function sends data and it receives data
from the called function.
It is Two-way Communication between calling function and
called function
5 November
2023
19
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
#include<stdio.h>
int fact(int);
int main()
{
int n,i,f;
printf("Enter Number: ");
scanf("%d",&n);
f=fact(n);
printf("n Factorial is: %d”,f);
return 0;
}
int fact(int n)
{
int i,f=1;
for(i=1; i<=n;
i++)
f=f*i;
return f;
}
Output:
Enter a Number : 5
Factorial of 5 is: 120
20. In C, there are two ways that arguments can be passed. They are,
1. Call by value
2. Call by address
1. Call by value:
In call by value method, the value of the variable is passed to
the function as parameter.
The value of the actual parameter cannot be modified by
formal parameter.
Different Memory is allocated for both actual and formal
parameters. The value of actual parameter is copied to formal
parameter.
Note:
Actual parameter – This is the argument which is used in
function call.
Formal parameter – This is the argument which is used in
function definition
5 November
2023
20
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
PARAMETER PASSING TECHNIQUES (OR)
INTER-FUNCTION COMMUNICATION:
21. In this program, the variables m and n are actual parameters and
variables a and b are formal parameters.
A copy of values of m and n are passed to the function swap().
These values are copied to formal parameters a and b in swap
function and are used.
Any changes made through formal parameters does not effect the
values of actual parameters
5 November
2023
21
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
EXAMPLE FOR CALL BY VALUE:
#include<stdio.h>
void swap(int, int);
int main()
{
int m = 22, n = 44;
printf(“Before swap, m = %d and n = %d",m, n);
swap(m, n);
printf(“nAfter swap m = %d and n = %d",m, n);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Output:
Before swap, m = 22and n = 44
After swap m, = 22and n = 44
22. 2. Call by address:
In call by address method, the address of the variable
is passed to the function as parameter.
The value of the actual parameter can be modified by
formal parameter.
Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
5 November
2023
22
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
23. In this program, the address of the variables “m” and “n” are
passed to the function “swap”.
The formal parameters “a” and “b” in swap function hold the
address of variables “m” and “n”
These addresses are used to access and change the values of
the actual variables.
Any changes made through formal parameters will change the
values of actual parameters
5 November
2023
23
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
EXAMPLE FOR CALL BY ADDRESS:
#include<stdio.h>
void swap(int * , int *);
int main()
{
int m = 22, n = 44;
printf(“Before swap, m = %d and n = %d",m, n);
swap(&m, &n);
printf(“nAfter swap m = %d and n = %d",m, n);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Output:
Before swap, m = 22and n = 44
After swap m, = 44and n = 22
24. Definition:
A function that calls itself is known as recursive function and the process of
calling function itself is known as recursion in C programming.
5 November
2023
24
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
RECURSION:
Example 1: Write a C program to find sum of first n natural numbers using
recursion.
#include<stdio.h>
int sum(int);
int main(){
int num,add;
printf("Enter a positive integer:n");
scanf("%d",&num);
add=sum(num);
printf(“Sum of first %d natural numbers=%d", num,add);
}
int sum(int n){
if(n = = 0)
return n;
else
return n+sum(n-1); //recrusive function call
}
Output:
Enter a positive integer: 5
Sum of first 5 natural
numbers=15
25. 5 November
2023
25
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
• For better visualization of recursion in this example:
• Every recursive function must be provided with a way to end the recursion.
In this example when, n is equal to 0, there is no recursive call and recursion
ends.
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
26. 5 November
2023
26
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Example2: Source code to find factorial of a number using recursion.
#include<stdio.h>
int factorial(int);
int main()
{
int n;
printf("Enter an positive integer:");
scanf("%d",&n);
printf("Factorial of %d = %ld",n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n = =1)
return 1;
else
return n*factorial(n-1); //recursive function call
}
Output:
Enter a positive
integer: 6
Factorial of 6 = 720
27. 5 November
2023
27
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Example2: Source code to find GCD using recursion.
include <stdio.h>
int gcd(int , int);
int main() {
int n1, n2,num,den;
printf("Enter two positive integers:");
scanf("%d %d", &n1, &n2);
if(n1>n2) {
num=n1;
den=n2;
}
else {
num=n2;
den=n1;
}
printf("G.C.D is %d.", gcd(num, den));
return 0;
}
int gcd(int num, int den)
{
if (den != 0)
return gcd (den, num% den );
else
return num;
}
Output:
Enter two positive integers: 24 8
G.C.D is 8.
28. 5 November
2023
28
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
RECURSION VS ITERATION
Recursion
In recursive approach, the
function callsitself until
the condition is true. It is
slower than iteration.
It uses more memory than
iteration.
Tracing the code will be
more difficult inthe case
large programs.
Iteration
Here code may be
longer but it isfaster
than recursive.
It consumes less
memory than
recursion.
Tracing the code is easy.
29. In C language, each variable has a storage class
which decides the following things:
scope: It tells where the value of the variable
would be available to access inside a program.
default initial value: It tells the default initial value
of a variable, if we do not explicitly initialize it.
lifetime :It tells for how long will that variable exist
in memory.
There are 4 storage classes in C :
1. auto
2. static
3. extern
4. register
5 November
2023
29
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
STORAGE CLASSES IN C
30. 1. Automatic variables: auto
A variable declared inside a function without any
storage class specification, is by default an automatic
variable.
They are created when a function is called and are
destroyed automatically when the function's execution
is completed.
By default they are assigned garbage value by the
compiler.
Scope: Variable defined with auto storage class are local
to the function block inside which they are defined.
Default Initial Value: Any random value i.e garbage
value.
Lifetime: Till the end of the function/method block where
the variable is defined.
5 November
2023
30
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
31. Example-1
#include <stdio.h>
int main()
{
int a; //auto
char b; //auto
float c; //auto
// printing initial default value of variables a, b, and c.
printf(“a=%d b=%c c=%f",a,b,c);
return 0;
}
5 November
2023
31
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Output:
a=garbage b=garbage c=garbage
32. Example-2
#include <stdio.h>
int main() {
int a = 10,i;
printf("%d ",++a); // 11 will be printed
{
int a = 20;
for (i=0;i<3;i++)
printf("%d ",a); // 20 will be printed 3 times
}
printf("%d ",a); // 11 will be printed
return 0;
}
5 November
2023
32
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Output:
11 20 20 20 11
33. 2. Static variables: static
A static variable tells the compiler to persist/save the value
of the variable until the end of program.
Instead of creating and destroying a variable every time
when it comes into and goes out of scope, static variable is
initialized only once and remains into existence till the end of
the program.
A static variable can either be local or global depending
upon the place of declaration.
Scope of local static variable remains inside the function in
which it is defined. Global static variables remain restricted to
scope of file in which they are declared.
Scope: Local to the block in which the variable is defined
Default initial value: 0(Zero).
Lifetime: Till the whole program doesn't finish its execution.
5 November
2023
33
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
34. Example-1:
#include <stdio.h>
int main()
{
static int a; //static
static char b; //static
static float c; //static
// printing initial default value of variables a, b, and c.
printf(“a=%d b=%c c=%f",a,b,c);
return 0;
}
5 November
2023
34
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Output:
a=0 b=0 c=0.000000
35. Example-2
#include<stdio.h>
void sum();
int main()
{
int i;
/*The static variables holds their value between multiple function calls.*/
for(i=0;i<3;i++)
sum();
return 0;
}
void sum()
{
static int a=10;
static int b=24;
printf("a=%d b=%dn",a,b);
a++;
b++;
}
5 November
2023
35
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Output:
a=10 b=24
a=11 b=25
a=12 b=26
36. 3. External variables: extern
The external storage class is used to tell the compiler that the
variable defined as extern is declared with an external linkage
elsewhere in the program.
The variables declared as extern are not allocated any memory. It
is only declaration and intended to specify that the variable is
declared elsewhere in the program.
The default initial value of external integral type is 0 otherwise null.
We can only initialize the extern variable globally, i.e., we can not
initialize the external variable within any block or method.
An external variable can be declared many times but can be
initialized at only once.
Scope: Global i.e everywhere in the program. These variables are
not bound by any function, they are available everywhere.
Default initial value: 0(zero).
Lifetime: Till the program doesn't finish its execution, you can
access global variables.
5 November
2023
36
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
37. Example:
First File: file1.c
#include <stdio.h>
int a;//global variable
void fun()
{
a++;
printf(“File1: a=%d”,a);
}
5 November
2023
37
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Output:
File1: a=1
File2: a=6
Second File: file2.c
#include <stdio.h>
#include”file1.c”
int main()
{
extern int a;// external variable
fun();
a+=5;
printf("nFile2: a=%d",a);
return 0;
}
38. 4. Register variables: register
Register variables inform the compiler to store the variable in CPU
register instead of memory.
Register variables have faster accessibility than a normal variable.
We cannot get the address of register variables.
Generally, the frequently used variables are kept in registers. But
only a few variables can be placed inside registers.
One application of register storage class can be in using loops,
where the variable gets used a number of times in the program, in a
very short span of time.
Scope: Local to the function in which it is declared.
Default initial value: Any random value i.e garbage value
Lifetime: Till the end of function/method block, in which the variable
is defined.
5 November
2023
38
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
39. Example:
#include <stdio.h>
int main()
{
register int age; //register variable
int *ptr=&age ; /*it produces an error as we cannot get a memory
location of CPU register*/
printf("Success!!!");
return 0;
}
5 November
2023
39
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
Output:
main.c:13:5:
error: address of register variable ‘age’ requested
int *ptr=&age
^
40. Whenever we need to pass a list of elements as argument to any
function in C language, it is preferred to do so using an array.
An array is passed to a function by using call by address
mechanism.
Ex:- float findAverage (int marks[ ]);
/*passing the array arr to the function sum()*/
5 November
2023
40
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
PASSING AN ARRAY TO FUNCTION:
Example Program:
#include<stdio.h>
float findAverage(int marks[ ]);
int main() {
float avg;
int marks[ ] = {99, 90, 96, 93, 95};
avg = findAverage(marks);
//name of the array is passed as argument.
printf("Average marks = %.lf", avg);
return 0;
}
float findAverage(int marks[ ])
{
int i, sum = 0;
float avg;
for (i = 0; i <= 4; i++)
sum += marks[i];
avg = (sum / 5);
return avg;
}
Output:
94.6
41. When we pass a pointer as an argument instead of a variable then
the address of the variable is passed instead of the value.
So, any change made by the function using the pointer is
permanently made at the address of passed variable.
A function can also return a pointer to the calling function.
Ex:- int* larger(int*, int*);
larger() function takes two integer pointers as parameters
and returns one integer pointer as result.
5 November
2023
41
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
PASSING POINTER TO FUNCTION:
Example Program:
#include <stdio.h>
int* larger(int*, int*);
int main()
{
int a = 15, b = 92;
int *p;
p = larger(&a, &b);
printf("%d is larger",*p);
}
int* larger(int *x, int *y)
{
if(*x > *y)
return x;
else
return y;
}
Output:
92 is larger
42. The arguments passed from command line are called
command line arguments. These arguments are
handled by main() function.
To support command line argument, you need to
change the structure of main() function as given below.
Syntax: int main(int argc, char *argv[] )
argc counts the number of arguments on the
command line.
argv[ ] is a pointer array which holds pointers of type
char which points to the arguments passed to the
program.
5 November
2023
42
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
COMMAND LINE ARGUMENTS IN C
43. #include <stdio.h>
int main( int argc, char *argv[] )
{
if( argc >= 2 )
{
printf("The argument supplied are:n”);
for(i = 1; i < argc; i++)
printf("%st", argv[i]);
}
else
printf("argument list is empty.n");
return 0;
}
5 November
2023
43
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
EXAMPLE PROGRAM FOR COMMAND LINE ARGUMENTS IN C
During compilation, pass the below arguments
through Command line
Mokshith 19 Male
Output:
The argument supplied are:
Mokshith 19 Male
44. A file is an external collection of related data treated as
a unit.
The primitive task of using a file is to keep a record of
data.
The contents of primary memory are lost when the
computer is shut down, so we need files to store our
data in a more permanent form.
Files are stored in auxiliary or secondary storage
devices. The two most common forms of secondary
storage are disk (hard disk, CD, and DVD) and tape.
A buffer is a temporary storage area that holds data
while they are being transferred to or from memory.
The primary purpose of a buffer is to synchronize the
physical devices with program’s need.
5 November
2023
44
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)
FILES IN C
45. Text vs Binary files
C uses two types of streams: text and binary.
A text stream consists of a stream of characters divided
into lines with each line terminated by a new- sequence
line (n).
A binary stream consists of a sequence of data values
such as integer, real, or complex using their memory
representation.
In text files, everything is stored in terms of text i.e. even
if we store an integer 54; it will be stored as a 5- byte
string - “540”.
A binary file contains data that was written in the same
format used to store internally in main memory. For
example, the integer value 1245 will be stored in 4 bytes.
5 November
2023
45
M. Rajesh Reddy, Assistant Professor, Dept of CSE- KITS (Autonomous)