使用isdigit()宏来解析命令行

use*_*007 0 c macros

我有以下代码从命令行读取参数.如果字符串是这种形式hw:1,0我想打破.

gboolean parse_one_option (gint opt, const gchar * arg, GError ** err)
{
    switch (opt) {
    case DEVICE:
        if (!strncmp(arg, "hw:", 3) && isdigit(arg[3]) && arg[4] == ',' && isdigit(arg[5])) {
            char *device = g_strdup (arg);
            break;
        break;
Run Code Online (Sandbox Code Playgroud)

编译器给了我一个警告:

warning: implicit declaration of function 'isdigit' is invalid in C99 [-Wimplicit-function-declaration]
                    if (!strncmp(arg, "hw:", 3) && isdigit(arg[3]) && arg[4] == ',' && isdigit(arg[5])) {
                                                   ^
Run Code Online (Sandbox Code Playgroud)

还有一个问题:

将g_strdup与GOptionContext结合使用是正确的

小智 9

您需要使#include <ctype.h>此功能/宏可用.

  • @nishant"它们可能被实现为宏",就像它们通常那样. (3认同)