将 GNU 大小写范围扩展转换为标准 C [英] Converting the GNU case range extension to standard C

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

问题描述

GNU 大小写范围扩展允许 switch 语句中的大小写范围:

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

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

如何将其转换为标准 C(c99 或 c89)?添加个别案例陈述?

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

您将如何专门处理非常大的 switch 语句?

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
"); 
        break;     
    default:         
        printf("Hello, default
");         
        break; 
} 

回答评论.
如果您有太多案例,那么您可能需要考虑将 switch-case 替换为 if-else 结构.它可以更干净、简洁和可读.

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
"); 
} 
else 
{   
    printf("Hello, default
"); 
}  

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

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