编译C code。与布尔不使用C99标准 [英] Compiling c code with bool without using c99 standard

查看:77
本文介绍了编译C code。与布尔不使用C99标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过用在C布尔变量来编译code和我已经包括了stdbool头,但是当我编译它,我没有指定我想与C99标准编译它(所以它与ANSI C标准编译),但也无妨的工作。
我想知道这是为什么?
这里的code:

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.\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;
}

另外关于我的code另一个问题。
当你被,如果你想退出程序要求的一部分,我用下面的输入进行了测试:
ñ
ñ

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 ?

编辑:
所以,我编辑了codeA位试图测试新的东西,我注意到有以下code。如果用户输入为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\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;
}

我不知道为什么我做到了,但我加了while循环前的getchar()调用,并试图编译它这样,然后该程序工作正常,从这个假设我与fgets \\ scanf函数的功能与干扰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 fgets\scanf functions interfering with the getchar function but I'm not sure why could someone explain ?

推荐答案

大多数C编译器扩展与延伸基本语言。一个这样的扩展可以让 stdbool.h 工作,即使在C90模式。如果你真的想,你通常可以关闭大部分的扩展与一些编译器标志,如海合会使用 -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.

有关你的第二个问题,试图通过该计划步进,打印的ç在每一步的价值。它应使相当明显发生了什么。

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.

这篇关于编译C code。与布尔不使用C99标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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