100% found this document useful (1 vote)
156 views

Chapter-4: Managing Input and Output Operation

This document discusses input and output functions in C programming. It explains that C does not have built-in input/output statements and describes functions like scanf(), printf(), getchar(), and putchar() that are used for input and output. It provides examples of using these functions to read integer, real, character, and string input and control the formatting of output.

Uploaded by

vinod_ms
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
156 views

Chapter-4: Managing Input and Output Operation

This document discusses input and output functions in C programming. It explains that C does not have built-in input/output statements and describes functions like scanf(), printf(), getchar(), and putchar() that are used for input and output. It provides examples of using these functions to read integer, real, character, and string input and control the formatting of output.

Uploaded by

vinod_ms
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 55

Chapter-4

Managing input
and
Output operation
 Reading, processing and writing of
data are three essential functions of a
computer program.

 Most programs take some data as


input and display the processed data
or result as output.

 So, we need some methods that can


read input and write output on a
suitable input/output medium.
 Bcoz unlike other high-level
languages, C does not have any built-
in input/output statements as part of
its syntax.
Input/output functions
 Thus far we have seen two methods for
providing data to the program variables.

1. One is to assign values to variables through


the assignment statements such as x = 5; and
so on.

2. And another method is to use input function


scanf which can read data from a terminal.

• And for outputting the results we have used


the function printf which sends the results out
to a terminal.
Reading a character
 There are also some functions other than
printf
and scanf which can be used as
input/output
functions.

 The simplest of all input/output operations


is
reading a character from the standard
input
and writing it to the standard output unit.
 Reading a single character can also be done
by
using the function getchar().
The getchar() function
The getchar() function is used to read a
single character from the standard input
unit.

It takes the following form:


variable_name = getchar();
 Variable_name is a valid C name that
has been declared as char type.

 When this statement is encountered,


the computer waits until a key is
pressed and then assigns this
character as a value to the getchar
function.
Example
Since getchar is used on the right-hand
side of an assignment statement, the
character value of getchar is assigned
to the variable name on the left side.
For example:
char name;
name = getchar();
Will assign the character ‘A’ to the
variable name when we press the key
A on the keyboard.
 Here, We Will assign the character ‘A’
to the variable name when we press
the key A on the keyboard.
 The getchar() function may be called
successively to read the characters
contained in a line of text.

 The getchar() function returns any


character keyed in. this include
RETURN and TAB also. This means
that it will not return, until u press
ENTER.
Other functions
 We can also determine whether the
character typed from the keyboard is
a letter or digit.
 These tests can be done with the help
of the following functions:
isalpha(character)
isdigit(character)
 isalpha function assumes a value non-
zero(TRUE) if the argument character
contains an alphabet; otherwise it
assumes 0(FALSE). Similar for isdigit
function.
 These all character functions are
contained in the file ctype.h and
therefore the statement
#include<ctype.h>
Must be included in the program.

 There are many other similar


functions contained in this file.
1.isalnum(c) - Is c an alphanumeric
character?
2.isalpha(c) – Is c an alphabetic
character?
3. isdigit(c) – Is c a digit?
4. islower(c) – Is c a lower case letter?
5. isprint(c) –Is c a printable character?
6. isspace(c) –Is c a white space
character?
7. isupper(c) – Is c an upper case letter?
Writing a character
 Like getchar, there is an analogous
function putchar for writing
characters one at a time to the
terminal.
 It takes the following form:
putchar(variable_name);
 where variable_name is a type char
variable containing a character.
This statement displays the
character contained in the
variable_name at the terminal.
Example:
char answer = ‘Y’;
putchar(answer);
 It will display the character Y on the
screen.
 The statement putchar(‘\n’); would
cause the cursor on the screen to move
to the beginning of the next line.
Formatted Input
 Formatted input refers to an input
data that has been arranged in a
particular format.

 For inputting the data, we use the


scanf function. The general form of this
scanf is :
Its..
scanf(“controlString”,arg1,arg2,…argn);
 Conrol string specifies the field format
in which the data is to be entered.
 Arguments arg1, arg2,…..specify the
address of locations where the data is
stored.
 The field (or format) specifications,
consisting of the conversion character
%, a data type character(or type
specifier), and an optional number,
specifying the field width.
Inputting Integer Numbers
 The field specification for reading an
integer number is:
%wd
 w is an integer number that specifies
the field width of the number to be
read and d, known as data type
character.
Example:-
scanf(“%2d%5d”,&num1,&num2);
Input: 50 31426
 Then,
50 31426

num1 num2
But if input : 31426 50

31 426

num1 num2

And the value 50 that is unread will be


assigned to the first variable in the
next scanf call.
 On the other way if we have written :
Scanf(“%d%d”,&num1,&num2);
Will read the data 31426 50
Correctly in num1, num2.
 Input data items must be separated by
spaces,tabs or new lines.
 When the scanf reads a particular value,
reading of the value will terminate as soon
as the number of characters specified by
the field width is reached(if specified) or
until a character that is not valid for the
value being read is encountered.
 An input field may be skipped by specifying
* in the place of field width.
For example,

Scanf (%d%*d%d”,&a,&b);

