There
are a number of I/O functions in C, based on the data type. These functions are
classified in two types:
Fig 05: I/O Functions
a)
Formatted
functions
i.
The formatted input/output functions read and write,
respectively, all types of data.
ii.
They require format string to produce formatted
results.
b)
Unformatted
functions
i.
The unformatted input/output functions work only
with character data type.
ii.
They do not require format conversion symbol for
formatting of data types because they work only with character data type.
Formatted
Functions
1) printf( ) statement
§ The formatted output
as per the programmers requirement is displayed on the screen with printf ( ).
§ The list of
variables can be indicated in the printf ( ). The values of variables are
printed according to the sequence mentioned in the printf ( ).
§ Syntax:
printf (“Control
String”, v1, v2, …., vn);
The control
string specifies the field format such as %d, %s, %f, %s, and variables as
taken by the programmer. It also tells how the user wants the output to be
printed. The control string is also called format string.
Sample Code
void main ( )
{
int x = 2;
float y =
5.2;
char z =
‘A’;
printf
(“%d %f
%c”, x, y, z);
}
OUTPUT
2 5.200000
A
§ Width and
precision specifier
o
Width
specifier
It
sets the minimum field width for an output value. It is used along with integer
and character (string) data values.
void main ( )
{
int x = 55,
y = 33;
printf
(“\n%3d”, x-y);
printf
(“\n%6d”, x-y);
}
OUTPUT
2
|
2
|
2
|
2
|
void main ( )
{
printf
(“\n%10s”, “abcdef”);
printf
(“\n%4s”, “abcdef”);
}
OUTPUT
a
|
b
|
c
|
d
|
e
|
f
|
a
|
b
|
c
|
d
|
e
|
f
|
o
Precision
specifier
Precision
results on the screen can be obtained. The precision specifier always start
with a dot (.) in order to separate it from any preceding width specifier. It
is used along with floating point and character (string) data values.
void main ( )
{
float a =
123.456789;
printf
(“\n%.1f”, a);
printf
(“\n%.2f”, a);
printf
(“\n%.3f”, a);
printf
(“\n%.4f”, a);
}
OUTPUT
1
|
2
|
3
|
.
|
5
|
1
|
2
|
3
|
.
|
4
|
6
|
1
|
2
|
3
|
.
|
4
|
5
|
7
|
1
|
2
|
3
|
.
|
4
|
5
|
6
|
8
|
void main ( )
{
printf
(“\n%.2s”, “abcdef”);
printf
(“\n%.3s”, “abcdef”);
}
OUTPUT
a
|
b
|
a
|
b
|
c
|
2)
scanf(
) statement
§ The scanf ( )
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 the program.
§ The scanf ( )
statement stops functioning when some input entered does not match with format
string.
§ Syntax:
printf (“Control
String”, &v1, &v2, …., &vn);
§ The scanf ( )
statement requires ‘&’ operator called address_of operator. The address_of
operator points towards the memory location of the variable.
UNFORMATTED FUNCTIONS
1)
Character
I/O functions
a.
getchar(
)
§ This function
reads a character-type data from standard input.
§ It reads one
character at a time till the user presses the ‘enter key’.
§ Syntax:
variable_name
= getchar( );
e.g.
char
c;
c
= getchar( );
Sample
Code
void main( )
{
char
a;
clrscr(
);
printf(“\nEnter
a character:”);
a
= getchar( );
printf(“Entered
character = %c”,a);
}
OUTPUT
Enter a character:
$
Entered
character = $
b.
putchar(
)
§ This function
prints one character on the screen ata time, read by the standard input.
§ Syntax:
putchar(variable_name);
e.g.
char
a = ‘*’;
putchar(a);
Sample
Code
void main( )
{
char
a = ‘9’;
clrscr(
);
printf(“\nEntered
character:”);
putchar(a);
}
OUTPUT
Entered
character:9
c.
getch(
) and getche( )
§ These functions
read any alphanumeric character from the standard input device.
§ The getche( )
accepts and displays the charater whereas getch( ) accepts but does not display
the character.
Sample
Code
void main( )
{
clrscr(
);
printf(“\nEnter
any two characters:”);
getche(
);
getch(
);
}
OUTPUT
Enter any
two characters:$
d.
putch(
)
§ This function
prints any alphanumeric character taken by the standard input device.
Sample
Code
void main( )
{
char
ch;
clrscr(
);
printf(“\nPress
any key to continue”);
ch
= getch();
printf(“\nYou
pressed:”);
putch(ch);
}
OUTPUT
Press any
key to continue
You
pressed:@
2)
String
I/O functions
a.
gets(
)
b.
puts(
)
3)
File
I/O functions
a.
putc(
)
b.
getc(
)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.