計算機程式設計 - 關鍵詞



到目前為止,我們已經學習了兩個重要的概念:變數及其資料型別。我們討論瞭如何使用intlongfloat來指定不同的資料型別。我們還學習瞭如何命名變數以儲存不同的值。

雖然本章節並非單獨必學,因為保留關鍵字是基本程式設計語法的一部分,但我們將其單獨列出,以便在學習資料型別和變數之後更容易理解。

像int、long和float一樣,C程式語言還支援許多其他關鍵字,我們將用於不同的用途。不同的程式語言提供不同的保留關鍵字集,但所有程式語言都有一個重要且通用的規則:我們不能使用保留關鍵字來命名我們的變數,這意味著我們不能將變數命名為intfloat,這些關鍵字只能用於指定變數的資料型別。

例如,如果您嘗試將任何保留關鍵字用作變數名,則會收到語法錯誤。

#include <stdio.h>

int main() {
   int float;
   float = 10;
   
   printf( "Value of float = %d\n", float);
}

編譯上述程式時,會產生以下錯誤:

main.c: In function 'main':
main.c:5:8: error: two or more data types in declaration specifiers
   int float;
......

現在讓我們為我們的整數變數賦予一個合適的名稱,然後上述程式應該能夠成功編譯並執行:

#include <stdio.h>

int main() {
   int count;
   count = 10;

   printf( "Value of count = %d\n", count);
}

C語言保留關鍵字

下表包含C程式語言支援的幾乎所有關鍵字:

auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double

Java程式設計保留關鍵字

下表包含Java程式語言支援的幾乎所有關鍵字:

abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while

Python程式設計保留關鍵字

下表包含Python程式語言支援的幾乎所有關鍵字:

and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield

我們知道你不可能記住所有這些關鍵字,但是我們列出它們是為了供你參考,並解釋保留關鍵字的概念。所以,在為變數命名時要小心,不要使用該程式語言的任何保留關鍵字。

廣告
© . All rights reserved.