Spockwang's Blog

C Gotchas

| 评论

+, - have higher precedence than >>, <<.

a/b if a or b is negative, its result is implementation-dependent (truncated to or from 0) prior to C99, and truncated to 0 by C99. The standard specifies that (a/b)*b + a%b is equal to a.

Array name vs. pointer

Usually an array name is transformed to be a pointer to the first element of the array. But in following situations an array name is not equivalent to a pointer to the first element of the array:

  • as an operand of sizeof operator
  • as an operand of & operator

The declaration of an array and its definition must be compatible, only except as a parameter of a function, in which the declaration of an array is equivalent to the declaration of a pointer. Particularly you must not define a variable to be an array in one file and declare it to be a pointer in another file.

Comments