Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

C/C++ Tokens


C++ Tokens คือหน่วยย่อยที่สุดของโปรแกรม

C++ เป็นซูเปอร์เซตของ C ดังนั้นโครงสร้างส่วนใหญ่ของ C ถูกต้องใน C++ ด้วยความหมายและการใช้งานไม่เปลี่ยน ดังนั้น tokens, expressions, และ data types คล้ายกับ C

ต่อไปคือ C++ tokens: (ส่วนใหญ่คล้าย C tokens)

  • Keywords
  • Identifiers
  • Constants
  • Variables
  • Operators

Keywords

Keywords เป็นคำสงวนที่มีความหมายคงที่ ไม่สามารถเปลี่ยนความหมายได้ ความหมายและการทำงานของคำเหล่านี้รู้จักโดยคอมไพเลอร์แล้ว C++ มี keywords มากกว่า C และคำเพิ่มเติมเหล่านี้มีความสามารถพิเศษ

มี 32 คำ ดังนี้:

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

อีก 30 คำสงวนที่ไม่มีใน C จึงใหม่ใน C++:

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

Identifiers คือชื่อที่ให้กับสิ่งต่างๆ เช่น ตัวแปร โครงสร้าง และฟังก์ชัน ชื่อ identifier ต้องเป็น unique เพราะใช้ในการรันโปรแกรม

กฎการตั้งชื่อ identifier:

  • อนุญาตเฉพาะตัวอักษร ตัวเลข และ underscore
  • ตัวแรกต้องเป็นตัวอักษหรือ underscore (_)
  • Identifiers แยกตัวพิมพ์เล็กใหญ่
  • คำสงวนไม่สามารถใช้เป็นชื่อ identifier ได้

Constants

Constants เหมือนตัวแปร แต่ค่าไม่เปลี่ยนระหว่างรันหลังจากกำหนด

มีสองวิธีกำหนด constants ใน C++:

  • ใช้คำสำคัญ const
  • ใช้ #define preprocessor

การประกาศ constant:

const [data_type] [constant_name]=[value];

Variable

Variable คือชื่อที่มีความหมายของตำแหน่งเก็บข้อมูลในหน่วยความจำคอมพิวเตอร์ เมื่อใช้ตัวแปร คุณอ้างอิงที่อยู่หน่วยความจำ

Syntax การประกาศตัวแปร:

[data_type] [variable_name];

ตัวอย่าง

#include 
int main() {
    int a,b;// a and b are integer variable
    cout<<" Enter first number :";
    cin>>a;
    cout<<" Enter the second number:";
    cin>>b;
    int sum;
    sum=a+b;
    cout<<" Sum is : "<

Operators

C++ operator คือสัญลักษณ์ที่ใช้ทำการดำเนินการทางคณิตศาสตร์หรือตรรกะ

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

Arithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus

Increment and Decrement Operators

OperatorDescription
++Increment
--Decrement

Relational Operators

OperatorDescription
==Is equal to
!=Is not equal to
>Greater than
<Less
>=Greater than or equal to
<=Less than or equal to

Logical Operators

OperatorDescription
&&And operator. Performs logical conjunction of two expressions.(if both expressions evaluate to True, result is True. If either expression evaluates to False, the result is False)
||Or operator. Performs a logical disjunction on two expressions.(if either or both expressions evaluate to True, the result is True)
!Not operator. Performs logical negation on an expression.

Bitwise Operators

OperatorDescription
<<Binary Left Shift Operator
!=Is not equal to
>>Binary Right Shift Operator
~Binary One's Complement Operator
&Binary AND Operator
^Binary XOR Operator
|Binary OR Operator

Assignment Operators

OperatorDescription
=Assign
+=Increments, then assign
-=Decrements, then assign
*=Multiplies, then assign
/=Divides, then assign
%=Modulus, then assigns
<<=Left shift and assigns
>>=Right shift and assigns
&=Bitwise AND assigns
^=Bitwise exclusive OR and assigns
|=Bitwise inclusive OR and assigns

Misc Operators

OperatorDescription
,Comma operator
sizeOf()Returns the size of a memory location.
&Returns the address of a memory location.
*Pointer to a variable.
? :Conditional Expression