Chương sau đây trình bày các kiến thức về xuất nhập trong C (Input and Output). Đây là tài liệu hữu ích cho những ai đang học Tin học đại cương khối tự nhiên. Tài liệu được viết bằng ngôn ngữ tiếng Anh. Mời các bạn cùng theo dõi.
Trang 1Input & Output
Nguyen Dung Faculty of IT - College of Science
Trang 2Functions I/O
Input:
scanf
gets
getch
getchar
Output
printf
puts
putch
getch getchar putch
…
conio.h
scanf printf gets puts
…
stdio.h
Trang 3Output - printf
int printf ( const char *format [,argument,…])
List arguments
Format string
string (plain text)
format specifier
% [ flags ][ width ][ prec ] type_char
Trang 4%[flags][width][.prec] type_char
Code Out put
d Integer number
u Unsigned integer
ld Long integer
lu Unsigned long integer
o Octal
x, X Hexa
f Real number: float, double
E, e Real number: float, double
G, g Real number: floar, double
c Character: char
s String: array char
p Address
Different???
Trang 5%[flags][width][.prec] type_char
int x = 5;
float y = 10;
printf(“x = %d, y = %f, z = %d”, x , y , x + y );
x = 5, y = 10, z = 15
Trang 6%[flags][width][.prec]type_char
Flag Description
- Align left
+ Display sign of number
white space Display sign of negative number
None Align right
Width Description
n Minimum number of characters
to be printed
* Get from parameter
None Depend on number input
Precision Description
.number The minimum number of
digits to be written
.* Get from parameter
None Depend on number input
Trang 7%[flags][width][.prec]type_char
x = -45
y = 15
y = +15
x = -45
x = -45
y = 15
int x = -45;
int y = 15;
printf("x = %d\n", x );
printf("y = %d\n", y );
printf("y = %+d\n", y );
printf("x = %5d\n", x );
printf("x = %-5d\n", x );
printf("y = %5d\n", y );
Trang 8%[flags][width][.prec]type_char
x = -000000145
y = -10.14
y = -10.13596 -y= 000010.13596
y = -10.136
y = -10.136
int x = -145;
float y = -10.13596;
printf("x = %010d\n", x );
printf("y = %7.2f\n", y );
printf("y = %12.5f\n", y );
printf("-y= %012.5f\n",- y );
printf("y = %-3.3f\n", y );
printf("y = %*.*f\n",8,3, y );
Trang 9Output – putch, puts
int putch (int ch )
int puts (const char * s )
char ch =„ a ‟;
putch( ch );
putch( 68 );
puts(“ hello world! ”);
a
D helloworld! _
Trang 10In - scanf
int scanf (const char * format [, address ,…]);
int x , y ;
printf(“Enter x,y: ");
scanf ("%d %d",& x ,& y );
printf ("x = %d\ny = &d", x , y );
Enter x, y: 10_4
x = 10
y = 4_
Trang 11In - gets
gets (char * s )
char name [30];
gets( name );
printf(“Name is: %s”, name );
But
char name [30];
scanf(“%s”, name );
printf(“Name: %s”, name );
Micheal Balack Name is: Micheal Balack
Micheal Balack Name is: Micheal
Trang 12In – getch, getchar
int getchar(void);
int getch(void);
int ch;
printf(“Input a character: ”);
ch = getchar();
printf(“Character is entered: %c”,ch);
Different : getchar – display the character entered
getch – don‟t display
Input a character: c Character is entered: c
Trang 13Exercises
Available:
nguyendung622.wordpress.com
learning day of next week
Trang 14The end