在IF及其他条件下接收相同的输出,如何解决?

发布于 2025-02-06 20:13:55 字数 305 浏览 3 评论 0原文

我正在尝试执行以下程序,但是如果其他条件,它会提供相同的输出。在输出中,即使我以周日为价,我也只能获得工作日。

#include <stdio.h>
int main(void)
{
char day; 
printf("Enter Day name: \n"); 
scanf("%c", &day);

if (day =="sunday"){
    printf("Holiday");
    }
    else{
        printf("Working day.");
    }
return 0;
}

I am trying to execute below program but it give same output, in if else condition. In output I only get Working day only even I give value as sunday.

#include <stdio.h>
int main(void)
{
char day; 
printf("Enter Day name: \n"); 
scanf("%c", &day);

if (day =="sunday"){
    printf("Holiday");
    }
    else{
        printf("Working day.");
    }
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2025-02-13 20:13:55

该程序无效。

要输入字符串,您需要声明字符数组,而不是类型char的对象,其中将存储输入的字符串。例如,

char day[10];
printf("Enter Day name: \n"); 
scanf("%9s", day);

要比较字符串,您需要使用标准功能strcmp在标题&lt; string.h&gt;中声明。

#include <string.h>

//...

if ( strcmp( day, "sunday" ) == 0 ){
    printf("Holiday\n");
    }
else{
        printf("Working day.\n");
    }

The program is invalid.

To enter a string you need to declare a character array instead of an object of the type char where the entered string will be stored. For example

char day[10];
printf("Enter Day name: \n"); 
scanf("%9s", day);

To compare strings you need to use standard function strcmp declared in the header <string.h>.

#include <string.h>

//...

if ( strcmp( day, "sunday" ) == 0 ){
    printf("Holiday\n");
    }
else{
        printf("Working day.\n");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文