在c中将布尔结果打印为“假"或“真"的最佳方法? [英] Best way to print the result of a bool as 'false' or 'true' in c?

查看:19
本文介绍了在c中将布尔结果打印为“假"或“真"的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.c:

This is my 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;
 }

我的一组数字的结果:

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

这是用 C 语言编写的,我似乎找不到太多关于它的内容.

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

提前感谢您的帮助!

附录:

这是从下面的答案中重复的.

This is repeated from an answer below.

这适用于 at_least 和negative 函数,但不适用于 between 函数.我有

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

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

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

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

推荐答案

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

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

  (a > b) ? "True" : "False";

或者在你的情况下:

  x ? "True" : "False" ;

这篇关于在c中将布尔结果打印为“假"或“真"的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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