Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

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

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.

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

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Alternate branchless version:

"falsetrue"+6*x

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...