== is a test for equality. = is an assignment.
Any good C book should cover this (fairly early on in the book I would imagine).
For example:
int i = 3; // sets i to 3.
if (i == 3) printf("i is 3\n"); // prints it.
Just watch out for the heinous:
if (i = 4) { }
which is valid C and frequently catches people out. This actually assigns 4 to the variable i and uses that as the truth value in the if statement. This leads a lot of people to use the uglier but safer:
if (4 == i) {}
which, if you accidentally use = instead of ==, is a compile-time error rather than something that will bite you on the backside while your program is running :-)
The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4:
||
&&
b4
No magic there.
Answer from paxdiablo on Stack OverflowVideos
== is a test for equality. = is an assignment.
Any good C book should cover this (fairly early on in the book I would imagine).
For example:
int i = 3; // sets i to 3.
if (i == 3) printf("i is 3\n"); // prints it.
Just watch out for the heinous:
if (i = 4) { }
which is valid C and frequently catches people out. This actually assigns 4 to the variable i and uses that as the truth value in the if statement. This leads a lot of people to use the uglier but safer:
if (4 == i) {}
which, if you accidentally use = instead of ==, is a compile-time error rather than something that will bite you on the backside while your program is running :-)
The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4:
||
&&
b4
No magic there.
a == b is a test if a and b are equal.
a = b is called an assignment, which means to set the variable a to having the same value as b.
(You type | with Shift-\ in the US keyboard layout.)
The first operation is called bitwise OR | and it works as follows imagining the numbers A=200 and B=184, which in binary will be, 11001000 and 10111000, respectively. The operator "|" will compare bit a bit of each of those numbers, and returns 1 when either of the bits is 1, 0 otherwise. So in your case:
11001000
| 10111000
--------
= 11111000
the result will be 248 (11111000 in binary). Therefore DDRB |= 0b00000001; is DDRB = DDRB | 0b00000001.
The second operation (e.g., DDRB = 0b00000001) is just an assignment of a value to a variable.
Do the two lines have same effect of writing one to data register B ?
No.
DDRB = 0b00000001;
writes one to bit-0 and zero to bits 1 to 7, while:
DDRB |= 0b00000001;
is a read-modify-write operation equivalent to:
DDRB = DDRB | 0b00000001;
so it writes a one to bit-0 as before and leaves all other bits unchanged.
So for example if DDRB's current value were 0b11110000 after:
DDRB = 0b00000001;
it will be 0b00000001 while after:
DDRB |= 0b00000001;
it will be 0b11110001.
So one sets DDRB to 1, while the other sets DDRB:Bit-0 to 1.
I'm just starting and really confused. Thanks!
In C, a == b == c is equivalent to (a == b) == c, where a == b yields 1 if true, 0 otherwise.
In your case, a == b is true, so a == b == c is equivalent to 1 == c, which is false.
You can further try it with:
printf("%d\n%d\n", 0 == 0, 0 == 1);
which gives the result:
1
0
The comparison operator == has left to right associativity.
So, an expression like
a == b == c
is the same as
(a == b ) == c
In your case, a, b, c all having values 0, the expression turns out to be
(0 == 0) == 0
or,
1 == 0
which yields 0 (Falsy). So, control goes to the else part.
Then, for the aforementioned reason, a == b evaluates to 1 (truthy), so the corresponding if block is executed.