C Coding Basics
Now, to start this thread, I'll first try to sum up this other thread: viewtopic.php?f=9&t=16234
Over there, it was suggested to start a more general thread for discussing tidbits of C and what code snippets mean. That gives more context and helps more enthusiasts and testers to try their hand at coding. I initially asked questions in the above listed thread about a code snippet I saw. Then I wanted to know what various symbols meant. The thread uncovered the following:
-- An asterisk in front of a pointer means to dereference a pointer and deal with its value.
-- An ampersand before a variable is used to get the variable's address rather than its value.
-- A single ampersand by itself means to do a bitwise AND. So if a single bit is false or zero, then it is false or zero in the result. So if you AND 2 (00000010) with 1 (00000001), you get 0 (00000000) since there are not corresponding bits that are both 1. However ANDing 3 (00000011) with 1 (00000001) would give you a decimal 1 (both a decimal 1 and a decimal 3 have the first bit high), and ANDing a decimal 3 with a decimal 2 will give you a decimal 2 (since bit two is high in both but not both high with any of the other bits).
-- Two ampersands by themselves (&&) means to do a short-circuited AND. As opposed to using AND to do a masking operation, this is used when evaluating to see if two or more conditions are true and not evaluating any longer once a condition is found to be false. In languages that don't have a short-circuited AND operator, you can still do the same thing using nested conditional operations. In Quickbasic, one could put IF....THEN clauses inside of IF....THEN clauses, and only do the intended action inside the innermost clause, causing the deeper conditionals to not execute should outer conditionals fail. That would likely produce smaller and faster code than using AND to stack conditions on a single line. In assembly, the way to do similar to a short-circuited AND is to use conditional jumps to where earlier conditionals skip around the other conditionals if they are not true, and then if all the conditionals are true, it falls into the code that works if all conditionals in that group are true.
-- A single pipe symbol means to do a bitwise OR. So if one bit is true, then the bit is true in the result. So ORing decimal 3 (00000011 b) with 0 (00000000 b) yields 3 since both of the lowest 2 bits are high in at least one of the numbers. I used this when I wanted to capitalize the ASCII value of a digit. So I would simply OR the bit mask of 00100000 or whatever it was to make sure that one bit was turned on. And I didn't bother testing the variable first. Since why spend up to 3 cycles on a conditional jump that passes (and reset the prefetch queue on older x86 CPUs) and 1 if it fails, if doing OR in a CPU register only takes one cycle? So 3 cycles if true and 2 if false when you can just presumptively do the OR without testing and only consume a cycle regardless.
-- A double pipe symbol (||) means to do a short-circuited OR. So when comparing two or more conditions, execution of the group of conditions stops once any condition is found to be true. So if you only care if ONE condition is true, there is no need to test them all once you find one to be true. This works similar to the short-circuited AND described above, but evaluations stop on the first true rather than the first false, and the code that is executed when any condition is true would be executed (and nothing is done but go to the next instruction past the block if all are false).
Then there are data types. I found that all the answers about what they mean and their sizes could be found here.