返回定义(开关)

时间:2015-11-12 21:03:36

标签: c

我正在编写带开关功能的代码。我希望我的默认代码打印“错误”并使程序停止运行。我应该在默认结束时添加return 1;吗?

如果是的话,这是对的吗?

switch (course3)
{
    case 'I':
        course3 = 5;
        grade3 = grade3 + 2;
        break;
    case 'O': 
        course3 = 4;
        grade3 = grade3 - 3;
        break;
    case 'A': 
        course3 = 3;
        grade3 = grade3 + 8;
        break;
    case 'D': 
        course3 = 2;
        grade3 = grade3*1.06;
        break;
    case 'L': 
        course3 = 1;
        grade3 = 10 * sqrt(grade3);
        break;
    default:
        printf("Error\n");
        return 1;
}

1 个答案:

答案 0 :(得分:1)

假设这个代码块驻留在select t.id, t.title, r.id, r.body from forum_responses r inner join forum_topics t on (r.forum_topic_id = t.id) order by r.id desc; +----+--------------+----+----------------------------------+ | id | title | id | body | +----+--------------+----+----------------------------------+ | 17 | New Topic | 69 | yes | | 19 | Test Topic 1 | 68 | This is a test | | 17 | New Topic | 64 | hey yo | | 19 | Test Topic 1 | 63 | Test Topic Starter | | 18 | Test Topic | 62 | Test. | | 18 | Test Topic | 61 | Test | | 17 | New Topic | 60 | Another test response. | | 17 | New Topic | 59 | Test response. | | 17 | New Topic | 54 | What should this topic be about? | +----+--------------+----+----------------------------------+ 函数中,那么使用select t.id, t.title, r.id, r.body from forum_responses r inner join forum_topics t on (r.forum_topic_id = t.id) group by t.id order by r.id desc; +----+--------------+----+----------------------------------+ | id | title | id | body | +----+--------------+----+----------------------------------+ | 19 | Test Topic 1 | 63 | Test Topic Starter | | 18 | Test Topic | 61 | Test | | 17 | New Topic | 54 | What should this topic be about? | +----+--------------+----+----------------------------------+ 是的,将完成此操作,因为从main返回相当于调用return 1;

如果此块不在main中,则exit语句将返回到调用函数。在这种情况下,您需要明确调用main以获得相同的结果。