2
int a;
scanf("%i", &a);
printf("%i", a&&1);

In this program, no matter the input it spits out a 1, even when I try even numbers. The only exception is when a = 0. I might not be understanding the AND operator correctly, but for any even number shouldn't the output be 0?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Changing title to "&& operator in c spitting out 1" or "logical AND operator in c spitting out 1" would make this question more valuable. – chux - Reinstate Monica Feb 10 '15 at 23:02
  • Your example invokes undefined behavior BTW due to reading an uninitialized variable. – Ed S. Feb 10 '15 at 23:09
  • @EdS.: Kind of. It is UB for invalid input, so you're half-right; he should check the return value of `scanf`. – Tim Čas Feb 10 '15 at 23:22
  • 1
    You need to use bit-wise comparison operator '&' to check for an odd number like: num & 1 – MaxZoom Feb 10 '15 at 23:49
  • @TimČas: No; it's UB because he is reading the *value* of `a` in `a && 1`, yet `a` is uninitialized. Reading an uninitialized variable is UB. – Ed S. Feb 10 '15 at 23:57
  • @EdS.: As I said, not for valid input. He has the `scanf` immediately before that, which initializes `a`. – Tim Čas Feb 11 '15 at 00:01
  • @TimČas:... and so it does :). Not sure how I missed that. Sorry. – Ed S. Feb 11 '15 at 00:25

2 Answers2

5

Because the && operator returns a non zero value if both its operands are not zero. Maybe you mean &.

int a;
if (scanf("%i", &a) == 1)
    printf("%i", a & 1);
  • && is the logical AND operator.
  • & is the bitwise AND operator.
Jongware
  • 22,200
  • 8
  • 54
  • 100
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    @Jongware I didn't get why you removed the `'` from `its`, but now I see... I am improving my english while participating here. – Iharob Al Asimi Feb 10 '15 at 23:12
  • 1
    :) Aw, all you have to remember is "'s" is short for "is". [No shortage of people who do not want's to learn](http://stackoverflow.com/search?q=%22want%27s%22) on this site. – Jongware Feb 10 '15 at 23:16
  • [Pronouns just suck in English](http://en.wikipedia.org/wiki/English_personal_pronouns). Particularly I/me/my/mine vs he/him/his/his vs she/her/her/hers. – Brian McFarland Feb 10 '15 at 23:28
1

Operator && is the logical AND operator that returns true/false. In C language false is 0 (zero) otherwise 1 (one) for true as C does not have boolean type. So the result of your operator is correct and as expected.

If you insist on using bool type you can include definition for it as below

// standard way
#include <stdbool.h>

which is basically

#define bool _Bool
#define true 1
#define false 0
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • 1
    "C does not have boolean type" Hmm what is `_Bool` then? – chux - Reinstate Monica Feb 10 '15 at 23:03
  • 1
    C does have a boolean type for the past 16 years. See http://stackoverflow.com/questions/4767923/c99-boolean-data-type – chux - Reinstate Monica Feb 10 '15 at 23:05
  • _Bool is a typedef int _Bool; – MaxZoom Feb 10 '15 at 23:31
  • With a C99 and C11 compiler, try `printf("%d %d\n", (_Bool) 0.9, (int) 0.9);` this prints `"1 0"` as conversion to `_Bool` differ from conversion to `int`. If "_Bool is a typedef int _Bool;", the same result would have occurred. – chux - Reinstate Monica Feb 10 '15 at 23:38
  • Not necessarily. The number 0.9 is not a zero (as false) then it is true (as 1). That is why integer 1 is printed. This behavior is along _Bool definition. – MaxZoom Feb 10 '15 at 23:40
  • I found the source code for it http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/EXTERNAL_HEADERS/stdbool.h – MaxZoom Feb 10 '15 at 23:45
  • @Max Zoom Yes, _Bool is not `int`. If "_Bool was a typedef int _Bool;", then `typedef int B; printf("%d %d\n", (B) 0.9, (int) 0.9);` results in `"1 1"`. But _Bool is a *not* a typedef int. It is its own type. hence `printf("%d %d\n", (_Bool) 0.9, (int) 0.9);` reports different results. – chux - Reinstate Monica Feb 10 '15 at 23:45
  • @Max Zoom Your source indicates `#define bool _Bool #if __STDC_VERSION__ < 199901L && __GNUC__ < 3 typedef int _Bool;"`. So for old versions of C, `bool` is a typedef `int`. Newer C, since C99, has a boolean type `_Bool` and `bool` is a macro for the C boolean type `_Bool`. – chux - Reinstate Monica Feb 10 '15 at 23:50