基本的切换案例

问题描述 投票:-4回答:1

下面提到的程序是开关盒的基本形式。我正在尝试在输出中打印宏变量。有关更多详细信息,请参见输出。它不打印宏变量。

开关盒

#include<stdio.h>
#define a 4
#define d 5

int main()
{
    int x = 10;

    switch (x)
    {
        case a: printf("Number is 40");
        break;

        case d: printf("Number is 50");
        break;

        default: printf("Default case");
        break;
    }
}

期望:数是40

实际:默认情况


请参考第1点和第2点。

#include<stdio.h>
#define a 4
#define d 5

int main()
{
    int x = 4; //(Point1)Here I'm declared the x value as 4 which is equal to the above defined one of the macros. Hence the output has printed the expected value as said by @ed heal. 

    switch (x)
    {
        case a: printf("Number is 40"); //(Point 2)User can be expect anything as dummy output.
        break;

        case d: printf("Number is 50");
        break;

        default: printf("Default case");
        break;
    }
}
c switch-statement
1个回答
5
投票

您将x设置为10。将x设置为10,并且ab均不等于10,因此使用默认的case语句。


0
投票

请参考第1点和第2点。

#include<stdio.h>
#define a 4
#define d 5

int main()
{
    int x = 4; //(Point1)Here I'm declared the x value as 4 which is equal to the above defined one of the macros. Hence the output has printed the expected value as said by @ed heal. 

    switch (x)
    {
        case a: printf("Number is 40"); //(Point 2)User can be expect anything as dummy output.
        break;

        case d: printf("Number is 50");
        break;

        default: printf("Default case");
        break;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.