C 關(guān)鍵字

2018-05-15 18:10 更新

學習C - C關(guān)鍵字

在標準ANSI C編程語言中定義了32個詞作為關(guān)鍵字。

這些關(guān)鍵字具有預定義的用途,不能用于C程序中的任何其他目的。

編譯器使用這些關(guān)鍵字。

關(guān)鍵詞描述
auto將局部變量定義為具有本地生存期
break將控制權(quán)從編程結(jié)構(gòu)中傳遞出去
case分支控制
char基本數(shù)據(jù)類型
const不可修改的值
continue控制循環(huán)開始
default分支控制
doDo While循環(huán)
double浮點數(shù)據(jù)類型
else條件語句
enum定義一組int類型的常量
extern表示其他地方定義的標識符
float浮點數(shù)據(jù)類型
forFor循環(huán)
goto無條件地轉(zhuǎn)移程序控制
if條件語句
int基本數(shù)據(jù)類型
long類型修飾符
register將聲明的變量存儲在CPU寄存器中
return退出函數(shù)
short類型修飾符
signed類型修飾符
sizeof返回表達式或類型大小
static在范圍結(jié)束后保留??變量值
struct將變量組成單個記錄
switch分支控制
typedef創(chuàng)建新類型
union組變量占用相同的存儲空間
unsigned類型修飾符
void空數(shù)據(jù)類型
volatile允許通過背景例程更改變量
while當條件為真時重復執(zhí)行程序

例子


#include <stdio.h> 
int main(void) 
{
   float fRevenue, fCost; 
   fRevenue = 0; 
   fCost = 0; 
   /* profit = revenue - cost */ 
   printf("\nEnter total revenue: "); 
   scanf("%f", &fRevenue); 
   printf("\nEnter total cost: "); 
   scanf("%f", &fCost); 
   printf("\nYour profit is $%.2f\n", fRevenue - fCost); 
   
   return 0;
} 

上面的代碼生成以下結(jié)果。


注意

使用字符作為菜單選項。


#include <stdio.h>  
int main(void) 
{
   char cResponse = "\0"; 
   printf("\n\tAC Control Unit\n"); 
   printf("\na\tTurn the AC on\n"); 
   printf("b\tTurn the AC off\n"); 
   printf("\nEnter your selection: "); 
   scanf("%c", &cResponse); 
   if (cResponse == "a") 
      printf("\nAC is now on\n"); 
   if (cResponse == "b") 
      printf("\nAC is now off\n"); 
    
   return 0;
} 

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號