66 Programming and Data Structures
In the above example the integer variable 'y ' contains value 65. The variable 'y ' is printed using two-
conversic-n symbols integer and character. As shown in the output, the %c converts numeric 65 value
to its corresponding character A. The %d prints the value 65, as it is, because the variable is of an integer
type. Sometimes if no conversion is possible between two data types; some garbage value is printed.
Example
# include <stdio.h>
main (7
{
in t y« 7 ;
c lr s c r ( );
printf("%f',y );
return 0;
I
In the above example, we forced the p r in tf statement to print the integer value as float value
providing %f as a conversion symbol. This is not the proper way. While compiling no error occurs, but
after execution th e p r i n t f () function will produce an error message "floating points formats not
linked
b) The scant () statem ent The scan f () statement reads all types of data values. It is used for
runtime assignment of variables. The scanf () statement also requires conversion symbol to identify
the data to be read during the execution of a program. The syntax of the scanf () statement is same as
p rin tf () except they work exactly opposite.
Syntax scanf ("%d %f He” , &a,&b,&c);
The scanf () statement requires ' &' operator called address operator. The address operator prints
the memory location of the variable. Here, in the sc a n f () statement the role of ' &' operator is to
indicate the memory location of the variable, so that the value read would be placed at that location.
The scan f () statement also returns values. The return value is exactly equal to the number of
values correctly read. In case of any mismatch, error will be shown. Otherwise if the read value is
converted to the given format, conversion is made. The following program shows the example of such
a mismatch case.
4JL Write a program to show the effect of mismatch of data types.
9 include <stdio.h>
# include cconio. h>
void main ()
{
in t a ;
c lrs c r () ;
printf ("Enter value of'A ':");
scanf ("%c",&a);
printf ("A=%c"fa);
I
OUTPUT:
Enter value of 'A ': 8
A=8
Input and Output in C 67
Explanation In the above program though the type of variable a is in t. But it perfectly works with
conversion symbol of character i.e. character and integer data types are compatible to each other.
When the two data types are compatible to each other, then the compatible range is equal to the lowest
range from the two data types. The above example illustrates this point.
4.2 Write a program to read and print the integer value using character variable.
# include <stdio.h>
# include <conio.h>
void main ()
{
char a;
c lr s c r ();
printf ("Enter value of'A ':);
scanf (% d" ,& a);
printf ("A=%d",a);
I
OUTPUT:
Enter value of 'A ': 255
A=255
Enter value of 'A ': 256
A=0
Explanation In the above program variable' a ' is of character type i.e. its valid range is 0 to 255. The
variable ' a ' is used with conversion symbol of integer data type i.e. in th ep rin tf () and scanf ()
statement the variable ' a ' is supposed to be as an integer type. The value read in first execution is
valid. Hence it is printed as it is read. In the second execution the value read is greater than the range
of character type. In such case, the excess range is again considered as a beginning or a starting point.
Here the excess value is 1. That's why 0 is printed.
The p r i n t f () sc a n f () statements follow the different data types which are listed in the
Table 4.2.
Data Type
Conversion Symbol
integer
short integer
%d or %i
short unsigned
%u
long signed
%ld
long unsigned
%lu
unsigned hexadecimal
%x
unsigned octal
%o
real
float
%for%g
double
%lf
character
signed character
%c
unsigned character
%c
string
%s
68 Programming and Data Structures
Table 4.2 Data types with Conversion Symbols
Octal Number
%o
Displays Hexa decimal Number in small case
%hx
Displays Hexa decimal Number in upper case
%>
Aborts program with error
%n
The p r i n tf () and sc a n f () statements follow the combination of characters called as escape
sequences. They are given in the Table 4.3.
Table 4.3 Escape Sequences with their ASCII values
Escape Sequence Use
ASCII Value
n
New line
10
b
Backspace
8
f Formfeed
12
' Single quote
39
Backslash
92
0 Null
0
t
Horizontal Tab
9
r
Carriage Return
13
a
Alert
7
"
Double quote
34
v
Vertical tab
11
?
Question Mark
63
43 Write a program to show the effect of various escape sequences.
# include <stdio.h>
# include <conio.h>
mainO
{
int aBl,baa+l,cab+l,dsc+l;
clrscr();
printf (" A=%d B=%d ,C=%d,",arbrc);
printf (“ a***D=%d**'',d);
printf
printf (“ A=%d B=%d",a,b);
)
OUTPUT:
A=1
B=2 'C=3'
**D=4**
A=1
Input and Output in C 69
Explanation In the above program, few commonly used escape sequences are described.
1. In the firstp rin tf () statement 1 t ' prints the value of 'a ' at second tab stop on the screen. The
1 ' splits the line and prints the value of B and C on the next line.
2. In the second p r in t f () statement the three prefix are written followed by the Jb'. The 1 Jb'
overwrites the last character. The last is erased. The output D=4 will be displayed.
3. In the third p rin t f () statement only sequence of are printed, but in the output only half line is
displayed because it is affected by the fourth p r in tf () statement.
4. In the fourth pr in t f () statement 1 r ' is used which reverses the printable area one line before
from the current location. Hence, the line generated by the third statement is replaced by the output
of the fourth statement.
The programs illustrated below u sep rintf () and scan f () statements.
4.4 Write a program to print the third power of 2 using pow() function. Assume the floating
point numbers.
#include <math.h>
#include <stdio.h>
void main ()
{
double x = 2.0, y = 3.0;
c l r s c r ( ) ;
printf("%lf raised to %lfis %lftt",x, y, pow(x, y))r (
O U T P U T :
2.000000 raised to 3.000000 is 8.000000
Explanation In the above program two variable x and y are declared and initalized. In the p r in t f ()
statement using pow () function expressionxy is calculated and displayed.
4.5 Write a program to print the third power of 10 using powlOO function. Assume the
floating-point numbers.
#include <math.h>
#include <stdio.h>
main (void)
{
in t p = 3;
prititf("Ten raised to %lfis %lf t", p, powlO(p));
O U T P U T:
Ten raised to 3.000000 is 1000.000000
Explanation In the above program power of 10 is calculated. Here, p is declared as an integer data
type. The value returned by this function is of double data type. Hence, conversion symbol %1 f is used.
70 Programming and Data Structures
4.6 Write a program to detect an error while inputting a data. Use return value of scanf
statement.
# include <stdio.h>
# include <conio.h>
void main ()
{
in t a ,b ,c ,v ;
clrs c r ();
printf ("Enter value of 'A '/Br &c 'C ': ");
v=scanf("%d %d %d"/&a,&b,&c);
( v<3 ?printf (" Error In Inputting."); printf (" Values Successfully read."));
OUTPUT:
Enter value o f' A'/B' & 'C : 1 2 3
Value Successfully read.
Enter value of 'A'/B' & 'C ': 1 J 2
Error In Inputting.
Explanation In the above program th eprin t f () statement returns the value equal to the number of
variables correctly read. The condition ? statement checks the value of variable * v ' and prints the
respective messages.
4.7 Write a program to find length of the string using printfO function.
# include <stdio.h>
# include <conio.h>
mainO
{
char nm[20] ;
in t 1;
c lr s c r ();
printf ("Enter String:");
scanf(“%s",nm):
l-printf(nm);
printf ("nLength = %d",l);
I
OUTPUT:
Enter String : HELLO
Length = 5
Explanation The p r in tfO function returns the length of the string entered. In the above program
the string entered is "HELLO. " Length of the string is 5, which is stored in the variable T.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.194