Mot so kien thc c s ve lap trnh bang ngon ng C

Phan trc | Muc luc | Phan sau

V. oc va viet qua cac giao tiep nhap xuat (I/O) chuan

Noi dung:

Vi phan nay ta tm hieu ky hn vi lenh printf() cung nh cac lenh nhap xuat sau:

 

Cac giao tiep nhap xuat chuan (I/O)

Ngon ng C x ly mot tap tin nh mot chuoi byte (8 bits), goi la stream va co the thu nhan hoac ghi lai no qua cac giao tiep xuat nhap (I/O):

 

Noi dung

Thiet b giao tiep

Stdin

Giao tiep nhap chuan

Ban phm

Stdout

Giao tiep xuat chuan

Man anh

Stderr

Giao tiep loi e thong bao loi

Man anh

Ghi chu

Ngon ng C thao tac vi nhieu lenh nhap xuat (I/O) cha trong header stdio.h. Cho nen, luon nh khai bao #include stdio.h trc khi s dung cac lenh I/O. oi vi C++, cac lenh nhap xuat cha trong iostream.h.

Giao tiep nhap t ngi s dung (thng la thong qua ban phm)

Ham getc()

Ham getc() oc 1 ky t t mot file stream va tra lai gia tr so nguyen ng vi ky t o.

#include <stdio.h>
int getc(FILE *stream);

Neu ket thuc tep (end-of-file) hoac loi xuat hien, ham tra lai gia tr EOF.

V du: oc ky t do ngi s dung cho vao

1: /*Reading input by calling getc() */
2: #include <stdio.h>
3:
4: main()
5: {
6: int ch;
7:
8: printf("Please type in one character:\n");
9: ch = getc( stdin );
10: printf("The character you just entered is: %c\n", ch);
11: return 0;
12: }
Ket qua:
Please type in one character:
H
The character you just entered is: H

Ham getchar()

Ham getchar() tng ng vi getc(stdin).

#include <stdio.h>
int getchar(void);

Neu ket thuc tep (end-of-file) hoac loi xuat hien, ham tra lai gia tr EOF.

V du: oc ky t do ngi s dung cho vao

1: /*Reading input by calling getchar() */
2: #include <stdio.h>
3:
4: main()
5: {
6: int ch1, ch2;
7:
8: printf("Please type in two characters together:\n");
9: ch1 = getc( stdin );
10: ch2 = getchar( );
11: printf("The first character you just entered is: %c\n", ch1);
12: printf("The second character you just entered is: %c\n", ch2);
13: return 0;
14: }
Ket qua:
Please type in two characters together:
Hi
The first character you just entered is: H
The second character you just entered is: i

Giao tiep xuat ra man anh

Ham putc()

Ham putc() ghi 1 ky t vao mot file stream.

#include <stdio.h>
int putc(int c, FILE *stream);

Neu ghi thanh cong, ham tra lai gia tr ky t,; neu khong tra lai EOF.

V du: Xuat mot ky t len man anh

1: /*Outputting a character with putc() */
2: #include <stdio.h>
3:
4: main()
5: {
6: int ch;
7:
8: ch = 65; /* the numeric value of A */
9: printf("The character that has numeric value of 65 is:\n");
10: putc(ch, stdout);
11: return 0;
12: }
Ket qua:
The character that has numeric value of 65 is:
A
C:\app>

Ham putchar()

Ham putchar() tng ng vi putc(stdout).

#include <stdio.h>
int putchar(int c);

Neu ghi thanh cong, ham tra lai gia tr ky t,; neu khong tra lai EOF.

V du: Xuat ky t len man anh

1: /* 05L04.c: Outputting characters with putchar() */
2: #include <stdio.h>
3:
4: main()
5: {
6: putchar(65);
7: putchar(10);
8: putchar(66);
9: putchar(10);
10: putchar(67);
11: putchar(10);
12: return 0;
13: }
Ket qua:
A
B
C

Ham printf()

#include <stdio.h>
int printf(const char *format-string, ...);

VD: printf("The sum of two integers %d + %d is: %d.\n",a,b,c);

Cac ac ta format dung vi printf():

%c

Ky t

%d

So nguyen

%i

So nguyen (nh %d).

%f

