Piseagan
Piseagan

Reputation: 541

What is the 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 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?

Upvotes: 21

Views: 84658

Answers (5)

alk
alk

Reputation: 70979

Use this one:

#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;
}

This prints out the following:

x=false
y=true

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

Upvotes: 4

Mitch Wheat
Mitch Wheat

Reputation: 300827

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

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

Or perhaps in your case:

  x ? "True" : "False";

Upvotes: 28

I have three arrays for that:

strbool.c:

#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"};

strbool.h:

#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];

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

It's very simple to use them:

#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;
}
var is True

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215627

Alternate branchless version:

"false\0true"+6*x

Upvotes: 50

Oscar Korz
Oscar Korz

Reputation: 2487

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.

Upvotes: 18

Related Questions