zoukankan      html  css  js  c++  java
  • 类型、运算符与表达式

    变量名
    对变量的命名与符号常量的命名存在一些限制条件,
    1. 名字是由字母和数字组成的序列,但其第一个字符必须为字母。下划线“_”被看做是字母,
    2. 大写字母与小写字母是有区别的,所以,x与X是两个不同的名字。
    3. 变量名使用小写字母,
    4. 符号常量名全部使用大写字母。
    5. 局部变量一般使用较短的变量名(尤其是循环控制变量),
    6. 外部变量使用较长的名字。
    数据类型及长度
    C语言只提供了下列几种基本数据类型:
    char  字符型,占用一个字节,可以存放本地字符集中的一个字符
    int  整型,通常反映了所用机器中整数的最自然长度
    float  单精度浮点型
    double  双精度浮点型
    short int sh;  短整型   int 可以省略
    long int counter; 长整型int 可以省略
     
    short类型通常为16位,
    1ong类型通常为32
     
     
       /* strlen:  return length of s */
       int strlen(char s[])
       {
           int i;
     
           while (s[i] != '\0')
               ++i;
           return i;
       }
     
    常量
     
    #define VTAB '\013'   /* ASCII vertical tab */
    #define BELL '\007'   /* ASCII bell character */
    上述语句也可以用十六进制的形式书写为:
    #define VTAB '\xb'   /* ASCII vertical tab */
    #define BELL '\x7'   /* ASCII bell character */
    ANSI C语言中的全部转义字符序列如下所示:
    \a 响铃符  \\  反斜杠
    \b 回退符  \?  问号
    \f 换页符  \'  单引号
    \n 换行符  \"  双引号
    \r 回车符  \ooo 八进制数
    \t 横向制表符  \xhh 十六进制数
    \v 纵向制表符
     
     
    声明
    所有变量都必须先声明后使用
    对数组而言,const限定符指定数组所有元素的值都不能被修改
     
    算术运算符
     
    二元算术运算符包括:
    +、-、*、/、%(取模运算符)。
     
     
    如果某一年的年份能被4
    整除但不能被100整除,那么这一年就是闰年,此外,能被400整除的年份也是闰年。因此,
    可以用下列语句判断闰年:
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        printf("%d is a leap year\n", year);
    else
        printf("%d is not a leap year\n", year);
     
    关系运算符与逻辑运算符
    关系运算符包括下列几个运算符:
    >  >= <  <=
     
    相等性运算符:
    == !=
     
    逻辑非运算符!
    作用是将非0操作数转换为0,将操作数0转换为1。该运算符通常用
    于下列类似的结构中
     
    if (!valid)
    一般不采用下列形式:
    if (valid == 0)
     
    类型转换
     
    它将一串数字转换为相应的数值
    /* atoi:  convert s to integer */
    int atoi(char s[])
    {
       int i, n;
     
       n = 0;
       for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
           n = 10 * n + (s[i] - '0');
       return n;
    }
     
       /* lower:  convert c to lower case; ASCII only */
       int lower(int c)
       {
           if (c >= 'A' && c <= 'Z')
               return c + 'a' - 'A';
           else
               return c;
       }
    自增运算符与自减运算符
     
    自增运算符++使其操作数递增1,
    自减运算符--使其操作数递减1
     
     
       unsigned long int next = 1;
     
       /* rand:  return pseudo-random integer on 0..32767 */
       int rand(void)
       {
           next = next * 1103515245 + 12345;
           return (unsigned int)(next/65536) % 32768;
       }
     
       /* srand:  set seed for rand() */
       void srand(unsigned int seed)
       {
           next = seed;
       }
     
       /* squeeze:  delete all c from s */
       void squeeze(char s[], int c)
       {
          int i, j;
     
          for (i = j = 0; s[i] != '\0'; i++)
              if (s[i] != c)
                  s[j++] = s[i];
          s[j] = '\0';
       }
     
     
       /* strcat:  concatenate t to end of s; s must be big enough */
       void strcat(char s[], char t[])
       {
           int i, j;
     
           i = j = 0;
           while (s[i] != '\0') /* find end of s */
               i++;
           while ((s[i++] = t[j++]) != '\0') /* copy t */
               ;
       }
     
     
    按位运算符
    &  按位与(AND)
    |  按位或(OR)
    ^  按位异或(XOR)
    <<  左移
    >>  右移
    ~  按位求反(一元运算符)
     
       /* getbits:  get n bits from position p */
       unsigned getbits(unsigned x, int p, int n)
       {
           return (x >> (p+1-n)) & ~(~0 << n);
       }
     
    赋值运算符与表达式
    =
    i = i+2
    则可以将这种表达式 i += 2
     
    条件表达式
    if (a > b)
        z = a;
    else
        z = b;
    条件表达式(使用三元运算符“? :”)
    z = (a > b) ? a : b;  /* z = max(a, b) *
     
     
     
       /* bitcount:  count 1 bits in x */
       int bitcount(unsigned x)
       {
           int b;
     
           for (b = 0; x != 0; x >>= 1)
               if (x & 01)
                   b++;
           return b;
       }
    运算符优先级与求值次序




  • 相关阅读:
    [Matlab] 短时傅里叶变换spectrogram函数
    [Word] Word中保存出矢量图
    [Matlab] Matlab 2020b Release Note
    [Matlab] Matlab 2020b Release Note
    [Matlab] Matlab 2020b Release Note
    [Matlab] Matlab 2020b Release Note
    [Matlab] 画图颜色扩展
    [脑电相关] 关于ERD现象
    [PPT技巧] PPT箭头设置及文字设置
    linux 新添加的硬盘格式化并挂载到目录下方法
  • 原文地址:https://www.cnblogs.com/xe2011/p/2564270.html
Copyright © 2011-2022 走看看