Tải bản đầy đủ (.docx) (17 trang)

Nhập và Xuất trong C

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (200.46 KB, 17 trang )

Bài 4 Nhập và Xuất trong C
Mục tiêu:
Kết thúc bài học này, bạn có thể:
 
  !"#$%
Giới thiệu
&'(!)*+,-'./'(01!,"
23,$'4
5% &*627/89:8;%
<% &*6+%
&'=>?@AB7(C%D:8;,*,=
6EF(!)27-'%G'H+H.27-F(=!CB
+,/',+!06CF%
&'I.2/9+F '/%&2/9+ 
6C,"'8J2'K!"#L%&'(E>.C+
ME+,/'N0(9C+M0!06C 
0(9%&0(9*2O,(H%&0(9*2O,
-'',$%D232P0>N>-0(
9%D+23,2KQ>K(A0(,2+'!%R+,/=J
230>%

4.1 Tập tin tiêu đề <stdio.h>
&'H 2P.SN0TS4
#include <stdio.h>
GU>,,/@,"preprocessor command)%&'I9.!"/#KVW=K%
stdio.h ,W23E,K@header). DA''@
23M'I%printf(), scanf(), putchar()getchar()230!0$'E
''stdio.h #*/27A%
4.2 Nhập và xuất trong C (Input and Output)
&2/9'I#/>K=%I1,4
• printf()X%


• scanf()X%
D+>E,+23-1E+,/$'
!23@!(Y2OM%ZW6A$'F
(0?23'%
4.2.1 printf()
I1S6$WP>6=2P%[U>.1?$107%
printf()23M+,/K0(9X'',$-%R\F
>24
printf(“control string”, argument list);
R]$,('^_.(0.(A>23U(Y
9>%I=CW,/_'L@!control string) 'L]
'%D+,/C27AP]@],23.!+ 
,/A#%IL@!C,*23V(K'V>!`“”.U>,U
delimiters)%IL@!AW>@7(=2PU>4
 Ký tự văn bản (Text characters)XZ'^!"#23?23]2
->%I!'Ca2O23M'/U2O$,23%
 Lệnh định dạng - bQA +,/']?23%
cW,/(a=PW!"/%$',WS27A' 
+,/%R%23M'printf()dVC>e%I,/
 +,/27H$'A#!NC%cWS-=0
'E +,/=%
 Các ký tự không in đượcXZ'^H(.!'Ca]T%
cL,/^W>@S%cWS('^!"/ %W(W
!%ZCf%5,/!KS!23L3(YU,/printf()4
Ðịnh dạng printf() scanf()
g"#7,$I$ h h
IL h h
]>K$$,$$ h h
]UWi,'' h h'Vh$
]UWjZk=U h, h,

]UWjZk]J h$ h'Vh$
]UWh'Vh$.']'H7 h
]>K!*l$$,$$ h h
], U!*RMmnZIRoip
l$$$,$$
h h
](U!*l$',$$ h' h'
Bảng 4.1: Mã định dạng trong printf ()
&'(CK.c, d, f, lf, e, g, u, s, ox,(W!%
I6>2P'S!23e!0'ZCf%<4
Mã định dạng Quy ước in ấn
h I']']>K%
h q=]>KF]?23>K%q=U?Af
']%D0=UF']H7f].?23K]
!*r(KC>E,,TH(KC%
h$ cW'](KUf'](KC]2h%
Bảng 4.2: Quy ước in
ZY-!"/%,s“23MV(/'L@!.01=!"/
>,K-.1C23M2'ZCf%t4
ss :!"#\
sm :!"#“
hh :!"#%
Bảng 4.3: Các ký tự đặc biệt trong chuỗi điều khiển
ZC2PU>2H  L@!S!%
Số Câu lệnh
Chuỗi
điều
khiển
Nội dung mà
chuỗi điều khiển

chứa đựng
Danh
sách
tham số
Giải thích
danh sách
tham số
Hiển thị trên
màn hình
5% mhp.trru h Id  A  ,/

trr _] trr
<% mhp.5rvwu h Id  A  ,/

5rvw ZA 5w
t% mx''
c'c%y$$%pu
x''
c'
c%y$$%
Id ,  !" #
B(C
g*  
D,
g* x''  c'
c%y$$%
z% '{5rru
mhp.'u
h Id  A  ,/


I' Z0 5rr
w% ms$,,'pu s$,,' Id ,  !" #
B(C!"#
!*23%
g* g* $,,'
&KTP
f% |$$    mx''
n,$p
}}%%
mhp.u
h Id  A  ,/

 _L x''n,$
