Sei sulla pagina 1di 11

Input/Output Statements

3.1. INTRODUCTION
The important aspect of C programming language is its ability to handle input
and output. Even though C does not have the language construct for I/O
operations, a set of functions designed to provide a standard I/O system for C
programs has been defined such as printf(), scanf(), puthchar() and getchar()
functions. program using these functions must include the standard header file
stdio.h in it, using the directive statement at the beginning as !
#include <stdio.h>
The filename stdio.h is an abbreviation for "standard input#output header
file$. The instruction %include &stdio.h' tells the compiler to search for a file
named stdio.h and place its contents at this point in the program. The contents of
the header file become pat of the source code (hen it is compiled. The t(o types
of input/output functions are !
). *nformatted I/O functions +. ,ormatted I/O functions
3.. UN!OR"#TT$D IN%UT !UNCTIONS
3..1. &etcha'() !unction
-eading a single character can be done by using the function getchar(). The
function does not re.uire any arguments, through a pair of empty parenthesis
must follo( the (ord getchar. The synta/ is !
variable_name = getchar() ;
0here vaiable1name is a valid C name that has been declared as char type.
0hen this statement is encountered, the computer (aits until a 2ey is pressed
and then assigns this character as a value to the getchar() function. The getchar()
function needs an E3TE- 2ey to be pressed to assign the character to the
variable. ,or e/ample !
cha' se* +
se* , &etcha'() +
3.2 Input / Output Statements
0ill assign the character 456 to the variable name se/ (hen (e press the 2ey
5 on the 2eyboard.
3... &etch() !unction
The getch() function reads a single character directly from the 2eyboard the
instant it is typed, (ithout (aiting for the E3TE- 2ey to be pressed. It 7ust
returns character that is typed (ithout echoing to the screen. The synta/ is !
variable_name = getch() ;
3..3. &etche() !unction
The getche() function reads a single character from the 2eyboard the instant
it is typed, (ithout (aiting for the E3TE- 2ey to be pressed. It 7ust returns the
character that is typed and echoes it to the screen. The synta/ is !
variable_name = getche() ;
3..-. &ets() !unction
The gets() collects a string of characters terminated by a ne( line from the
standard input device and puts it into the variable. This function accepts a single
argument. The synta/ is !
gets(variable_name) ;
0here the variable1name (ithin the function must be a data item (hich
represent a string or a character array. The gets() function replaces the ne( line
by a null character (89) in the variable. It also allo(s input strings to contain
certain (hite space characters (spaces, tabs). The gets() function returns (hen it
encounters a ne( line, everything up to the ne( line is copied into the variable.
3.3. UN!OR"#TT$D OUT%UT !UNCTIONS
3.3.1. putcha'() !unction
0riting a single character can be done by using the function putchar(). The
character being transmitted (ill normally be e/pressed as an argument to the
function enclosed in parenthesis follo(ing the (ord putchar. The synta/ is !
putchar(variable_name)
0here the variable1name is a type char variable containing a character. This
statement displayed the character contained in the variable1name at the terminal.
,or e/ample !
Input / Output Statements 3.3
se* , .!/ +
putcha'(se*) +
0ill displays the character , on the screen. The statement !
putcha'(.0n/) +
0ould cause the cursor on the screen to move to the beginning of the ne/t
line.
3.3.. putch() !unction
putch() function outputs the character to the current te/t (indo(. It is a
te/t#mode function that performs direct video output to the console. putch()
function does not translate linefeed characters (8n) into carriage#return/linefeed
pairs. The synta/ is !
putch(variable_name) ;
3.3.3. puts() !unction
The puts() function is used to (rite only one string at a time to the standard
output device (usually the screen) and appends a ne( line character. The puts()
function accepts only a single argument. The synta/ is !
puts(variable_name) ; or
puts(string) ;
0here the variable1name (ithin the function must be a data item (hich
represents a string or a character array. The string displayed by the puts()
function may contain certain (hite space characters (spaces, tabs).
3.-. !OR"#TT$D IN%UT !UNCTION 1 scan2()
The scanf() function is used to input a set of values. This function inputs any
combination of digits, characters and strings. The synta/ is !
scanf(control strings, arg1, arg2, . . . argn) ;
0here the control strings specifies the field format in (hich the data is to be
entered and the arguments arg),arg+, . . . argn specify the address of locations
(here the data is stored. scanf reads one data item from the input corresponding
to each argument, s2ipping (hite spaces including ne( lines to find the ne/t data
item.
-ules for using scanf() functions are !
3.4 Input / Output Statements
). ll the format specifers are (ritten (ithin double .uotes.
+. Each format specifier must be preceded by the : sign.
;. Each numeric variable must be preceded by < symbol e/cept for array of
characters.
=. There must be a format specifier corresponding to every variable. The
order of the format specifier is same as the order of the corresponding
variables.
3.-.1. Input o2 Inte&e' Num3e's
The field specification for reading an integer number is !
% or %!
0here percent sign(:) indicates that conversion specification follo(s, ( is an
integer number that specifies the field (idth of the number to be read and d
2no(n as data type character, indicates that the number to be read is in integer
mode. Consider the follo(ing statement !
scan2(45d 5d 53d67 8a7 837 8c) +
0hen this statement is e/ecuted, the user has to enter the values through
the standard input device (2eyboard). >uppose the input data items entered be !
) + ;
The assignment (ill be !
a ? ) b ? + c ? ;
If the input data items entered be !
)+ ;= @A
The assignment (ill be !
a ? )+ b ? ;= c ? @A
If the input data items entered be !
)+;=@ABC
The assignment (ill be !
a ? )+ b ? ;= c ? @AB
Input / Output Statements 3.5
nd the remaining digits (ill be ignored. If the input data items entered be !
)+; =@ABC
The assignment (ill be !
a ? )+ b ? ; c ? =@A
nd the remaining digits (ill be ignored.
3.-.. Input o2 Real Num3e's
The field specification for reading a real number is !
%f
The above notation can be used for both decimal point notation and
e/ponential notation. Consider the follo(ing statement !
scan2(452 52 5267 8a7 837 8c) +
0hen this statement is e/ecuted, the user has to enter the values through
the standard input device (2eyboard). >uppose the input data items entered be !
)+;.=@ )+.;=E#) )+;
The assignment (ill be !
a ? )+;.=@ b ? ).+;= c ? )+;.9
3.-.3. Input o2 Sin&le Cha'acte's
The field specification for reading single characters is !
%c or %!c
Consider the follo(ing statement !
scan2(45c 5c 5c67 8a7 837 8c) +
0hen this statement is e/ecuted, the user has to enter the values through
the standard input device (2eyboard). >uppose the input data items entered be !
r e c
3.6 Input / Output Statements
The assignment (ill be !
a ? 4r6 b ? 4e6 c ? 4c6
3.-.-. Input o2 St'in&s
The field specification for reading a strings is !
%s or %!s
Consider the follo(ing statement !
scan2(45s 5s 5s67 a7 37 c) +
0hen this statement is e/ecuted, the user has to enter the values through
the standard input device (2eyboard). >uppose the input data items entered be !
Dod is Eove
The assignment (ill be !
a ? "Dod$ b ? "is$ c ? "Eove$
3.-.9. Input o2 "i*ed "ode Data T:pes
It is possible to use one scanf statement to input a data line containing mi/ed
mode data such as integer, float and char. In such cases, care should be
e/ercised to ensure that the input data items match the control specifications in
order and type. Consider the follo(ing statement !
scan2(45d 5s 5c 5267 8id7 name7 8se*7 8sala':) +
0hen this statement is e/ecuted, the user has to enter the values through
the standard input device (2eyboard). >uppose the input data items entered be !
)9) Fhuvan 5 A999
The assignment (ill be !
id ? )9) name ? "Fhuvan$ se/ ? 456 salary ? A999.9
The table belo( sho(s the different scanf() format codes for getting input
from the user !
Input / Output Statements 3.7
"ormat
#p
ec
ifi
er
$%pe of
&rg
um
ent
'nput
:c Character -eads a single character
:d or :i Integer -eads a decimal integer
:e or :E or
:f or :g or
:D
,loating point -eads a floating point value
:hd or :hi >hort integer -eads decimal short integer
:hu >hort integer -eads decimal unsigned short
integer
:ld or :li Eong integer -eads decimal long integer
:le or :lf or
:lg
Gouble -eads signed double
:Ee or :Ef or
:Eg
Eong double -eads signed long double
:lo Eong integer -eads an octal long integer
:lu Eong integer -eads decimal unsigned long integer
:l/ Eong integer -eads he/adecimal long integer
:o Octal integer -eads an unsigned octal integer
:s >e.uence of
characters
-eads a string
:u Integer -eads an unsigned decimal integer
:/ or :H Ie/adecimal
integer
-eads a unsigned he/adecimal
integer
3.9. !OR"#TT$D OUT%UT !UNCTION ; p'int2()
The calculated results are output (ith the help of output function called
printf(). This function is used to display any combination of digits, characters and
strings.
It is similar to the scanf() function, but the purpose is to display the data. The
printf() function moves data from the computer6s memory to the standard output
device (hereas the scanf() function enters data from the standard input device
and stores it in the computer6s main memory. The synta/ is !
printf(control strings, arg1, arg2, . . . argn) ;
0here the control strings consists of three types of items !
). Characters that (ill be printed on the screen as they appear.
3.8 Input / Output Statements
+. ,ormat specifications that define the output format for display of each
item.
;. Escape se.uence characters such as 8n and 8t.
0here the arguments arg), arg+, . . . argn are the variables (hose values
are formatted and printed according to the specifications of the control string. The
arguments should match in number, order and type (ith the format specification.
-ules for using printf() functions are !
). ll the format specifers are (ritten (ithin double .uotes.
+. Each format specifier must be preceded by the : sign.
;. There must be a format specifier corresponding to every variable. The
order of the format specifier is same as the order of the corresponding
variables.
3.9.1. Output o2 Inte&e' Num3e's
The format specification for printing an integer number is !
% or %!
0here ( specifies the minimum field (idth for the output. Io(ever, if a
number is greater than the specified field (idth, it (ill be printed in full,
overriding the minimum specification. d specifies that the value to be printed is an
integer. Consider the follo(ing statement !
p'int2(45d 5d 5d67 a7 37 c) +
>uppose the input data items are ! a ? )9, b ? +9 and c ? ;9, the system
displays the follo(ing output !
)9 +9 ;9
3.9.. Output o2 Real Num3e's
The format specification for printing a real number in decimal notation is !
%f or %!.pf
The format specification for printing a real number in e/ponential notation is !
%e or %!.pe
0here the integer ( indicates the minimum number of positions that are to
be used for the display of the value and the integer p indicates the number of
Input / Output Statements 3.9
digits to be displayed after the decimal point (precision), the default precision is A
decimal places. Consider the follo(ing statement !
p'int2(452 5.2 51<.267 a7 37 c) +
>uppose the input data items are ! a ? )+.;=@A, b ? JC.BA@= and
c ? );.@BJ), the system displays the follo(ing output !
)+.;=@A JC.BA ).;AeK9)
3.9.3. Output o2 Sin&le Cha'acte's
single character can be displayed in a desired position using the format !
%c or %!c
The character (ill be displayed right 7ustified in the field of ( columns. 0e
can ma2e the display left 7ustified by placing a minus sign before the integer (,
the default value for ( is ). Consider the follo(ing statement !
p'int2(45c67 se*) +
>uppose the input data item is ! se/ ? 456, the system displays the follo(ing
output !
5
3.9.-. Output o2 St'in&s
The format specification for outputting strings is similar to that of real
numbers. It is of the form !
%s or %!.ps
0here ( specifies the field (idth for display and p instructs that only the first
p characters of the string are to be displayed. Consider the follo(ing statement !
p'int2(45s67 name) +
>uppose the input data item is ! name ? "Fhuvan$, the system displays the
follo(ing output !
Fhuvan
3.9.9. Output o2 "i*ed "ode Data T:pes
3.10 Input / Output Statements
It is possible to use one printf() statement to output the values containing
mi/ed mode data such as integer, float and char. In such cases, care should be
e/ercised to ensure that the format specifications should match the variables in
number, order and type.
Consider the follo(ing statement !
p'int2(45d 5s 5c 5.267 id7 name7 se*7 sala':) +
>uppose the input data items are ! id ? )9), name ? "Fhuvan$, se/ ? 456
and salary ? A999, the system displays the follo(ing output !
)9) Fhuvan 5 A999.99
The table belo( sho(s the different printf() format codes for printing the
output in the screen !
"ormat
#p
ec
ifi
er
$%pe of
&rg
um
ent
(utput
:c Character Lrints a single character
:d or :i Integer Lrints a signed decimal integer
:e ,loating point Lrints a floating point value in
e/ponent form
:E ,loating point >ame as e, (ith E for e/ponent
:f ,loating point Lrints a floating point value (ithout
e/ponent
:g ,loating point Lrints a floating value either e or f
form depending on the value or
precision
:D ,loating point >ame as D, (ith E for e/ponent if e
format is used
:hd or :hi >hort integer Lrints decimal short integer
:hu >hort integer Lrints decimal unsigned short integer
:ld or :li Eong integer Lrints decimal long integer
:le or :lf or
:lg
Gouble Lrints signed double
:Ee or :Ef or
:Eg
Eong double Lrints signed long double
:lo Eong integer Lrints an octal long integer
:lu Eong integer Lrints decimal unsigned long integer
:l/ Eong integer Lrints he/adecimal long integer
:o Octal integer Lrints an unsigned octal integer
Input / Output Statements 3.11
:s >e.uence of
characters
Lrints a string until a null (89)
character is reached
:u Integer Lrints an unsigned decimal integer
:/ Ie/adecimal
integer
Lrints a unsigned he/adecimal
integer ((ith a,b,c,d,e,f)
:H Ie/adecimal
integer
Lrints a unsigned he/adecimal
integer ((ith ,F,C,G,E,,)
3.=. $SC#%$ S$>U$NC$S (?#C@SA#SB CB#R#CT$R CONST#NTS)
Certain constants are non#printable, (hich means that they are not displayed
on the screen or in the printer, called as escape se.uences (bac2slash character
constants). C supports some special bac2slash character constants that are used
in output functions. Table belo( sho(s the commonly used escape se.uences.
&#)''
*alue
+scape
#e,uences
-eaning
999 89 3ull
99B 8a udible alter (bell)
99C 8b Fac2space
99J 8t IoriMontal tab
9)9 8n 3e( line
9)) 8v Nertical tab
9)+ 8f ,orm feed
9); 8r Carriage return
9;= 8$ Gouble .uote
9;J 86 >ingle .uote
9A; 8O Puestion mar2
9J+ 88 Fac2slash

Potrebbero piacerti anche