PPS Using C R20 - UNIT-5
PPS Using C R20 - UNIT-5
PPS Using C R20 - UNIT-5
UNIT-V
Text Input / Output: Files, Streams, Standard Library Input / Output Functions, Formatting Input /
Output Functions, Character Input / Output Functions, Tips and Common Programming Errors, Key
Terms, Summary, Practice Set.
Binary Input / Output: Text versus Binary Streams, Standard Library, Functions for Files, Converting
File Type, Tips and Common Programming Errors, Key Terms, Summary, Practice Set.
Functions: Designing, Structured Programs, Function in C, User Defined Functions, Inter-Function
Communication, Standard Functions, Passing Array to Functions, Passing Pointers to Functions,
Recursion, Passing an Array to Function, Tips and Common Programming Errors, Key Terms,
Summary, Practice Set.
Streams:-
In C all input and output is done with streams.
Stream is nothing but the sequence of bytes of data.
A sequence of bytes flowing into program is called input stream.
A sequence of bytes flowing out of the program is called output
stream.
Use of Stream make I/O machine independent.
www.Jntufastupdates.com 1
The standard input and output library is stdio.h, and you will
find that you include this library in almost every program you
write.
It allows printing to the screen and collecting input from the
user. The functions you will use the most include:
Syntax
For Example:
getch(): Use to input single character at a time. But it will not display input
character. get stands for input, ch stands for character.
Syntax: char a = getch();
www.Jntufastupdates.com 2
Character Output Functions:-
putch(): use to print a single character.
Syntax: putch(a);
putchar(): use to print a single character.
Syntax: putchar(a);
Binary Stream:-
A binary stream consists of one or more bytes of arbitrary information.
You can write the value stored in an arbitrary object to a (byte-oriented) binary
stream and read exactly what was stored in the object when you wrote it.
The library functions do not alter the bytes you transmit between the program
and a binary stream.
They can, however, append an arbitrary number of null bytes to the file that
you write with a binary stream.
The program must deal with these additional null bytes at the end of any
binary stream.
www.Jntufastupdates.com 3
Standard Library:-
The prototype and data definitions of these functions are present in their
respective header files. To use these functions we need to include the
header file in our program. For example,
If you want to use the printf() function, the header file <stdio.h> should
be included.
#include<stdio.h>
int main()
If you try to use printf() without including the stdio.h header file, you will
get an error.
One of the most important reasons you should use library functions is
simply because they work. These functions have gone through multiple
rigorous testing and are easy to use.
www.Jntufastupdates.com 4
4. The functions are portable
To can compute the square root of a number, you can use the sqrt() library function. The function is
defined in the math.h header file.
#include<stdio.h>
#include<math.h>
int main()
{
floatnum, root;
printf("Enter a number: ");
scanf("%f", &num);
root = sqrt(num);
C Header Files
www.Jntufastupdates.com 5
C Header Files
Function description
www.Jntufastupdates.com 6
putw() writes a integer to a file
Functions:-
Designing:-
Functions are an essential ingredient of all programs, large and small,
and serve as our primary medium to express computational processes in
a programming language. So far, we have discussed the formal
properties of functions and how they are applied. We now turn to the
topic of what makes a good function. Fundamentally, the qualities of
good functions all reinforce the idea that functions are abstractions.
Each function should have exactly one job. That job should be
identifiable with a short name and characterizable in a single line of
text. Functions that perform multiple jobs in sequence should be
divided into multiple functions.
Don't repeat yourself is a central tenet of software engineering. The
so-called DRY principle states that multiple fragments of code
should not describe redundant logic. Instead, that logic should be
implemented once, given a name, and applied multiple times. If you
find yourself copying and pasting a block of code, you have
probably found an opportunity for functional abstraction.
www.Jntufastupdates.com 7
Functions should be defined generally. Squaring is not in the
Python Library precisely because it is a special case of
the pow function, which raises numbers to arbitrary powers.
Structured programs:-
Structured programming is a programming paradigm aimed at
improving the clarity, quality, and development time of a
computer program by making extensive use of the structured control
flow constructs of selection (if/then/else) and repetition (while and
for), block structures, and subroutines.
Function in C:-
A function is a block of statements that performs a specific task.
Suppose you are building an application in C language and in one of
your program, you need to perform a same task more than once. In such
case you have two options –
a) Use the same set of statements every time you want to perform the
task
b) Create a function to perform that task, and just call it every time you
need to perform that task.
Using option (b) is a good practice and a good programmer always uses
functions while writing codes in C.
Types of functions
1) Predefined standard library functions – such
as puts(), gets(), printf(), scanf() etc – These are the functions which
already have a definition in header files (.h files like stdio.h), so we just
call them whenever there is a need to use them.
2) User Defined functions – The functions that we create in a
program are known as user defined functions.
Inter-Function Communication:-
www.Jntufastupdates.com 8
When a function gets executed in the program, the execution control is
transferred from calling a function to called function and executes
function definition, and finally comes back to the calling function. In this
process, both calling and called functions have to communicate with
each other to exchange information. The process of exchanging
information between calling and called functions is called inter-function
communication.
In C, the inter function communication is classified as follows...
Downward Communication
Upward Communication
Bi-directional Communication
www.Jntufastupdates.com 9
Passing array to function using call by value method
As we already know in this type of function call, the actual parameter is
copied to the formal parameters.
#include<stdio.h>
voiddisp(charch)
{
printf("%c ",ch);
}
int main()
{
chararr[]={'a','b','c','d','e','f','g','h','i','j'};
for(int x=0; x<10; x++)
{
/* I’m passing each element one by one using subscript*/
disp(arr[x]);
}
return0;
}
Output:
abcdefghij
int main()
{
intarr[]={1,2,3,4,5,6,7,8,9,0};
for(inti=0;i<10;i++)
{
/* Passing addresses of array elements*/
disp(&arr[i]);
}
www.Jntufastupdates.com 10
return0;
}
Output:
1234567890
#include<stdio.h>
voidmyfuncn(int*var1,int var2)
{
/* The pointer var1 is pointing to the first element of
* the array and the var2 is the size of the array. In the
* loop we are incrementing pointer so that it points to
* the next element of the array on each increment.
*
*/
for(int x=0; x<var2; x++)
{
printf("Value of var_arr[%d] is: %d \n", x,*var1);
/*increment pointer for next element fetch*/
var1++;
}
}
int main()
{
intvar_arr[]={11,22,33,44,55,66,77};
myfuncn(var_arr,7);
return0;
}
Output:
Value of var_arr[0]is:11
Value of var_arr[1]is:22
Value of var_arr[2]is:33
Value of var_arr[3]is:44
Value of var_arr[4]is:55
Value of var_arr[5]is:66
www.Jntufastupdates.com 11
Value of var_arr[6]is:77
www.Jntufastupdates.com 12