0% found this document useful (0 votes)
26 views14 pages

Input Output Stmt1

Uploaded by

Raghu M E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views14 pages

Input Output Stmt1

Uploaded by

Raghu M E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Continued….

2.4.2 Inputting Real Numbers


 Unlike integer numbers, the field width of real numbers is not to be specified and
therefore scanf reads real umbers using the simple specification %f for both the
notations, namely, decimal point notations and exponential notation. For example, the
statement
Scanf(“%f %f %f”, &x, &y, &z);
With the input data
475.89 43.21E-1 678
 Will assign the value 4755.89 to x, 4.321 to y, and 678.0 to z. The input field
specifications may be separated by any arbitrary blank spaces.
 If the number to be read is of double type, then the specification should be %If
instead of simple %f. A number may be skipped using %*f specification.
2.4.3 Inputting Character Strings

 The single character can be read from the terminal using the getchar function.

 The same can be achieved using the scanf function also.

 In addition, a scanf function can input strings containing more than one character.

 Following are the specifications for reading character strings:

%ws or %wc

 The corresponding argument should be a pointer to a character array.

 However, %c may be used to read a single character when the argument is a


pointer to a char variable.
2.4.4 Reading Mixed Data Types
 It is possible to use one scant statement to Input a data line containing mixed mode
data.
 In such cases care should be exercised to ensure that the input data Items match
the control specifications in order and type.
 When an attempt is made to read an item that does not match the type expected the
scanf function does not read any further and immediately returns the values read.
 The statement
Scanf (“%d %c %f %s”, &count, &code, &ratio, name);
Will read the data
15 p 1.575 coffee
 Correctly and assign the values to the variable in the order in which they appear.
Some systems accepts integers in the place of real numbers and vice versa, and the
Input data is converted to the specification, in the control string.
2.4.5 Detection of Errors in Input
 When a scanf function completes reading its list, if returns the value of number of items
that are successfully read.
 This value can be used to test whether any errors occurred in reading the input.
 For example, the statement

Scanf(“%d %f %s”, &a, &b, name);

will return the value 3 If the following data is typed in:

20 150. 25 motor

and will return the value 1 if the following line is entered

20 mot or 150.25

This is because the function would encounter a string when it was expecting a floating-point
value and would therefore terminate Its scan after reading the first value.
Table: Commonly used scanf format codes.
Given below are some of the general points to keep in mind while
writing a scanf Statement

 All function arguments, except the control string. Must be pointers to


variables.
 Format specifications contained in the control string should match the
arguments in 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 scant encounters a 'mismatch' of data or
a character that is not valid for the value being read.
 When searching for a value. scant Ignores line boundaries and simply looks
for the next appropriate character.
 Any unread data items In a line will be considered as part of the data input line
to the next scant call.
 When the field width specifier w Is used, It should be large enough to contain
the input data size.
2.5 Formatted Output

 The use of printf function for printing captions and numerical results.
 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 his program.
 The printf statement provides certain features that can be effectively exploited
to control the alignment and spacing of print-outs on the terminals.
 The general form of printf statement is
printf(“control string”, arg1, arg2, ….., argn);
 Control string consists of following three types of items:
1. Characters that will be printed on the screen as they appear.
2. Format specifications that define the output format for display of each item.
3. Escape sequence characters such as \n, \t, and \b.
 Some examples of formatted printf statement are:
printf (“ programming in c”);
printf (“ “);
printf (“\n”);
printf (“%d”, x);
printf (“a = %f\n b = %f”, a, b);
printf (“ sum = %d”, 1234);
printf (“\n\n”);
2.5.1 Output of Integer Numbers

 The formal specification for printing an Integer number is:


%wd
 Where w specifies the minimum field width for the output.
 However, if a number is greater than the specified field width, it will be
printed in full, overriding the minimum specification.
 d specifies that the value to be printed Is an integer.
 The number is written right-justified in the given field width. Leading blanks
will appear as necessary.
2.5.2 Output of Real Numbers
 The output of a real number may be displayed in decimal notation using the
following format specification:
%w.p f
 The integer w indicates the minimum number of position that are to be used
for the display of the value and the integer p indicates the number of digits to
be displayed after the decimal point. The value, when displayed, is rounded
top decimal places and printed right-justified in the field of w columns.
 We can also display a real number in exponential notation by using the
specification:
% w.p e
 The display takes the form
[ - ]m.nnnne[+-]xx
 Where the length of the string of n’s is specified by the precision p. The
default precision is 6 the field width w should satisfy the condition.
2.5.3 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 the integer w. The default value
for w is 1.

2.5.4 Printing of Strings


The format specification for outputting strings is similar to that of real numbers. It is of
the form
%w.ps
Where w specifies the field width for display and d instructs that only the first p
characters of the string are to be displayed. The display is right-justified.
2.5.5 Mixed Data Output

 It is permitted to mix data types in one printf statement.


 For example, the statement of the type is valid.
printf(“%d %f %s %c”, a, b, c, d);
 As pointed out earlier, printf uses its control string to decide how many
variables to be printed and what their types are.
 Therefore, the format specifications should match the variables in number
order, and type.
 If there are not enough variables or if they are of the wrong type, the output
results all be incorrect.
Table: Commonly used printf format codes.
Table: Commonly used Output format flags.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy