Text Input / Output:: Programming For Problem Solving Using C Unit-V
Text Input / Output:: Programming For Problem Solving Using C Unit-V
USING C 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.
Text Input / Output:
Files: -
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.
Standard Library I/O Functions: -
WWW.JNTUKNOTES.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:
printf() is output to the screen
scanf() is read input from the screen
getchar() is return characters typed on screen
putchar() is output a single character to the screen
fopen() is open a file, and
fclose() is close a file
Formatting Input /Output Functions:-
Formatted Input
The function scanf() is used for formatted input from
standard input and provides many of the conversion
facilities of the function printf().
Syntax
scanf (format, num1, num2,……);
The function scanf() reads and converts characters from the
standards input depending on the format specification string
and stores the input in memory locations represented by the
other arguments (num1, num2,….).
For Example:
scanf(“ %c %d”,&Name, &Roll No);
Character Input / Output Functions:-
Character input functions is used to input a single character.
All these functions are available in 'stdio.h' header file.
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.JNTUKNOTES.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 Input / Output:-
Text versus Binary Streams:-
Text:-
A text stream consists of one or more lines of text that can be written to a
text-oriented display so that they can be read.
When reading from a text stream, the program reads an NL (newline) at the
end of each line.
Writing to a text stream, the program writes an NL to signal the end of a line.
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.JNTUKNOTES.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.JNTUKNOTES.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);
WWW.JNTUKNOTES.COM 5
C Header Files
Function description
WWW.JNTUKNOTES.COM 6
putw() writes a integer to a file
Converting File Type:-
Type conversion refers to changing a variable of
one data type into another.
For instance, if you assign an integer value to a floating-
point variable, the compiler will convert the int to a float.
Casting allows you to make this type conversion explicit, or
to force it when it wouldn't normally happen.
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.JNTUKNOTES.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.JNTUKNOTES.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
Standard library functions:-
Passing Array to Functions:-
Just like variables, array can also be passed to a function as an
argument. In this guide, we will learn how to pass the array to a
function using call by value and call by reference methods.
WWW.JNTUKNOTES.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.JNTUKNOTES.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.JNTUKNOTES.COM 11
Value of var_arr[6]is:77
Passing pointer to function:-
In this example, we are passing a pointer to a function. 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. This technique is known as call by reference in C.
#include<stdio.h>
voidsalaryhike(int *var, int b)
{
*var = *var+b;
}
int main()
{
int salary=0, bonus=0;
printf("Enter the employee current salary:");
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
return0;
}
Output:
Enter the employee current salary:10000
Enter bonus:2000
Final salary: 12000
WWW.JNTUKNOTES.COM 12