Thread: Printing Boolean Output of a Function? Can't get program with a macro working...

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    Printing Boolean Output of a Function? Can't get program with a macro working...

    This question regards another exercise from my book.

    I have this rather dull program that is supposed to report back as "True" if it finds a 0 contained in the array, or "False" if there are no zeros.

    Instead, the program is is reporting back "1" for true, or "0" for false.

    I can't figure out how to get it working. I think it has something to do with the %d (int) conversion specifier in the printf line. (Highlighted in orange). I don't suppose %c works either, since that's a single character.

    We haven't gotten to strings yet in the book. Is there a conversion spec for strings that will properly printing out True or False?


    Code:
    #include <stdio.h>
    
    
    #define TRUE 1
    #define FALSE 0
    
    
    typedef int Bool;
    
    
    Bool has_zero(int a[], int n);
    
    
    int main(void)
    {
        int i;
        int a[10] = { 1, 0, 8, 20, 12, 27, 1, 32, 12, 10 };
    
    
        printf("Zero found: %d\n", has_zero(a, 10));
    
    
        return 0;
    }
    
    
    
    
    Bool has_zero(int a[], int n)
    {
        int i;
    
    
        for (i = 0; i < 10; i++)
            if (a[i] == 0)
                return TRUE;   /* Will return from the function when a zero element is found */
    
    
        return FALSE;  /*Can only be reached if all elements are non zero */
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Presumably you're supposed to test has_zero in an if statement where the true branch prints "True" and the false branch prints "False".
    Code:
    printf("Zero found: ");
    if (has_zero(a, 10))
        printf("True");
    else
        printf("False");
    putchar('\n');
    Alternativley you could use the conditional operator (?:) and the %s format specifier for printf:
    Code:
        printf("Zero found: %s\n", has_zero(a, 10) ? "True" : "False");
    Note that your has_zero function shouldn't have a hard-coded 10 in it for the array size. It should use the 'n' parameter, instead.

    Also, if you are allowed to use C99 (and why not?), then you should use the stdbool.h header for your booleans: bool, true, false.
    With gcc, you need to specify c99 as your standard: gcc -std=c99 ...

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    ^ Funny that you mention C99. I'm using the 1st edition (1996) of the KN King book. There is a 2nd edition from 2008.

    I have it only because it was about $100 cheaper than the 2nd edition on eBay. I do have the ebook version now of the 2nd edition and have looked at it a few times, but have stuck with the 1st edition for the first part of the book because it already has a lot of annotations in it.

    There are probably lots of little things like the <stdbool.h> header that are not being taught in the 1996 book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help writing a macro that returns a boolean value
    By zahid990170 in forum C Programming
    Replies: 6
    Last Post: 04-26-2014, 02:19 AM
  2. Prime Number Program using Boolean Function
    By hockeybro12 in forum C Programming
    Replies: 16
    Last Post: 07-26-2013, 12:47 PM
  3. macro program not working?!?!?!
    By SenoritaManu in forum C Programming
    Replies: 3
    Last Post: 09-01-2011, 04:17 AM
  4. Replies: 2
    Last Post: 04-12-2010, 12:57 PM
  5. Program with boolean function
    By sarathius in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 10:47 AM

Tags for this Thread