So thc dau phay ong

%e

So thc dang a.aaae+bbb

%E

So thc dang a.aaaE+bbb

%g

Dung %f hoac %e, neu cai nao ngan hn.

%G

Dung %f hoac %E, neu cai nao ngan hn.

%o

So octa khong dau

%s

Chuoi ky t

%u

So nguyen khong dau

%x

So hexa khong dau (vi x thng)

%X

So hexa khong dau (vi X hoa)

%p

Hien th oi so la pointer

%%

Cho ra ky t (%).

oi thanh so Hexa

V du:

1: /*Converting to hex numbers */
2: #include <stdio.h>
3:
4: main()
5: {
6: printf("Hex(uppercase) Hex(lowercase) Decimal\n");
7: printf("%X %x %d\n", 0, 0, 0);
8: printf("%X %x %d\n", 1, 1, 1);
9: printf("%X %x %d\n", 2, 2, 2);
10: printf("%X %x %d\n", 3, 3, 3);
11: printf("%X %x %d\n", 4, 4, 4);
12: printf("%X %x %d\n", 5, 5, 5);
13: printf("%X %x %d\n", 6, 6, 6);
14: printf("%X %x %d\n", 7, 7, 7);
15: printf("%X %x %d\n", 8, 8, 8);
16: printf("%X %x %d\n", 9, 9, 9);
17: printf("%X %x %d\n", 10, 10, 10);
18: printf("%X %x %d\n", 11, 11, 11);
19: printf("%X %x %d\n", 12, 12, 12);
20: printf("%X %x %d\n", 13, 13, 13);
21: printf("%X %x %d\n", 14, 14, 14);
22: printf("%X %x %d\n", 15, 15, 15);
23: return 0;
24: }
Ket qua:
Hex(uppercase) Hex(lowercase) Decimal
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
A a 10
B b 11
C c 12
D d 13
E e 14
F f 15

Format so nguyen xac nh so ch so: % (-0n) d

1: /*Specifying minimum field width */
2: #include <stdio.h>
3:
4: main()
5: {
6: int num1, num2;
7:
8: num1 = 12;
9: num2 = 12345;
10: printf("%d\n", num1);
11: printf("%d\n", num2);
12: printf("%5d\n", num1);
13: printf("%05d\n", num1);
14: printf("%2d\n", num2);
15: return 0;
16: }
Ket qua:
12
12345
 12
00012
12345

Sap xep so nguyen thang hang ben phai hoac trai

1: /*Aligning output */
2: #include <stdio.h>
3:
4: main()
5: {
6: int num1, num2, num3, num4, num5;
7:
8: num1 = 1;
9: num2 = 12;
10: num3 = 123;
11: num4 = 1234;
12: num5 = 12345;
13: printf("%8d %-8d\n", num1, num1);
14: printf("%8d %-8d\n", num2, num2);
15: printf("%8d %-8d\n", num3, num3);
16: printf("%8d %-8d\n", num4, num4);
17: printf("%8d %-8d\n", num5, num5);
18: return 0;
19: }
Ket qua:
 1 1
 12 12
 123 123
 1234 1234
 12345 12345

Format bieu diem o chnh xac: % (a.b) f , % (a.b) d

1: /* Using precision specifiers */
2: #include <stdio.h>
3:
4: main()
5: {
6: int int_num;
7: double flt_num;
8:
9: int_num = 123;
10: flt_num = 123.456789;
11: printf("Default integer format: %d\n", int_num);
12: printf("With precision specifier: %2.8d\n", int_num);
13: printf("Default float format: %f\n", flt_num);
14: printf("With precision specifier: %-10.2f\n", flt_num);
15: return 0;
16: }
Ket qua:
Default integer format: 123
With precision specifier: 00000123
Default float format: 123.456789
With precision specifier: 123.46

Bai tap

  1. Xuat len man anh ky t B, y va e.
  2. Hien th len man anh 2 so 123 va 123.456 va xep thang hang ve pha trai.
  3. Bieu dien 3 so 15, 150, and 1500 di dang hexa.
  4. Chng trnh sau co loi g?
main()
{
 int ch;
 ch = getchar();
 putchar(ch);
 return 0;
}

Phan trc | Muc luc | Phan sau