Why doesn't return (a % b == 0) work?

C

Chad

Given the following...
#include <stdio.h>

int div(int a, int b)
{
return (a % b == 0);
}

int main(void)
{

printf("the value is: %d\n",div(4,2));
return 0;
}

I get a value of 1 when I run the program...

[cdalten@localhost oakland]$ gcc -g -Wall div.c -o div
[cdalten@localhost oakland]$ ./div
the value is: 1
[cdalten@localhost oakland]$


If 4%2 == 0, then should return look something like

return (0 == 0);

and since 0 == 0, shouldn't return statement return a value of 0
instead of 1 in this case?
 
L

luser-ex-troll

Given the following...
#include <stdio.h>

int div(int a, int b)
{
  return (a % b == 0);

}

int main(void)
{

  printf("the value is: %d\n",div(4,2));
  return 0;

}

I get a value of 1 when I run the program...

[cdalten@localhost oakland]$ gcc -g -Wall div.c -o div
[cdalten@localhost oakland]$ ./div
the value is: 1
[cdalten@localhost oakland]$

If 4%2 == 0, then should return look something like

return (0 == 0);

and since 0 == 0, shouldn't return statement return a value of 0
instead of 1 in this case?

zero IS equal to zero.
this is true.
true == 1.
 
J

Jens Thoms Toerring

Chad said:
Given the following...
#include <stdio.h>
int div(int a, int b)

That's a pretty misleading function name...
{
return (a % b == 0);
}
int main(void)
{
printf("the value is: %d\n",div(4,2));
return 0;
}
I get a value of 1 when I run the program...
If 4%2 == 0, then should return look something like
return (0 == 0);
and since 0 == 0, shouldn't return statement return a value of 0
instead of 1 in this case?

No. Since 0 is equal to 0 the result of the comparison is TRUE
and thus it's 1. Only if the comparison would test to be FALSE
you would get 0.
Regards, Jens
 
J

jfbode1029

[snip]
and since 0 == 0, shouldn't return statement return a value of 0
instead of 1 in this case?

No. If both operands of == are the same, then the result of the
expression is true, which is represented by the value 1.

Your div() function is doing exactly what it is supposed to; you're
just not interpreting the result correctly. If div() returns a 1,
then a is evenly divisible by b.
 
A

Andrey Tarasevich

Chad said:
return (0 == 0);

and since 0 == 0, shouldn't return statement return a value of 0
instead of 1 in this case?

Why? True logical expressions in C evaluate to 1. Is 0 == 0 true? Yes,
it is. So it evaluates to 1, as it should. What makes you think it
should evaluate to 0?
 
D

Don Bruder

Chad said:
Given the following...
#include <stdio.h>

int div(int a, int b)
{
return (a % b == 0);
}

int main(void)
{

printf("the value is: %d\n",div(4,2));
return 0;
}

I get a value of 1 when I run the program...

Sounds like it's working perfectly to me.

In div(), you're asking "Is the result of the expression 'a % b' equal
to zero?"

When you pass in 4,2, the answer to that question is "yes", so div()
returns "1" - C's equivalent of "yes".

If you pass in 4,3, or 7,9, or 149,16, you should get back "0" (or "no")
because in none of these cases is the result equal to zero.
[cdalten@localhost oakland]$ gcc -g -Wall div.c -o div
[cdalten@localhost oakland]$ ./div
the value is: 1
[cdalten@localhost oakland]$


If 4%2 == 0, then should return look something like

return (0 == 0);

and since 0 == 0, shouldn't return statement return a value of 0
instead of 1 in this case?

No. "0 == 0" translated to english from C is a question - "Does zero
equal zero?"

The answer is "yes". Which translates from english to C as "1".

Therefore, your program is behaving *EXACTLY* as it should.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top