简体   繁体   English

不使用c99标准使用bool编译c代码

[英]Compiling c code with bool without using c99 standard

I've tried to compile a code using a bool variable in C and I've included the stdbool header but when I compiled it I didn't specify that I want to compile it with the c99 standard (so it was compiled with ANSI C standard) but it worked anyway. 我尝试使用C中的bool变量来编译代码,并且包含了stdbool标头,但是在编译时,我并未指定我要使用c99标准进行编译(因此它是使用ANSI C编译的标准),但还是可以。 I was wondering why is that ? 我想知道为什么吗? Here's the code : 这是代码:

#include <stdio.h>
#include <stdbool.h>

int main() {
    char name[20];
    printf("What's your name ? ");
    gets(name);
    printf("Nice to meet you %s.\n", name);
    bool exit = false;
    char c;
    printf("Do you wish to exit the program ? (Y/N) ");
    while (!exit) {
        c = getchar();
        if (c == '\n') {
            continue;
        }
        printf("Do you wish to exit the program ? (Y/N) ");
        if (c == 'Y' || c == 'y') {
            exit = true;
        }
    }
    printf("Have a nice day %s\n", name);
    return 0;
}

Also another question regarding to my code. 还有一个关于我的代码的问题。 In the part where you are being asked if you wish to exit the program, I've tested it with the following input : nny 在询问您是否要退出程序的部分中,我已经使用以下输入进行了测试:nny

And for some reason it printed out to the console the question for the fourth time and I don't see why. 由于某种原因,它第四次向控制台输出了问题,我不知道为什么。 I've set it so if the input is Y/y the next iteration in the while loop shouldn't take place but for some reason it printed it again, could someone explain me what I did wrong ? 我已经将其设置为如果输入为Y / y,则不应该在while循环中进行下一次迭代,但是由于某种原因,它将再次打印出来,有人可以解释一下我做错了什么吗?

EDIT : So I've edited the code a bit tried to test new things and I've noticed that with the following code if the user input is Y/y it won't come out of the loop : 编辑:所以我已经编辑了一些代码以尝试测试新事物,并且我注意到,使用以下代码,如果用户输入是Y / y,它将不会退出循环:

#include <stdio.h>
#include <stdbool.h>

int main(int argc, char* argv[]) {
    for(int i = 0; i < argc; i++)
        printf("argv[%d] = %s\n", i, argv[i]);
    char name[20];
    printf("What's your name ? ");
    gets(name);
    char lastname[20];
    printf("%s what's your last name ? ", name);
    fgets(lastname, 20, stdin);
    int age;
    printf("%s %s what's your age? ", name, lastname);
    scanf("%d", &age);
    bool exit = false;
    char c;
    while (!exit) {
        printf("Do you wish to exit the program ? (Y/N) ");
        c = getchar();
        getchar();
        if (c == 'Y' || c == 'y')
            exit = true;
    }
    printf("Have a nice day %s %s.\n", name, lastname);
    return 0;
}

I don't know why I did it but I added a getchar() call before the while loop and tried to compile it this way and then the program worked fine, from this I assume that the fgets\\scanf functions interfering with the getchar function but I'm not sure why could someone explain ? 我不知道为什么要这样做,但是我在while循环之前添加了一个getchar()调用,并尝试以这种方式进行编译,然后程序运行正常,由此我认为fgets \\ scanf函数会干扰getchar函数但我不确定为什么有人可以解释?

About second question: 关于第二个问题:

Your code deciding to leave program or not is strange, it can be done simpler way: 您决定退出程序的代码很奇怪,可以用更简单的方法完成:

int c=0;
do{
    if(c!='\n')
        puts("Do you wish to exit the program? (Y/N) ");
    c=getchar();
}while(c!='y' && c!='Y');

When code is simpler, it's harder to make mistake in it. 当代码更简单时,很难在其中犯错误。

Most C compilers extend the base language with extensions. 大多数C编译器通过扩展来扩展基本语言。 One such extension could be to let the stdbool.h work even in C90 mode. 一种此类扩展可能是让stdbool.h甚至在C90模式下也可以工作。 If you really want to, you can usually turn off most of the extensions with some compiler flag, eg for gcc use -std=c90 . 如果确实愿意,通常可以使用一些编译器标志来关闭大多数扩展,例如,对于gcc使用-std=c90 Not sure about extra headers, the file is still there after all, so it can probably be included regardless of mode. 不确定多余的头文件,毕竟文件仍然存在,因此无论使用哪种模式都可以包含该文件。

For your second question, try stepping through the program, printing the value of c at each step. 对于第二个问题,请尝试逐步执行该程序,并在每个步骤中打印c的值。 It should make it fairly obvious what's happening. 它应该使发生的事情相当明显。

stdbool.h defines bool, true, and false for you - the header itself is not dependent on the language standard you pass in, so long as you have the header on your system. stdbool.h为您定义了bool,true和false-标头本身并不依赖于您传入的语言标准,只要您的系统上具有标头即可。 EDIT - this may only be the case for the stock osx header. 编辑-仅适用于股票osx标头。 gcc's header (4.8) doesn't look like it will work. gcc的标头(4.8)看起来不起作用。

Your logic for exiting the loop doesn't look like how you describe what you want it to do. 您退出循环的逻辑与您描述自己想要的工作方式不同。 You're still testing the previous character in your y/Y section, even though you printed out the question again at that point. 即使此时再次打印出问题,您仍在测试y / Y部分中的上一个字符。 I suggest stepping through your code with a debugger - this will make it more clear to you what's going on. 我建议您使用调试器逐步检查代码-这将使您更清楚地了解发生了什么。 The debugger will be an incredibly important learning tool for you, now and forever. 对于您而言,无论现在还是将来,调试器都是一个非常重要的学习工具。 :) :)

You probably want something like this for your code (remove the first printf question in your code): 您的代码可能需要这样的代码(删除代码中的第一个printf问题):

while (!exit) {
    printf("Do you wish to exit the program ? (Y/N) ");
    c = getchar();
    if (c == '\n') {
        continue;
    }
    if (c == 'Y' || c == 'y') {
        exit = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM