"hist"
不
'hist'
“历史的”
只是一个字符文字(如第节所述)
2.14.3
字符文字是用单引号括起来的一个或多个字符,如在x中,可以选择前面加上字母u、u或L中的一个,如在uy、Uz或Lx中。
不以u、u或L开头的字符文字是普通字符文字,也称为窄字符文字。
包含单个c字符的普通字符文字具有char类型,其值等于执行字符集中c字符编码的数值。
包含多个c字符的普通字符文字是多字符文字。多字符文字具有int类型和实现定义的值。
至于有更好的方法,这取决于你所说的更好是什么意思:-)
一种可能性是建立一个functon表,它基本上是一个结构数组,每个结构包含一个单词和一个函数指针。
更好的
解决方案,我将把它留给您(这是一种中等先进的技术)-您可能会更好地坚持您所理解的内容。
#include <stdio.h>
typedef struct { // This type has the word and function pointer
char *word; // to call for that word. Major limitation is
void (*fn)(void); // that all functions must have the same
} tCmd; // signature.
// These are the utility functions and the function table itself.
void hello (void) { printf ("Hi there\n"); }
void goodbye (void) { printf ("Bye for now\n"); }
tCmd cmd[] = {{"hello",&hello},{"goodbye",&goodbye}};
// Demo program, showing how it's done.
int main (int argc, char *argv[]) {
int i, j;
// Process each argument.
for (i = 1; i < argc; i++) {
//Check against each word in function table.
for (j = 0; j < sizeof(cmd)/sizeof(*cmd); j++) {
// If found, execute function and break from inner loop.
if (strcmp (argv[i],cmd[j].word) == 0) {
(cmd[j].fn)();
break;
}
}
// Check to make sure we broke out of loop, otherwise not a avlid word.
if (j == sizeof(cmd)/sizeof(*cmd)) {
printf ("Bad word: '%s'\n", argv[i]);
}
}
return 0;
}
在运行时使用:
pax> ./qq.exe hello goodbye hello hello goodbye hello bork
您将获得以下输出:
Hi there
Bye for now
Hi there
Hi there
Bye for now
Hi there
Bad word: 'bork'