将 bool 结果打印为“false”的最佳方法是什么?或“真实”在C语言中?
我必须编写一个程序,其中 main 调用其他函数来测试一系列数字是否小于某个数字,是否所有系列数字都在两个限制之间,以及是否有负值。
我的代码返回值 1 表示 true,0 表示 false,但赋值要求将它们打印为“true”或“false”。我不知道如何从 printf 获取布尔答案作为字符串打印。我用了 if (atl == false) printf("false");在我的 at_least.c 文件和文件 main.c 中,但它只返回一长串 true 或 false(例如:truetruetrue....)。
我不确定这是否是正确的编码,我将其放在错误的位置,或者我需要使用其他一些代码。
这是我的 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 语言编写的,我似乎找不到太多相关内容。
附录:
这是从下面的答案中重复的。
这适用于at_least和负函数,但不适用于 Between函数。我有
void between(int x, bool* bet)
{
if (x >= LOWER && x <= UPPER)
*bet = false;
return;
}
作为我的代码。怎么了?
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 file and in file main.c, but it returns only a long string of true or false (example: 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.
This is my main.c file:
#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.
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
void between(int x, bool* bet)
{
if (x >= LOWER && x <= UPPER)
*bet = false;
return;
}
as my code. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
备用无分支版本:
Alternate branchless version:
您可以使用 C 的 条件(或三元)运算符:
或者也许在您的情况下:
You could use C's conditional (or ternary) operator:
Or perhaps in your case:
<代码>x ? "true" : "false"
上面的表达式返回一个
char *
,因此您可以像这样使用:puts(x ? "true" : "false");< /代码>
或者
printf(" ... %s ... ", x ? "true" : "false");
您可能想为此创建一个宏。
x ? "true" : "false"
The above expression returns a
char *
, thus you can use like this:puts(x ? "true" : "false");
or
printf(" ... %s ... ", x ? "true" : "false");
You may want to make a macro for this.
使用这个:
这将打印出以下内容:
将此与类型 bool 一起使用,但 int 应该以相同的方式工作。
Use this one:
This prints out the following:
Using this with type bool, but int should work the same way.
我有三个数组:
strbool.c
:strbool.h
:免责声明:前缀是必要的,因为以
str
开头的名称为 保留(实际上,只有strbool
),但在 C 或 POSIX 实际使用它们之前,我将使用简短版本。使用它们非常简单:
I have three arrays for that:
strbool.c
:strbool.h
:Disclaimer: The prefixes are necessary because names starting with
str
are reserved (actually, onlystrbool
), but until they are actually used by C or POSIX, I will use the short version.It's very simple to use them: