0

I'm beginner on C and checking this code

int is_finished()
{
    int counter = 0;
    sem_wait(&global_mutex);
    counter = NotEatenCount;
    sem_post(&global_mutex);    
    return counter==0;
}

the function should return an integer. What does return counter==0 means in here? It looks like:

if(counter==0){
  return true;
}
else{
 return false;
}
JayGatsby
  • 1,541
  • 6
  • 21
  • 41
  • 4
    The **function** can only return int, so the evaluation of `counter==0` is irrelevant. (it is boolean (cast to 0 or 1 integer) , since c89, IIRC) – wildplasser May 13 '15 at 18:41
  • Any non-zero value will be considered true for the conditional evaluation effect, so the safe thing to do is to compare with `0` not with `1` in any case, or just use the expression as a truth value. – Iharob Al Asimi May 13 '15 at 18:43

6 Answers6

1

The best solution is to check - print the typeof (:

Anyhow, the answer is both. In C, the true/false keywords are not built-in the language. They are merely a definition, or a part of a library that uses these, for example:

#define TRUE 1
#define FALSE 0

Such definitions are saved as integers if they are numbers, and thus, every boolean is an integer (but the opposite is not true... unless you define it that way). The norm is that 0 is FALSE, and the rest is TRUE.

EDIT: Looking back, I've actually answered a similar question that has to do with objective C. If you're intrested, take a look here.

Community
  • 1
  • 1
A. Abramov
  • 1,823
  • 17
  • 45
  • 3
    In C, booleans do really exist. `_Bool`. – chux - Reinstate Monica May 13 '15 at 18:45
  • @chux take a look here: http://stackoverflow.com/questions/8724349/difference-between-bool-and-bool-types-in-c _Bool is an older version of the boolean from `stdbool`, which has a similar definition to the one I gave. – A. Abramov May 13 '15 at 18:47
  • 1
    `_Bool` is part of the language - it is a boolean type - It is available even if code does not include `stdbool.h`. `bool` is a typedef'd in `stdbool.h` and also `true`, `false` is defined. – chux - Reinstate Monica May 13 '15 at 18:54
  • @chux I see... my bad. I'll edit. Always good to study something new (: – A. Abramov May 13 '15 at 18:55
  • 3
    It worth mentioning that `_Bool` was introduced in C99 – Eugene Sh. May 13 '15 at 18:56
  • 1
    And saying that `true` and `false` keywords do not exist is misleading: they are not keywords but they do exist as macro definitions in `stdbool.h`. – ouah May 13 '15 at 18:57
1

== evaluates to an integer that is either 0 or 1, depending on the comparison outcome. In this case, the function will return 1 (true) if counter is equal to 0; it will return 0 otherwise.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
1

Since you are using the 'equal to' operator and not the 'assignment' operator you will get a boolean function (Difference here: http://en.wikipedia.org/wiki/Operators_in_C_and_C++). So how your return statement is deciding this is:

return (counter == 0);


I believe what you want to return here is

return counter

then outside of your is_finished() function equate if counter == 0 or not and have that represent your bool.

1

I prefer to avoid using true and false because they are inherent in the way you use the C language.

Integer values 0 are false. Non-0 values are true. So

if (apples) {...}

will execute when apples != 0.

The C language will give a value of 1 in response to a specific test.

int a = (apples > 0);

That is not so in all languages, some give -1 for true. But there is never any need to compare with 0, true, or false.

if (a) {...}

Only clumsy code will try to use true and false. Otherwise, the logic flow is sweet.

To point out the absurdity of a statement using TRUE

if ((ptr == NULL) == TRUE) {...}

when

if (ptr == NULL) {...}

does the job.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1
 return counter==0

it returns the int value 1 if counter is equal to 0 or the int value 0 otherwise.

In C equality and relational operators always yield an int value of either 0 or 1. Note that this is different in C++ where by default these operators return a value of type bool.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

return counter==0 produces an int because the return type is int.
It has a value of 0 or 1.


counter==0 itself produces an int.

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. ... C11 §6.5.9 3

The function returns an int regardless even is the 3.14 was attempted to be returned - it will be converted to an int. @wildplasser

int is_finished(){   
   ...
   return true;
   ...
   return 3.14
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256