Input: 123 456 789


123 456 789
a skipped b
The data character d may be preceded
by ‘l’ to read long integers. (%ld)
Inputting Real Numbers
 The field width of real numbers is not to be
specified and therefore scanf uses simple
specification %f for both the notations,
decimal point and exponential notation.
 For double type , %lf is used instead of %f.
 A number may be skipped using %*f
specification.
Inputting Character Strings

 Single character can be read from the


terminal using getchar function. The
same can be achieved using the scanf
function also.
In addition, a scanf can input strings
containing more than one character.
The specifications for reading character
strings are:
%ws or %wc
%wc used to read a single character
while %ws terminates the reading at
the encounter of blank space.
All function arguments must be a
pointers to variables.
Format specifications contained in the
control string should match the
argument order.
Input data items must be separated by
spaces and must match the variables
receiving the input in the same order.
 The reading will be terminated, when
the scan encounters an ‘invalid
mismatch’ of data or a character that is
not valid for the value being read.
 When searching for a value, scanf
ignores line boundaries and simply looks
for the next appropriate character.
 Any unread data items in a line will be
considered as a part of the data input
line to the next scanf call.
 When the field width specifier w is
used, it should be large enough to
contain input data size.
 It is highly desirable that the outputs
are produced in such a way that they
are understandable and are in an
easy-to-use form.

 It is therefore necessary for the


programmer to give careful
consideration to the appearance and
clarity of the output produced by
program.
 printf statement provides certain
features that can be effectively control
the alignment and spacing of print-
outs on the terminals.
printf(“control string”,arg1,arg2....argn);
Control string consists of three types of
items:
1.Characters that will be printed on the
screen as they appear.
2.Format specifications that will define
the output format for display of each
item.
3.Escape sequence characters such as
\n, \t, and \b.
 The format specification for printing an
integer is %wd. Where w specifies the
minimum field width for the output.
Format Output
 Printf(“%d”,9876)
9 8 7 6

 Printf(“%6d”,9876)
9 8 7 6

 Printf(“%2d”,9876)
9 8 7 6
 Printf(“%-6d”,9876)
9 8 7 6

 Printf(“%06d”,9876)
0 0 9 8 7 6
 The output of real number may be displayed
in decimal notation using the format %w.pf
If y = 98.7654
Format Output
9 8 . Printf(“%7.4f”,y)
7 6 5 4

9 Printf(“%7.2f”,y)
8 . 7 7

9 8 .Printf(“%-7.2f”,y)
7 7

9 8 . 7 Printf(“%f”,y)
6 5 4
Printing of a single character
A single character can be displayed in
a desired position using the format %wc.
The character will be displayed right-
justified in the field of w columns.
We can make the display left-justified
by placing a minus sign before w.
The default value for w is 1.
Printing of strings
The format specification for outputting
string is of the form %w.ps.
To print string “NEW DELHI 110001”,
containing 16 characters.
%20s
1 10 2
0
To print string “NEW DELHI 110001”,
containing 16 characters.
%20s
N E W D E L H I 1 1 0 0 0 1

%20.10s
N E W D E L H I
Difference between
%[a-z]
%[^a-z]
sizeof operator
 when used with operand, it returns
the no. of bytes the operand
occupies…
i.e. sizeof(int) is returns 2
sizeof(char) returns 1 ..on 16bit
processor.
Q. float x; printf(“%d”,sizeof(x));
what will be the output? Why?
Precedence of arithmetic
operator
High priority * / %
Low priority + -
The basic evaluation includes left to right
passes…..
During first pass higher priority opr is
applied as they encounter….during
second pass from left to right the low
priority operators are applied as they
are encountered.
example
i.e
x = 9-12/3+3*2-1;
pass 1: x = 9 – 4 + 3*2 –1;
x = 9 – 4 + 6 –1;
Pass 2:
x= 5 +6 –1;
x=11-1;
x=10;
Use of parenthesis to override
normal precedence
X= 9 – 12 / (3+3) *(2-1)
Pass 0: x=9 – 12 / 6 * (2-1)
x=9 – 12 / 6 * 1;
Pass 1: x=9 – 2 * 1;
x=9 – 2;
Pass 2: x=7
Thus, when parenthesis are used, the
expressions within parenthesis assume
highest priority.
Associativity
The operator having same precedence
are evaluated either from left to right
or right to left…..depending of the
level … .known as associativity.
 There are two ways for type conversion.

1) Implicit Type Conversion

2) Explicit Type Conversion


 Automatic conversion.
e.g. a=3 b=2
int i;
float a,b;
i=a/b;
here, value of i will be 1. instead of 1.5
 (type-name) expression
Where, type-name is one of the
standard
C data types.
Expression may be a
constant, variable or an
expression.
 i=( int ) 7.5
Means, 7.5 is converted to integer by
truncation.
 i=( int )21.3/ ( int )4.5
Means, Evaluated as 21/4 and result would
be 5.
 Casting can be used to round-off a
given value.
HOW???
x = (int) (y+0.5);
if y = 27.6 then,
y+0.5=28.1 and on casting the result
becomes 28.

You might also like