Input/output in Programming
Input/output in Programming
1
Contents
▶ Introduction to Input & Output
▶ Input operations
▶ Output operations
2
1. Introduction
1.1. Roles of I/O operations
▶ Allowing programs to input/ouput data from/to I/O devices
such as keyboard, display
▶ Requirements of input operations:
▶ Support all data types, from simple to structured ones.
▶ Support inputing from different sources/devices such as keyboards,
mouses, input ports.
▶ Support input of correct data.
▶ Easy and convenient input of data.
3
1. Introduction
1.1. Roles of I/O operations
▶ Requirements of output operations:
▶ Support all data types, from simple to structured ones.
▶ Support outputing to different devices such as display, files, output
ports.
▶ Support formatting output data
▶ Easy and convenient output of data.
4
1. Introduction
▶ By data types, there are two groups of I/O operations:
▶ Character I/O
▶ Sequence of characters I/O
▶ By format, there are also two groups of I/O
operations:
▶ Formating I/O operations
▶ Unformating I/O operations
▶ Each operation is defined by one function, and all of
them defined in two library files <stdio.h> và
<conio.h>.
5
2. Input operations
▶ There are two groups of input operations:
▶ Character input (byte input)
▶ Input for sequence of characters(multi-byte)
▶ 2.1. Operations for character input
▶ getchar()
▶ getch()
▶ getche().
6
2. Input operations
▶ 2.1. Operations for character input
▶ Common features of the operations:
▶ No parameters,
▶ They all return ASCII code of input character (an integer).
▶ They are all in <stdio.h>
▶ There are some differences among them.
7
2. Input operations
▶ 2.2 Keyboard buffer
▶ A special memory region used to store characters input from
keyboard before taken out by programs to process.
▶ Used to synchronize in-program processing with input
operations, because there are huge speed difference between
them.
▶ Its operating mechanism follows FIFO (First In, First Out or
Queue).
8
2. Input operations
2.2. Input operation for sequence of characters
2.2.1. gets()
↖ Syntax : gets(char s[]) ;
↖ Explanation:
↖ This function inputs a sequence of characters from keyboard until the
Enter button pressed. The sequence will be assigned to variable s,
with a null character (‘\0’) also added automatically.
9
2.2.1. Example of gets()
#include <stdio.h> Result:
void main() {
Input a name : binh ↵
char name[80] ;
The input name is : binh
printf(“Input a name
:”);
gets(name) ;
printf(“The input name
is : %s”,name);
}
10
2. Input operations
2.2. Input for sequence of characters
2.2.2. Function scanf()
▶ Roles: it is multi data type input with formatting.
▶ Syntax:
scanf(formatting string, list of variables);
▶ In which:
▶ formatting string: composes of one or many formatting
codes.
▶ list of variables: list of input variables. The number of the
variables is equal to the number of formatting codes.
Every formatting code begins with ‘%’.
11
2. Input operations
2.2. Input for sequence of characters
2.2.2. Function scanf()
▶ Rules of operation:
▶ Right after Enter pressed, the function checks the
correspondance between input data and input variables.
▶ If the correspondence is valid (sufficient), input data will be
converted and assigned to input variables respectively.
▶ Otherwise, it waits for next input.
12
3. Output operations
▶ putchar() : print a character to display.
13
3. Output operations
3.1. Function putchar()
▶ Syntax: putchar(char c) ;
▶ Explanation: print a character c to display at current
position without formatting.
3.2. Function puts()
▶ Syntax: puts(const char *s) ;
▶ Explanation: print sequence s to display, then go to
the beginning of the next line.
14
3. Output operations
Exp: Using putchar() to print characters a-z
#include <stdio.h>
#include <conio.h>
void main(){
int i='a';
while (i<='z')
{ putchar(i++); putchar(' ');}
getch();
}
15
3. Output operations
3.3. Function printf()
▶ Roles:
▶ Print one or more expressions of all simple date types with
formatting.
▶ All-in-one output operation
16
3. Output operations
3.3. printf()
▶ Syntax:
printf(Formatting string[, list of expressions]);
▶ Explanation: print the formatting string which contains
two parts:
▶ Static content part
▶ Dynamic content part
17
3.3. printf()
▶ Static content part: this part is:
▶ Fixed content defined by a constant string
▶ Mandatory
18
3.3. printf()
▶ Dynamic content part: this part is:
▶ not fixed and evaluated from list of expressions
▶ defined by special strings called format code beginning with ‘%’
▶ optional
19
3.3. printf()
▶ Exp: Using printf(); ▶ Result:
char c = ‘X’ ;
int i =10 ;
Kí tự X có mã ASCII là 88
const PI=3.1415 ;
float r=11.2345678 ; 10 bình phương bằng 100
printf(“Kí tự %c có mã ASCII là %d
\n”,c,c) ; Bán kính hình tròn là
printf(“%d bình phương”, i) ; 11.234568 có diện tích là
printf(“ bằng %d ”, i*i) ; 378.647
printf(“Bán kính hình tròn là %f ”,r)
;
printf(“ có diện tích là %.3f
\n”,PI*r*r) ;
20
3.3. printf()
3.3.2. Formatting codes
a) Roles and structure: it aims to define types and formats of output data.
Each formatting code has a form below:
% [stereotype_code] conversion_code
In which:
▶ stereotype_code: defines formats of output data like size, alignment, ect.
This part is optional.
▶ conversion_code: defines type of output data, and mandatory.
21
3.3. printf()
b) Stereotype code
▶ It has a form: [-][+][0][m | *][.n | .*]
With m, n are unsigned integers. Trong đó:
▶ Sign ‘-’ : for left alignment. The default (without the sign) is
right alignment.
▶ Sign ‘+’ : applied for numeric types (int, float, double), means
that positive numbers will have the sign ‘+’, and negative
numbers with ‘-’ at the beginning.
22
3.3. printf()
b) Stereotype code (continue)
▶m : a constant integer defining the minimum size of
output data (number of ouput characters). There are
several cases:
▶ If m is bigger than real size of data, then spaces (or 0) will
be inserted on the left or right.
▶ Otherwise, real size of data will be output.
▶ m can be replaced by ‘*’. In that case, the minimum size
will be calculated by a correspondent expression in the list
of expressions.
23
3.3. printf()
b) Stereotype code (continue)
▶ 0: applied for numeric types(int, float), in case of right
alignment and minimum size is bigger than real size of data, a
sufficient number of 0 will be added in front of the data. The
default mode (without 0) is using spaces.
24
3.3. printf()
▶ n: a value right after ‘.’. This option has different meanings for
different data types.
▶ For real numbers: it defines the precision (number of digits after ‘.’).
The default is 6.
▶ For integers: it defines the minimal number of output digits. If n is
bigger than number of digits than a sufficient number of 0 will be
added.
▶ For string of characters: it defines the maximal number of characters
output. If n is smaller than length of the string than n first characters
of the string will be output. Value n can also be replaced by ‘*’.
25
Example of printf()
main(){ Result:
char ch='A';
int i=1234, j=-1234;
A^^^^,^^^^A
float x=11.23456789;
printf("%-5c,%5c\n",ch,ch); 1234,1234^^^^^^,^^^^^+1234,
printf("%d,%-10d,%+10d, ^^^^001234,-0000001234
%10.6d, %010d\n",i,i,i,i,j); 11.234568,
^^^11.234568,11.2346^^^^^,+0
printf("%f,%12f,%-12.4f,% 00011.2346
+012.4f\n",x,x,x,x);
printf("%*f\n",12,x); ^^^11.234568
printf("%*.*f",12,3,x); ^^^^^^11.235
getch(); -----------------------------
return; Notes: ‘^’ represents space.
}
26
Table of formatting codes
Conversion Conversion
Explanation Explanation
Code Code
%c print a character %ld print a long integer in
decimal base
%s print a string of %lu print a long unsigned
characters integer in decimal
base
%d,%i,%hd print an integer in %f,%lf print a real number in
decimal base floating point
%o print an integer in octal %e,%E print a real number in
base scientific format
%x,%X print an integer in %g,%G print a real number in
hexadecimal base floating point or
scientific format
%u print an unsigned
integer in decimal base
27
Summary
▶ Input operations
▶ Output operations
Thank you!
28