C语言 在c中将bool结果打印为“false”或“true”的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7617479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:46:25  来源:igfitidea点击:

Best way to print the result of a bool as 'false' or 'true' in c?

cprintfboolean

提问by Piseagan

I have to write a program in which main calls other functions that test a series of number if any are less than a number, if all the series' numbers are between two limits, and if any are negative. My code returns the values of 1 for true and 0 for false, but the assignment asks that they be printed as 'true' or 'false'. I'm not sure how to get the bool answers to print as a string from printf. I used if (atl == false) printf("false"); in my at_least.c and in main.c, but it returns only a long string of true or false (ex: truetruetrue....). I'm not sure if that is the correct coding and I'm putting it in the wrong spot or there was some other code that I need to use.

我必须编写一个程序,其中 main 调用其他函数来测试一系列数字,如果有任何小于一个数字,如果所有系列的数字都在两个限制之间,如果有任何为负数。我的代码返回值 1 为真,0 为假,但赋值要求将它们打印为“真”或“假”。我不知道如何从 printf 中获取 bool 答案以作为字符串打印。我使用 if (atl == false) printf("false"); 在我的 at_least.c 和 main.c 中,但它只返回一长串真或假(例如:truetruetrue....)。我不确定这是否是正确的编码,我把它放在错误的地方,或者我需要使用其他一些代码。

This is my main.c:

这是我的 main.c:

#include "my.h"

int main (void)

{
    int     x;
    int     count    = 0;
    int     sum      = 0;
    double  average  = 0.0;
    int     largest  = INT_MIN;
    int     smallest = INT_MAX;
    bool    atlst    = false;
    bool    bet      = true;
    bool    neg      = false;
    int     end;

    while ((end = scanf("%d",&x)) != EOF)
        {
            sumall(x, &sum);                           //calling function sumall
            count++;
            larger_smaller(x, &largest, &smallest);    //calling function larger_smaller
            if (atlst == false)
               at_least(x, &atlst);                    //calling function at_least if x < 50
            if (bet == true)
               between(x, &bet);                       //calling function between if x is between 30 and 40 (inclusive)
            if (neg == false)
               negative(x, &neg);                      //calling function negative if x < 0
        }
    average = (double) sum / count;         
    print(count, sum, average, largest, smallest, atlst, bet, neg);
    return;
 }

my results for a set of numbers:

我对一组数字的结果:

The number of integers is:           15
The sum is               :         3844
The average is           :       256.27
The largest is           :          987
The smallest is          :          -28
At least one is <  50    :            1     //This needs to be true
All between  30 and  40  :            0     //This needs to be false
At least one is negative :            1     //This needs to be true

This is in C, which I can't seem to find much on.

这是在 C 中,我似乎找不到太多。

Thanks in advance for your help!

在此先感谢您的帮助!

ADDENDUM:

附录:

This is repeated from an answer below.

从下面的答案中重复了这一点。

This worked for the at_least and negative functions, but not for the between function. I have

这适用于 at_least 和负函数,但不适用于 between 函数。我有

void between(int x, bool* bet) 
  { 
    if (x >= LOWER && x <= UPPER) 
        *bet = false; 
    return; 
  }

as my code. I'm not sure what's wrong.

作为我的代码。我不确定出了什么问题。

回答by R.. GitHub STOP HELPING ICE

Alternate branchless version:

替代无分支版本:

"false
  (a > b) ? "True" : "False";
true"+6*x

回答by Mitch Wheat

You could use C's conditional (or ternary) operator :

您可以使用 C 的条件(或三元)运算符:

  x ? "True" : "False" ;

or perhaps in your case:

或者在你的情况下:

#include <stdio.h>

#define BOOL_FMT(bool_expr) "%s=%s\n", #bool_expr, (bool_expr) ? "true" : "false"

int main(int iArgC, char ** ppszArgV)
{
    int x = 0;
    printf(BOOL_FMT(x));

    int y = 1;
    printf(BOOL_FMT(y));

    return 0;
}

回答by Oscar Korz

x ? "true" : "false"

x ? "true" : "false"

The above expression returns a char *, thus you can use like this:

上面的表达式返回 a char *,因此您可以像这样使用:

puts(x ? "true" : "false");or printf(" ... %s ... ", x ? "true" : "false");

puts(x ? "true" : "false");或者 printf(" ... %s ... ", x ? "true" : "false");

You may want to make a macro for this.

您可能想为此制作一个宏。

回答by alk

So what about this one:

那么这个呢:

x=false
y=true

This prints out the following:

这将打印出以下内容:

#include "strbool.h"

const char alx_strbool[2][6]    = {"false", "true"};
const char alx_strBool[2][6]    = {"False", "True"};
const char alx_strBOOL[2][6]    = {"FALSE", "TRUE"};

Using this with type bool but int should work the same way.

将此与类型 bool 一起使用,但 int 应该以相同的方式工作。

回答by Cacahuete Frito

I have three arrays for that:

我有三个数组:

strbool.c:

strbool.c

#pragma once

/* rename without alx_ prefix */
#if defined(ALX_NO_PREFIX)
#define strbool     alx_strbool
#define strBool     alx_strBool
#define strBOOL     alx_strBOOL
#endif

extern  const char alx_strbool[2][6];
extern  const char alx_strBool[2][6];
extern  const char alx_strBOOL[2][6];

strbool.h:

strbool.h

#include <stdio.h>

#define ALX_NO_PREFIX
#include "strbool.h"

int main(void)
{
        int var = 7;

        printf("var is %s\n", strBool[!!var]);

        return 0;
}

Disclaimer: The prefixes are necessary because names starting with strare reserved(actually, only strbool), but until they are actually used by C or POSIX, I will use the short version.

免责声明:前缀是必需的,因为以 开头的名称str保留的(实际上,只有strbool),但是在 C 或 POSIX 实际使用它们之前,我将使用简短版本。

It's very simple to use them:

使用它们非常简单:

var is True
##代码##