Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Token trong C/C++

Token là đơn vị nhỏ nhất của chương trình C/C++.

Các loại Token

  • Keywords (Từ khóa): 32 từ khóa C + 30 từ khóa C++ mới (auto, const, double, float, int, short, struct, unsigned, break, continue, else, for, long, signed, switch, void, case, default, enum, goto, register, sizeof, typedef, volatile, char, do, extern, if, return, static, union, while, asm, dynamic_cast, namespace, reinterpret_cast, try, bool, explicit, new, static_cast, typeid, catch, false, operator, template, typename, class, friend, private, this, using, const_cast, inline, public, throw, virtual, delete, mutable, protected, true, wchar_t)
  • Identifiers (Định danh): Tên biến, hàm, struct... Quy tắc: chữ cái/số/_ , bắt đầu bằng chữ/_ , phân biệt hoa/thường, không trùng từ khóa.
  • Constants (Hằng số): Giá trị không thay đổi. Khai báo: const type name=value; hoặc #define.
  • Variables (Biến): Vùng nhớ có tên. Khai báo: type name;.
  • Operators (Toán tử):
    • Số học: + - * / %
    • Tăng/giảm: ++ --
    • Quan hệ: == != > < >= <=
    • Logic: && || !
    • Bitwise: << >> ~ & ^ |
    • Gán: = += -= *= /= %= <<= >>= &= ^= |=
    • Khác: , sizeof() & * ?:

Ví dụ

#include <iostream.h>
int main() {
    int a,b; // a,b là biến (identifier)
    cout<<"Enter first:"; cin>>a;
    cout<<"Enter second:"; cin>>b;
    int sum = a+b; // + là operator, sum là variable
    cout<<"Sum: "<