~% }}%%
'.•u
'{ru
•{5rru
mh  hsp.
'.•u
hh Id  A  ,/
-
#'
'.
•
(0 r.5rr
Bảng 4.4 : Chuỗi điều khiển và mã định dạng
Ví dụ 4.1 :
GU>,W27-7CME'WL23$',/%
I27->JW!"#7.]>K]#,$$.$$.,'%
#include <stdio.h>

void main()
{
int a = 10;
float b = 24.67892345;
char ch = ‘A’;
printf(“\nInteger data = %d”, a);
printf(“\nFloat Data = %f”, b);
printf(“\nCharacter = %c”, ch);
printf(“\nThis prints the string”);
printf(“%s”, ”\nThis also prints a string”);
}
g06C27-24
Integer data = 10
Float Data = 24.678923
Character = A
This prints the string
This also prints a string
 Bổ từ (Modifier) cho các lệnh định dạng trong printf()
I,/(eNmodifier.>eVC>e]%U>,(eN
23'U,/printf(). D0@(eN23M-1U$'-#4
Bổ từ ‘-‘
R+,/?23(K'!*'.1?23(a=NH'M
(K%
Bổ từ xác định độ rộng
I123MP!4float, double hay char array Lj%ZeNWW,
W]>KWW€F2O+,/%I+,/WW€7?'!0 
6CC'2O+,/%I+,/!H2P,P7?23(_MK
+H'F>K=%•H .%10f ,,/' +,/!]#PWW
2O+,/,5r%
Bổ từ xác định độ chính xác

I123MP!float, double > mảng ký tự (char array, string)%ZeNWW
H2302P.mPm,W]>K%D0 P!floatdouble.L]
d]']tối đa23H(KCU%
D0=U]F +,/!float haydouble236WW']d'(eN.-
]?23làm tròn%D0@L236@d-L?23cắt bỏ phần
dư ra ở phía cuối%cW]!*r?23K'0]']##'W +,/H
723d'(eN%&27#.!'Ca?23K''L!"#%•H . 
h5r%t,,/' +,/!float.PWW]'2O+,/,5rt
H=U%
Bổ từ ‘0’
&$'V./K'W2O23#/P!'Ca%D02OM]
K'2OP]!*r.(eN>C23M%
Bổ từ ‘l’
ZeN>23M]>K24long int>W]!double%cS
27A',%ld.
Bổ từ ‘h’
ZeN>23M/!short$$%cS27A',%hd%
Bổ từ ‘*’
ZeN>23M!2OM!*]d2PWWF2O]27-
%D2!P(eN>.W]23>K=CdWW2O %
I1S>$+(eN>'W0'%G=K.1$`WF]P
++,/!]>K%
Ví dụ 4.2:
/* Chương trình này trình bày cách dùng bổ từ trong printf() */
#include <stdio.h>
void main()
{
printf(“The number 555 in various forms:\n”);
printf(“Without any modifier: \n”);
printf(“[%d]\n”, 555);

printf(“With - modifier:\n”);
printf(“[%-d]\n”, 555);
printf(“With digit string 10 as modifier:\n”);
printf(“[%10d]\n”, 555);
printf(“With 0 as modifier: \n”);
printf(“[%0d]\n”, 555);
printf(“With 0 and digit string 10 as modifiers:\n”);
printf(“[%010d]\n”, 555);
printf(“With -, 0 and digit string 10 as modifiers:\n”);
printf(“[%-010d]\n”, 555);
}
g06C22PU>4
The number 555 in various forms:
Without any modifier:
[555]
With - modifier:
[555]
With digit string 10 as modifier:
[ 555]
With 0 as modifier:
[555]
With 0 and digit string 10 as modifiers:
[0000000555]
With -, 0 and digit string 10 as modifiers:
[555 ]
I1SM!"/‚[‘‚]’ d72O(a=7!01%g1M%d
!*(eN.1>_M'W2OMWWP]>K%gM
%10d 1>_M5r!'Ca'2O]23,@C$'V%D0
M(eNX.]?23'2O%D0M(eNr.1>_]?K
'r>-,!'Ca%

ZU>O1S>$(eNMP]#%
Ví dụ 4.3:
/* Chương trình này trình bày cách dùng bổ từ trong printf() */
#include <stdio.h>
void main()
{
printf(“The number 555.55 in various forms:\n”);
printf(“In float form without modifiers:\n”);
printf(“[%f]\n”, 555.55);
printf(“In exponential form without any modifier:\n”);
printf(“[%e]\n”, 555.55);
printf(“In float form with - modifier:\n”);
printf(“[%-f]\n”, 555.55);
printf(“In float form with digit string 10.3 as modifier\n”);
printf(“[%10.3f]\n”, 555.55);
printf(“In float form with 0 as modifier:\n”);
printf(“[%0f]\n”, 555.55);
printf(“In float form with 0 and digit string 10.3”);
printf(“as modifiers:\n”);
printf(“[%010.3f]\n”, 555.55);
printf(“In float form with -, 0 ”);
printf(“and digit string 10.3 as modifiers:\n”);
printf(“[%-010.3f]\n”, 555.55);
printf(“In exponential form with 0”);
printf(“ and digit string 10.3 as modifiers:\n”);
printf(“[%010.3e]\n”, 555.55);
printf(“In exponential form with -, 0”);
printf(“ and digit string 10.3 as modifiers:\n”);
printf(“[%-010.3e]\n\n”, 555.55);
}

g06C24
The number 555.55 in various forms:
In float form without modifiers:
[555.550000]
In exponential form without any modifier:
[5.555500e+02]
In float form with - modifier:
[555.550000]
In float form with digit string 10.3 as modifier
[ 555.550]
In float form with 0 as modifier:
[555.550000]
In float form with 0 and digit string 10.3 as modifiers:
[000555.550]
In float form with -, 0 and digit string 10.3 as modifiers:
[555.550 ]
In exponential form with 0 and digit string 10.3 as modifiers:
[05.555e+02]
In exponential form with -,0 and digit string 10.3 as modifiers:
[5.555e+02]
&$'V'%f.1>_f']'=UV'%e,
W']=>Kf']=(KCU%I1"/<]] 
M'H K.]'](KCU,t.\0!06C!*23, 
T%
ZU>O.1S>$(eNMPL]%I1"YW2OA'(WL%
7+.1"VCWH.4'/P]!"#23%
Ví dụ 4.4:
/* Chương trình trình bày cách dùng bổ từ với chuỗi*/

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×