本文介绍了转换GNU受案范围的扩展标准C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GNU的情况下,范围扩展允许范围的情况下在switch语句:

 开关(值){
    情况1 ... 8:
        的printf(你好,1〜8 \\ n);
        打破;
    默认:
        的printf(你好,默认\\ n);
        打破;
}

你会如何将其转换为标准C(C99,C89或)?新增个案陈述?

编辑:你将如何处理非常大的switch语句专门


解决方案

 开关(值)
{
    情况1:
    案例2:
    案例3:
    情况4:
    情况5:
    情况6:
    案例7:
    案例8:
        的printf(你好,1〜8 \\ n);
        打破;
    默认:
        的printf(你好,默认\\ n);
        打破;
}

编辑::要回答这个评论结果
如果你有太多的情况下,那么你可能要考虑与的if-else 结构取代了的switch-case 。它可以是更干净,简洁和放大器;可读。

 如果(价值> = 1&安培;&安培;价值< = 8)
{
    的printf(你好,1〜8 \\ n);
}
其他
{
    的printf(你好,默认\\ n);
}

The GNU case range extension allows case ranges in switch statements:

switch (value) {
    case 1 ... 8:
        printf("Hello, 1 to 8\n");
        break;
    default:
        printf("Hello, default\n");
        break;
}

How would you convert this to standard C (c99, or c89)? Add the individual case statements?

Edit: How would you handle very large switch statements specifically?

解决方案
switch(value)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
        printf("Hello, 1 to 8\n");
        break;
    default:
        printf("Hello, default\n");
        break;
}

EDIT: To answer the comment.
If you have too many cases, then You might want to consider replacing the switch-case with if-else constructs. It can be much cleaner, concise & readable.

if (value >=1 && value <= 8)
{
    printf("Hello, 1 to 8\n");
}
else
{
    printf("Hello, default\n");
}

这篇关于转换GNU受案范围的扩展标准C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:23