C 什么';这个三元运算符的结果是什么?
有人能给我解释一下下面三元运算符的结果吗 这是完整的代码:C 什么';这个三元运算符的结果是什么?,c,linux,glibc,C,Linux,Glibc,有人能给我解释一下下面三元运算符的结果吗 这是完整的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <errno.h> #include "get_num.h" static void gnFail(const char *fname, const char *msg, const char *ar
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include "get_num.h"
static void
gnFail(const char *fname, const char *msg, const char *arg, const char *name)
{
fprintf(stderr, "%s error", fname);
if (name != NULL)
fprintf(stderr, " (in %s)", name);
fprintf(stderr, ": %s\n", msg);
if (arg != NULL && *arg != '\0')
fprintf(stderr, "
offending text: %s\n", arg);
exit(EXIT_FAILURE);
}
static long
getNum(const char *fname, const char *arg, int flags, const char *name)
{
long res;
char *endptr;
int base;
if (arg == NULL || *arg == '\0')
gnFail(fname, "null or empty string", arg, name);
base = (flags & GN_ANY_BASE) ? 0 : (flags & GN_BASE_8) ? 8 :
(flags & GN_BASE_16) ? 16 : 10;
errno = 0;
res = strtol(arg, &endptr, base);
if (errno != 0)
gnFail(fname, "strtol() failed", arg, name);
if (*endptr != '\0')
gnFail(fname, "nonnumeric characters", arg, name);
if ((flags & GN_NONNEG) && res < 0)
gnFail(fname, "negative value not allowed", arg, name);
if ((flags & GN_GT_0) && res <= 0)
gnFail(fname, "value must be > 0", arg, name);
return res;
}
long
getLong(const char *arg, int flags, const char *name)
{
return getNum("getLong", arg, flags, name);
}
int
getInt(const char *arg, int flags, const char *name)
{
long res;
res = getNum("getInt", arg, flags, name);
if (res > INT_MAX || res < INT_MIN)
gnFail("getInt", "integer out of range", arg, name);
return (int) res;
}
*此代码是根据(linux编程接口书)中的linux库部分编写的。如果第一个条件为真,则使用0作为表达式的结果,否则请检查第二个条件,如果为真,则使用8。否则,检查第三个并使用16,否则使用10。每个条件都是检查是否在
标志中设置了相应的位如果第一个条件为真,则使用0作为表达式的结果,否则检查第二个条件,如果为真,则使用8。否则,检查第三个并使用16,否则使用10。每个条件都是检查标志中是否设置了相应的位
base = (flags & GN_ANY_BASE) ? 0 : (flags & GN_BASE_8) ? 8 :
(flags & GN_BASE_16) ? 16 : 10;
等于:
if(flags & GN_ANY_BASE)
base = 0;
else if(flags & GN_BASE_8)
base = 8;
else if(flags & GN_BASE_16)
base = 16;
else
base = 10;
三元运算符是赋值和,
后结合力最低的运算符
等于:
if(flags & GN_ANY_BASE)
base = 0;
else if(flags & GN_BASE_8)
base = 8;
else if(flags & GN_BASE_16)
base = 16;
else
base = 10;
三元运算符是赋值和,
后结合力最低的运算符
if(flags & GN_ANY_BASE)
base = 0;
else if(flags & GN_BASE_8)
base = 8;
else if(flags & GN_BASE_16)
base = 16;
else
base = 10;