Tu hoc C Cac bien kieu va hang so

Thứ năm, 11 Tháng mười 2001, 14:05 GMT+7

Mot ten bien (indentifiers) hop le la mot chuoi gom cac chu cai, chu so hoac ky tu gach duoi. Chieu dai cua mot ten la khong gioi han. Ky tu trong, cac ky tu danh dau deu khong the co mat trong mot ten. Chi co chu cai, chu so va ky tu gach duoi la duoc cho phep.

Them vao do, mot ten bien luon phai bat dau bang mot chu cai. Chung cung co the bat dau bang ky tu gach duoi ( _ ) nhung ky tu nay thuong duoc danh cho cac lien ket ben ngoai (external link). Khong bao gio chung bat dau bang mot chu so.

Mot luat nua ma ban phai quan tam khi tao ra cac ten cua rieng minh la chung khong duoc trung voi bat ky tu khoa nao cua ngon ngu hay cua trinh dich. Vi du cac ten sau day luon luon duoc coi la tu khoa theo chuan ANSI-C++ va do vay chung khong the duoc dung de dat ten:

asm, car, bool, break, marry, catch, to char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, to register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t.

Them vao do, mot so bieu dien khac cua cac toan tu (operator) cung khong duoc dung lam ten vi chung la nhung tu duoc danh rieng trong mot so truong hop:

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq.

Trinh dich cua ban co the them mot tu danh rieng dac trung khac. Vi du, rat nhieu trinh dich 16 bit (nhu cac trinh dich cho DOS) con co the cac tu khoa far, huge va near.

Chu y: Ngon ngu C++ la "case sensitive" co nghia la phan biet chu hoa chu thuong. Do vay bien RESULT khac voi result cung nhu Result.

Cac kieu du lieu

Khi lap trinh, chung ta luu tru cac bien trong bo nho cua may tinh, nhung may tinh phai biet chung ta muon luu tru gi trong chung vi cac kieu du lieu khac nhau se can luong bo nho khac nhau.

Bo nho cua may tinh chung ta duoc to chuc thanh cac byte. Mot byte la luong bo nho nho nhat ma chung ta co the quan ly. Mot byte co the dung de luu tru mot loai du lieu nho nhu la kieu so nguyen tu 0 den 255 hay mot ky tu. Nhung may tinh co the xu ly cac kieu du lieu phuc tap hon bang cach gop nhieu byte lai voi nhau, nhu so nguyen dai hay so thap phan. Tiep theo ban se co mot danh sach cac kieu du lieu co ban trong C++ cung nhu mien gia tri ma chung co the bieu dien:


Ten So byte Mo ta Mien gia tri
char 1 ky tu hay kieu so nguyen 8-bit co dau: -128 to 127
khong dau: 0 to 255
short 2 kieu so nguyen 16-bit co dau: -32763 to 32762
khong dau: 0 to 65535
long 4 kieu so nguyen 32-bit co dau:-2147483648 to 2147483647
khong dau: 0 to 4294967295
int * So nguyen. Do dai cua no phu thuoc vao he thong, nhu trong MS-DOS no la 16-bit, tren Windows 9x/2000/NT la 32 bit... Xem short, long
float 4 Dang dau phay dong 3.4e + / - 38 (7 digits)
double 8 Dang dau phay dong voi do chinh xac gap doi 1.7e + / - 308 (15 digits)
long double 10 Dang dau phay dong voi do chinh xac hon nua 1.2e + / - 4932 (19 digits)
bool 1 Gia tri logic. No moi duoc them vao chuan ANSI-C++. Boi vay khong phai tat ca cac trinh dich deu ho tro no true hoac false

Ngoai cac kieu du lieu co ban noi tren con ton tai cac con tro va cac tham so khong kieu (void) ma chung ta se xem xet sau.

(Theo OurViet Network)

Phan sau: Khai bao va khoi tao bien


Theo dong su kien:
Ngon ngu C (11/10/2001)

Cac bai viet khac:

Tiep theo >>


^ Lên đầu trang ^


Copyright © 2006-2012 by VietBao.vn
Viet Bao Viet Nam - All Rights Reserved.