Help on an "if" statement on a numerical value

S

sapnsapn

Hi folks,

I am looking at an application written in C some 10+ years ago. There
is a lot of such if statements:
if (my_number) {....} else {...}
Here, my_number is an unsigned int. What exactly is the "if" looking
for to evaluate? Is it the existence of a numerical value or a non-zero
value or just any positive value? I am not a C programmer by trade and
hence this question.

LOL,
Sapn
 
S

Simon Morgan

I am looking at an application written in C some 10+ years ago. There is a
lot of such if statements:
if (my_number) {....} else {...}
Here, my_number is an unsigned int. What exactly is the "if" looking for
to evaluate? Is it the existence of a numerical value or a non-zero value
or just any positive value? I am not a C programmer by trade and hence
this question.

Any non-zero value.
 
D

David Resnick

Hi folks,

I am looking at an application written in C some 10+ years ago. There
is a lot of such if statements:
if (my_number) {....} else {...}
Here, my_number is an unsigned int. What exactly is the "if" looking
for to evaluate? Is it the existence of a numerical value or a non-zero
value or just any positive value? I am not a C programmer by trade and
hence this question.

LOL,
Sapn

For any variable type, "if (foo)" is the same as
"if (foo != 0)"

That works for ints, pointers, chars, etc. Some (including
me) think that "if (foo)" should be only used for variables being
used as boolean, but that is a style question. I prefer being
explicit,
as in "if (foo != '\0')", or "if (foo != NULL)" or "if (foo != 0)".
But all work the same as "if (foo)".

-David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top