案例中的三个点(案例'0' ... '9':) 是有效的C 语言切换语法吗? [英] Are triple dots inside a case (case '0' ... '9':) valid C language switch syntax?

查看:11
本文介绍了案例中的三个点(案例'0' ... '9':) 是有效的C 语言切换语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DRBD 软件 (user/drbdtool_common.c)

const char* shell_escape(const char* s)
{
    /* ugly static buffer. so what. */
    static char buffer[1024];
    char *c = buffer;

    if (s == NULL)
        return s;

    while (*s) {
        if (buffer + sizeof(buffer) < c+2)
            break;

        switch(*s) {
        /* set of 'clean' characters */
        case '%': case '+': case '-': case '.': case '/':
        case '0' ... '9':
        case ':': case '=': case '@':
        case 'A' ... 'Z':
        case '_':
        case 'a' ... 'z':
            break;
        /* escape everything else */
        default:
            *c++ = '\';
        }
        *c++ = *s++;
    }
    *c = '';
    return buffer;
}

我以前从未在 C 中见过这种三点"结构(case '0' ... '9':).它是一种有效的标准 C 语言吗?或者这是某种预处理器魔法?这里发生了什么?

I have never seen this "triple dot" construction (case '0' ... '9':) in C before. Is it a valid standard C language? Or is that some kind of preprocessor magic? What's going on here?

推荐答案

正如其他人所说,这是一个特定于编译器的扩展.使用正确的选项(例如,gcc -std=c99 -pedantic)调用编译器,它应该会发出警告.

As others have said, this is a compiler-specific extension. Invoke the compiler with the right options (say, gcc -std=c99 -pedantic), and it should warn you about it.

我还要指出,它的使用具有潜在的危险,除了另一个编译器可能不会实现它.'a' ... 'z' 表示 26 个小写字母——但 C 标准不保证它们的值是连续的.例如,在EBCDIC中,字母之间有标点符号.

I'll also point out that its use is potentially dangerous, apart from the fact that another compiler might not implement it. 'a' ... 'z' denotes the 26 lowercase letters -- but the C Standard doesn't guarantee that their values are contiguous. In EBCDIC, for example, there are punctuation characters among the letters.

另一方面,我怀疑 gcc 或 Sun C 是否支持使用字母 连续的字符集的系统.(它们是 ASCII 及其所有派生词,包括 Latin-1、Windows-1252 和 Unicode.)

On the other hand, I doubt that either gcc or Sun C supports systems that use a character set in which the letters aren't contiguous. (They are in ASCII and all its derivatives, including Latin-1, Windows-1252, and Unicode.)

另一方面,它不包括重音字母.(取决于 DRBD 的使用方式,这可能是也可能不是问题.)

On the other other hand, it excludes accented letters. (Depending on how DRBD is used, that may or may not be an issue.)

这篇关于案例中的三个点(案例'0' ... '9':) 是有效的C 语言切换语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