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

Phan trc | Muc luc | Phan sau

IV. Kieu va ten d lieu trong C

Noi dung:

Vi phan nay ta lam quen vi cac bon kieu d lieu c s cua ngon ng C:

 

T khoa

T khoa la t danh rieng e xac nh mot ac trng cua ngon ng, khong c dung lam bien, hang hoac ten ham. ANSI C co 32 t khoa sau (va ch viet bang ch thng !!):

Keyword

Description

 

Keyword

Description

auto

Storage class specifier

 

int

Type specifier

break

Statement

 

long

Type specifier

case

Statement

 

register

Storage class specifier

char

Type specifier

 

return

Statement

const

Storage class modifier

 

short

Type specifier

continue

Statement

 

signed

Type specifier

default

Label

 

sizeof

Operator

do

Statement

 

static

Storage class specifier

double

Type specifier

 

struct

Type specifier

else

Statement

 

switch

Statement

enum

Type specifier

 

typedef

Statement

extern

Storage class specifier

 

union

Type specifier

float

Type specifier

 

unsigned

Type specifier

for

Statement

 

void

Type specifier

goto

Statement

 

volatile

Storage class modifier

if

Statement

 

while

Statement

Kieu d lieu char

Mot oi tng cua kieu char bieu th mot ky t n ng vi bang ma ky t 8 bits ASCII.

Bien ky t

char variablename;

hoac:

char variablename1, variablename2, variablename3;

Format Ky t: %c

Hang ky t

x = `A';
x = 65;

hoac

 
x = `a';
x = 97;

Ky t thoat (\)

Ky t

Mo ta

\b

BACKSPACE

\f

Ket thuc trang (form-feed)

\r

Xuong hang (RETURN)

\t

TAB

Xuat cac ky t

Dung ham printf(), nhnghi4a trong stdio.h, e xuat len man anh.

V du: In cac ky t len man anh.

 
1: /* Printing out characters */
2: #include <stdio.h>
3:
4: main()
5: {
6: char c1;
7: char c2;
8:
9: c1 = `A';
10: c2 = `a';
11: printf("Convert the value of c1 to character: %c.\n", c1);
12: printf("Convert the value of c2 to character: %c.\n", c2);
13: return 0;
14: }
 
Ket qua:
 
Convert the value of c1 to character: A.
Convert the value of c2 to character: a.

V du: Bien oi so thanh ky t (dung format %c)

1: /* Converting numeric values back to characters */
2: #include <stdio.h>
3:
4: main()
5: {
6: char c1;
7: char c2;
8:
9: c1 = 65;
10: c2 = 97;
11: printf("The character that has the numeric value of 65 is: %c.\n", c1);
12: printf("The character that has the numeric value of 97 is: %c.\n", c2);
13: return 0;
14: }
 
Ket qua:
 
The character that has the numeric value of 65 is: A.
The character that has the numeric value of 97 is: a.

Kieu d lieu int

D lieu so khong co thanh phan thap phan. Khoang ln cua no phu thuoc vao o dai bo nh lu tr.

So nguyen 32 bits : t 2147483647 (231_1) en -2147483648.
So nguyen 16 bits : t 32767 (215_1) en -32768.

Bien int

int variablename;
int variablename1, variablename2, variablename3;

Format int: %f

V du: Bien oi ky t thanh so (dung format %d)

1: /* Showing the numeric values of characters */
2: #include <stdio.h>
3:
4: main()
5: {
6: char c1;
7: char c2;
8:
9: c1 = `A';
10: c2 = `a';
11: printf("The numeric value of A is: %d.\n", c1);
12: printf("The numeric value of a is: %d.\n", c2);
13: return 0;
14: }
 
Ket qua:
The numeric value of A is: 65.
The numeric value of a is: 97.

Kieu d lieu float

D lieu so thc bieu dien bang dau cham ong.

So thc 32 bits: Khoang gia tr +/-1.0*1037, co t nhat 6 so le thap phan. Cac phep tnh gi so thc va so thc hoac vi so nguyen cho ket qua kieu float.

Bien float

float variablename;
float variablename1, variablename2, variablename3;

Format float: %f

V du: Xuat ket qua chia so int va float

1: /* Integer vs. floating-point divisions */
2: #include <stdio.h>
3:
4: main()
5: {
6: int int_num1, int_num2, int_num3; /* Declare integer variables */
7: float flt_num1, flt_num2, flt_num3; /* Declare floating-point variables */
8:
9: int_num1 = 32 / 10; /* Both divisor and dividend are integers */
10: flt_num1 = 32 / 10;
11: int_num2 = 32.0 / 10; /* The divisor is an integer */
12: flt_num2 = 32.0 / 10;
13: int_num3 = 32 / 10.0; /* The dividend is an integer */
14: flt_num3 = 32 / 10.0;
15:
16: printf("The integer divis. of 32/10 is: %d\n", int_num1);
17: printf("The floating-point divis. of 32/10 is: %f\n", flt_num1);
18: printf("The integer divis. of 32.0/10 is: %d\n", int_num2);
19: printf("The floating-point divis. of 32.0/10 is: %f\n", flt_num2);
20: printf("The integer divis. of 32/10.0 is: %d\n", int_num3);
21: printf("The floating-point divis. of 32/10.0 is: %f\n", flt_num3);
22: return 0;
23: }
 
Ket qua:
 
The integer divis. of 32/10 is: 3
The floating-point divis. of 32/10 is: 3.000000
The integer divis. of 32.0/10 is: 3
The floating-point divis. of 32.0/10 is: 3.200000
The integer divis. of 32/10.0 is: 3
The floating-point divis. of 32/10.0 is: 3.200000

Kieu d lieu double

D lieu so thc co o chnh xac gap oi.

So thc 64 bits, co t nhat 10 so le thap phan.

Bien double

double variablename;
double variablename1, variablename2, variablename3;

Format double: %e, %E

Quy c at ten bien

Cac ky t sau ay c dung at ten bien: A-Z, a-z, 0-9 (nhng khong c at au), dau gach di (_).

Bien khong c cha ky hieu toan t.
Bien khong c cha dau (.).
Bien khong c cha (‘).
Bien khong c cha ky hieu ac biet nh *, @, #, ?, v.v..

Canh bao

Khong bao gi dung t khoa cua C hoac ten cac ham th vien C lam ten bien mac du co the c.

Bai tap

  1. Viet chng trnh xuat cac gia tr so cua cac ky t Z va z..
  2. Viet chng trnh xuat cac cac ky t co gia tr so 72 and 104.
  3. Cho so double dbl_num = 123.456;,viet chng trnh xuat gia tr tren theo format float va doule.
  4. Viet chng trnh xuat gia tr bang so cua ky t xuong hang (\n).

Phan trc | Muc luc | Phan sau