不使用c99标准用bool编译c代码 [英] Compiling c code with bool without using c99 standard

查看:20
本文介绍了不使用c99标准用bool编译c代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 C 中使用 bool 变量编译代码,并且我已经包含了 stdbool 头文件,但是当我编译它时,我没有指定我想用 c99 标准编译它(所以它被编译ANSI C 标准),但它仍然有效.我想知道为什么会这样?这是代码:

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. 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.
", name);
    bool exit = false;
    char c;
    printf("Do you wish to exit the program ? (Y/N) ");
    while (!exit) {
        c = getchar();
        if (c == '
') {
            continue;
        }
        printf("Do you wish to exit the program ? (Y/N) ");
        if (c == 'Y' || c == 'y') {
            exit = true;
        }
    }
    printf("Have a nice day %s
", name);
    return 0;
}

还有一个关于我的代码的问题.在询问您是否希望退出程序的部分中,我已使用以下输入对其进行了测试:nn是

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 : n n y

出于某种原因,它第四次将问题打印到控制台,但我不明白为什么.我已经设置了它,所以如果输入是 Y/y,while 循环中的下一次迭代不应该发生,但由于某种原因它再次打印出来,有人可以解释我做错了什么吗?

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,它将不会退出循环:

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 :

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

int main(int argc, char* argv[]) {
    for(int i = 0; i < argc; i++)
        printf("argv[%d] = %s
", 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.
", name, lastname);
    return 0;
}

我不知道我为什么这样做,但我在 while 循环之前添加了一个 getchar() 调用并尝试以这种方式编译它然后程序运行良好,由此我假设 fgetsscanf 函数干扰getchar 函数,但我不知道为什么有人可以解释?

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 fgetsscanf functions interfering with the getchar function but I'm not sure why could someone explain ?

推荐答案

大多数 C 编译器通过扩展来扩展基本语言.一种这样的扩展可能是让 stdbool.h 即使在 C90 模式下也能工作.如果你真的想,你通常可以用一些编译器标志关闭大多数扩展,例如对于 gcc,使用 -std=c90.不确定额外的头文件,毕竟文件还在那里,所以不管模式如何,它都可能被包含在内.

Most C compilers extend the base language with extensions. One such extension could be to let the stdbool.h work even in C90 mode. If you really want to, you can usually turn off most of the extensions with some compiler flag, e.g. for gcc use -std=c90. Not sure about extra headers, the file is still there after all, so it can probably be included regardless of mode.

对于您的第二个问题,请尝试逐步执行程序,在每一步打印 c 的值.它应该使正在发生的事情变得相当明显.

For your second question, try stepping through the program, printing the value of c at each step. It should make it fairly obvious what's happening.

这篇关于不使用c99标准用bool编译c代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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