Home > OS >  Ambiguity about Boolean functions in C
Ambiguity about Boolean functions in C

Time:12-11

Part of my C code 1:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
[snip!!]
#define FALSE            0
#define TRUE             1
[snip!!]
typedef  int              Boolean;
[snip!!]
typedef  struct {
   int identification ;
   char name[NAMESIZE] ;
   char subject[SUBJECTSIZE] ;
   int  grade ;
} RECORD ;
typedef  struct {
                 char type ;
                 RECORD student ;
                } TRANSACTION ;
typedef  struct {
                  char occupied ;
                  RECORD student ;
                } MASTER ;

char *prog ;
Boolean get_transaction_record(FILE *fp, TRANSACTION *transaction);
[snip!!]
void  main(int argc, char *argv[])
{
   FILE     *fpmas,*fptrans ;
   int      current_key ,
            relative_record_number ;
   Boolean     allocated;
   TRANSACTION transaction;
   MASTER      master ;
   clrscr();
   prog = argv[0];
   [snip!!]
   else
   {
    get_transaction_record(fptrans, &transaction) ;
    current_key = choose_next_key(transaction.student.identification);
    while(current_key != TRAILER)
{
 relative_record_number = do_initial_status(
           current_key,&allocated, fpmas,&master);
 while(current_key == transaction.student.identification)
 {
 apply_transaction (&allocated, &transaction,&master) ;
 get_transaction_record(fptrans, &transaction);
 }
 do_final_status (fpmas, relative_record_number, allocated, &master);
 current_key = choose_next_key(transaction.student.identification);
} //end of while
fcloseall();
} //end of else
}

Part of code 2:

Boolean get_transaction_record(FILE *fp, TRANSACTION *transaction)
{
    if (fscanf (fp, "M%c ss-",
    &transaction -> student. Identification,
        &transaction -> type,
        transaction -> student.name,
        transaction -> student. Subject,
        &transaction -> student. Grade) == 5)
       return(TRUE);
    return(FALSE);
}

How these return(); work (in the screenshot I attached) ?! I see before Boolean application like this typedef int Boolean; before. But that usage was in accompany with if() . But I do not see any if() in this code! My main question is, how it works when the return value (TRUE or FALSE), get back to main() function'?! In main(), there is no condition evaluation to take decision about that (TRUE or FALSE) returned value. So what?

Boolean - return

CodePudding user response:

First, your C learning book is largely outdated. C99 (out in 1999) allows #include <stdbool.h> that contain a bool type and true/false defines, use these instead of making your own boolean.

C and many other languages are a bit weird with boolean, if and what is true of false. In C what is 0 is false, everything else is true, hence why TRUE is defined as 1 and FALSE defined as 0 in your book. Note that comparison operators such as ==, &&, ||, ! (not to be confused with binary operators &, |, ~) returns either 1 or 0 depending of the condition.

See this example:

#include <stdio.h>
#include <stdbool.h>
bool return_the_truth(void)
{
    return true; // A trivial function that returns true.
}
int main(void) {
    if (2)
        printf("As you see just \"2\" is true since it's different of zero\n");
    if (0)
        printf("That line won't be printed\n");
    if (4 - 4)
        printf("Neither this one since 4-4 is zero\n");
    if (true == 1)
        printf("true is in fact a one.\n");
    if (true == 2)
        printf("But merely a integer with value 1 so this line won't print.\n");
    // You can invoke a function that returns a boolean and ignore it's return value. 
    return_the_truth();
    // It is never mandatory to use the returned value. This example is absurd but perfectly valid
    fopen("delete_me.txt","a"); // <- Don't forget to delete this file afterward.

    if (printf("printf() return an int that is the numbers of characters printed.\n"))
        printf("Hence again, this line is printed because what in the if() is not zero.");
    int boolean_check_result =  42 == 40 2;
    if (boolean_check_result)
        printf("You can write expressions like this, there boolean_check_result is %d.\n",boolean_check_result);
    printf("You can performs math with boolean, there is %d true expressions there, it works because a comparison returns 1 if true, 0 if false and we doing a sum of 1 and zeros (it's brainfuck to understand don't do that or people reviewing your code will hate you).\n",
        (4 == 5)   (4 < 5)   (4 != 5)   (4 > 5)   (4 >= 5) // <- The expressions.
    );
    // These examples below are valid C.
    5   6; // This sum goes to... nothing
    7 == 7; // Same for this boolean, nothing will happen
    printf; // Functions are kind of constants variables so that works.
    // It's a non-sense, a compiler will output warnings because making computations without using the results is likely a programming error.
    printf("But remind you that in the case of a function (returning a boolean or not), there might be side-effects you want and don't care about the returned value such as with this printf().\n");
}

// Did you know that you can use a `void` value ?
// The code below is perfectly valid.
// It's useful for C   wrappers when you want to exactly mimic the behavior a library in order to catch errors.
void a_function_that_does_nothing(void)
{
}
void you_can_return_a_void(void)
{
    return a_function_that_does_nothing(); // Allowed because the function return a void
}

There is tons of valid case where a returned boolean are not used inside a if().

So to answer your original question (that I might forgot while writing this answer hehe...):

how it works when the return value (TRUE or FALSE), get back to main() function'?!

And !

  • Related