Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is uint strictly positive

Tags:

c

I want to assert that an unsigned int is strictly positive. I was debating between the following two options:

unsigned int i = 1;
assert(i > 0); /*option 1*/
assert(i != 0); /*option 2*/

I found myself stuck in a Buridan's ass dilemma. Is there any reason to prefer one over the other?

like image 608
Benjy Kessler Avatar asked Dec 14 '15 17:12

Benjy Kessler


1 Answers

assert(i > 0); is more readable to other programmers that i is checked for positive integers and including the case for i = 0.


Note: As Joachim pointed in his comment that keep in mind that if the macro NDEBUG was defined at the moment <assert.h> was last included, the macro assert() generates no code, and hence does nothing at all.

like image 129
haccks Avatar answered Oct 08 '22 06:10

haccks