CP UNIT - 5
CP UNIT - 5
COM
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.
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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();
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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.
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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.
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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);
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
C Header Files
Function description
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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.
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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:-
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
Downward Communication
Upward Communication
Bi-directional Communication
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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]);
}
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
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
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM
Value of var_arr[6]is:77
JNTUK396.BLOGSPOT.COM
JNTUK396.BLOGSPOT.COM