打印布尔在C'假'或'真'的结果,最好的方法? [英] Best way to print the result of a bool as 'false' or 'true' in c?
问题描述
我必须写一个程序,其中主要的调用,测试序列号,如果任何低于一个数字,如果所有系列的数字是两者之间的界限,并且如果有任何负面的其他功能。我的code返回1的值虚假真实和0,但作业要求他们打印为真或假。我不知道如何让布尔答案打印从printf的字符串。我使用,如果(ATL ==假)的printf(假);在我at_least.c和main.c中,但它只返回真或假的长字符串(如:truetruetrue ....)。我不知道这是正确的编码,我把它在错误的地方或者有一些其他的code,我需要使用。
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;
}
我的结果的一组数字:
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
这是C,我似乎无法找到太多的。
This is in C, which I can't seem to find much on.
在此先感谢您的帮助!
附录:
这是从下面的答案重复。
This is repeated from an answer below.
此工作了at_least和负的功能,但不为函数之间。我有
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;
}
我的code。我不知道什么是错的。
as my code. I'm not sure what's wrong.
推荐答案
您可以用C的条件(或三元)运算符:
You could use C's conditional (or ternary) operator :
(a > b) ? "True" : "False";
或者在您的情况:
or perhaps in your case:
x ? "True" : "False" ;
这篇关于打印布尔在C'假'或'真'的结果,最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!