C語言宣告的優先順序規則

2022-03-30 22:59:43 字數 2286 閱讀 8548

c語言宣告的有時候又些難以理解,如果不理解它的優先順序規則,就經常難以分析,下面列出其規則:

a  宣告從它的名字開始讀取,然後按照優先順序順序依次讀取

b  優先順序從高到低依次是:

b.1   宣告中被括號起來的那部分

b.2   字尾操作符:

括號()表示這是乙個函式,方括號表示這是乙個陣列

b.3  字首操作符:星號*表示 「指向...的指標」

c  如果const和(或)volatile關鍵字的後面緊跟型別說明符(如int,long等),那麼它作用於型別說明符,在其他情況下,const和(或)volatile關鍵字作用於它左邊緊鄰的指標星號。

《c專家程式設計》中通過圖表分析c語言的宣告,如下圖

c宣告解析**

/*

(expert c programming)

*/#include

#include

#include

#include

#define maxtokens 100

#define maxtokenlen 64

enum type_tag

;struct token

;struct token stack[maxtokens];

struct token this;

#define pop stack[top--]

#define push(s) stack[++top] = s;

int top=-1;

enum type_tag classify_string(void)

if(!strcmp(s,"

volatile

")) return qualifier;

if(!strcmp(s,"

void

")) return type;

if(!strcmp(s,"

char

")) return type;

if(!strcmp(s,"

signed

")) return type;

if(!strcmp(s,"

unsigned

")) return type;

if(!strcmp(s,"

short

")) return type;

if(!strcmp(s,"

int")) return type;

if(!strcmp(s,"

long

")) return type;

if(!strcmp(s,"

float

")) return type;

if(!strcmp(s,"

double

")) return type;

if(!strcmp(s,"

struct

")) return type;

if(!strcmp(s,"

union

")) return type;

if(!strcmp(s,"

enum

")) return type;

return identifier;

}void gettoken(void)

if(*p == '*')

this.string[1] = '

\0';

this.type = *p;

return;

}read_to_first_identifier()

printf("

%s is

",this.string);

gettoken();

}deal_with_arrays()

gettoken();

printf("

of ");}}

deal_with_function_args()

gettoken();

printf("

function returning

");}

deal_with_pointers()

}deal_with_declarator()

deal_with_pointers();

while(top>=0)

else

printf("

%s",pop.string);}}

int main()

C語言宣告優先順序規則

a 宣告從它的名字開始讀取,然後按照優先順序順序依次讀取。b 優先順序從高到低依次是 b.1 宣告中被括號括起來的部分 b.2 字尾操作符 括號 表示這是乙個 函式,而方括號表示這是乙個陣列 b.3 字首操作符 號表示 指向.的指標 c 如果const和 或 volatile關鍵字的後面緊跟型別說明...

C語言宣告的優先順序規則

a 宣告從它的名字開始讀取,然後按照優先順序順序依次讀取 b 優先順序從高到低依次是 b.1 宣告中被括號括起來的那部分 b.2 字尾操作符 括號 表示這是乙個函式,而方括號表示這是乙個陣列 b.3 字首操作符 星號 表示 指向.的指標 c 如果const和 或 volatile關鍵字的後面緊跟型別...

理解C語言宣告的優先順序規則

a 宣告從它的名字開始讀取,然後按照優先順序順序依次讀取 b 優先順序從高到低依次是 b.1 宣告中被括號括起來的那部分 b.2 字尾操作符 括號 表示這是乙個函式,而 表示這是乙個陣列 b.3 字首操作符 星號 表示 指向.的指標 c 如果const和 或 volatile關鍵字的後面緊跟型別說明...