C语言学习笔记

  1. 负数求模运算符号决定
    C99规定"趋零截断".
    如果第一个运算数为负数,那么求模结果为负数;如果第一个运算数是正数,那么求模结果也是正数.
    实际上,标准是这样规定的:
    无论何种情况,只要a和b都是整数值,便可以通过
    a % b = a - (a/b) * b
    来计算.

  2. 关于真假的算术表示

    By definition, the numeric value of a relational or logical expression is 1 if the relation is true, and 0 if the relation is false.
    The unary negation opreator ! converts a non-zero operand into 0, and a zero operand into 1.
    而有的函数,比如isdigit,返回非0值表示true,返回0表示false.
    对于判断,比如if,while,for等等,非0意味着true,0意味着false.

  3. 关于char的定义:

    The language does not specify whether variables of type char are signed or unsigned quantities.
    The definition of C guarantees that any character in the machine's standard printing character set will never be negative, so these characters will always be positive quantities in expressions. But arbitrary bit patterns stored in character variables may appear to be negative on some machines, yet positive on others. For portability, specify signed or unsigned if non-character data is to be stored in char variables.

  4. 关于字符的表示:
    一个字符可以使用\ooo(o表示一个八进制数字)或者\xhh(h表示一个十六进制数字)来表示,但是这样最大只能表示数字值为255的字符.以下表达式:
    int c = '\xfff'
    合法么?
    编译通不过,报错.

  5. 位操作实例及问题:

    1. 问题:

      The unary opertator ~ yields the one's complement of an inter; that is, it converts each 1-bit into a 0-bit and vice versa. For example,
      x = x & ~077
      sets the last six bits of x to zero. Note that x & ~077 is independent of word length, and is thus preferable to, for example, x & 0177700, which assumes that x is a 16-bit quantity. The portable form involves no extra cost, since ~077 is a constant expression that can be evaluated at compile time.

你可能感兴趣的:(C语言学习笔记)