编程知识 cdmana.com

Basic knowledge of c++ - operators and expressions 1 (detailed)

一、赋值运算符与表达式

赋值运算符

下表列出了 C 语言支持的赋值运算符:

运算符描述实例
=简单的赋值运算符,把右边操作数的值赋给左边操作数C = A + B 将把 A + B 的值赋给 C
+=加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数C += A 相当于 C = C + A
-=减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数C -= A 相当于 C = C - A
*=乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数C *= A 相当于 C = C * A
/=除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数C /= A 相当于 C = C / A
%=求余数且赋值运算符,求两个操作数的模赋值给左边操作数,浮点数不适用取余数.C %= A 相当于 C = C % A

赋值运算符支持的是C语言的基本数据类型,包括char、int和double,字符串(字符数组)不能使用赋值运算符.

示例(book17.c)

/*
 *  程序名:book17.c,This program demonstrates the use of the assignment operator.
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int    C=0;     // 定义变量C并初始化
  int    A=21;    // 定义变量A并初始化

  C=A;
  printf("运算符  = 演示,C的值 = %d\n",C);

  C+=A;  // C=C+A;
  printf("运算符 += 演示,C的值 = %d\n",C);

  C-=A;  // C=C-A;
  printf("运算符 -= 演示,C的值 = %d\n",C);

  C*=A;  // C=C*A;
  printf("运算符 *= 演示,C的值 = %d\n",C);

  C/=A;  // C=C/A;
  printf("运算符 /= 演示,C的值 = %d\n",C);

  C=200;
  C%=A;  // C=C%A;
  printf("运算符 %= 演示,C的值 = %d\n",C);

  return 0;
}

运行效果
在这里插入图片描述


练习题:赋值运算符

Implement the assignment operation of three variables,并输出三个变量的值.Please select the wrong assignment operation.

#include <stdio.h>

int main(int argc, char** argv)
{
    (_____1_____)

    printf("a = %d\n", a);
    printf("b = %d\n", b);
    printf("c = %d\n", c);

    return 0;
}

Introduction to the assignment operator:

赋值运算符说明示例
=普通赋值运算符val = 2
+=Add and assign operationsval += 2 等价于 val = val + 2
-=Subtract and assign operationsval -= 2 等价于 val = val - 2
*=Multiply and assign operationsval *= 2 等价于 val = val * 2
/=Divide and assign operationsval /= 2 等价于 val = val / 2
%=The remainder and assignment operationval %= 2 等价于 val = val % 2
<<=Left shift and assignment operationval <<= 1 等价于 val = val << 1
>>=Right shift and assignment operationval >>= 1 等价于 val = val >> 1
&=Bitwise AND and assignment operationval &= 1 等价于 val = val & 1
^=Bitwise XOR and assignment operationval ^= 1 等价于 val = val ^ 1
|=Bitwise OR and assignment operationval |= 1 等价于 val = val | 1
    int a, b, c;
    a = 10;
    b = 10;
    c = 10;
    int a, b, c;
    a = b = c = 10;
    int a = 10, b = 10, c = 10;
    int a = b = c = 10;

答案:    int a = b = c = 10;


二、Arithmetic operators and expressions

算术运算符

下表显示了 C 语言支持的所有算术运算符.假设变量 A 的值为 18,变量 B 的值为
5,则:

运算符描述实例
+两个数相加A+B 将得到 23
-一个数减另一个数A-B 将得到 13
*两个数相乘A*B 将得到 90
/分子除以分母A/B 将得到 3.6
%余数运算符,整除后的余数B%A 将得到 3
++自增运算符,整数值增加 1A++ 将得到 19
自减运算符,整数值减少 1A-- 将得到 17

示例(book15.c)

/*
 *  程序名:book15.c,This program demonstrates the use of arithmetic operators.
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  double    A=18;      // 定义变量A,赋值18
  double    B=5;       // 定义变量B,赋值5

  printf("A的值是:%lf\n",A);
  printf("B的值是:%lf\n",B);

  printf("A+B的值是:%lf\n",A+B);
  printf("A-B的值是:%lf\n",A-B);
  printf("A*B的值是:%lf\n",A*B);
  printf("A/B的值是:%lf\n",A/B);

  //printf("A除B的余数是:%lf\n",A%B);

  A++;  // 自增1
  printf("AThe auto-incremented value is :%lf\n",A);

  B--;  // 自减1
  printf("BThe decremented value is :%lf\n",B);

  return 0;
}

运行效果
在这里插入图片描述

There are two ways to write the self-increment or self-decrement of a variable:

变量名++; // Indicates that the variable will be incremented after this use of the variable;

++变量名; // Indicates that it will be incremented before the variable is used this time;

变量名–; // Indicates that the variable will be incremented after this use of the variable;

–变量名; // Indicates that it is decremented before the variable is used this time;

示例(book16.c)

/*
 *  程序名:book16.c,This program demonstrates arithmetic operators++和--的使用.
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int    ii=0;      // 定义变量ii并初始化

  ii=10;
  printf("ii++的值是:%d\n",ii++);  // Incremented after use
  printf("ii的值是:%d\n",ii);

  ii=10;
  printf("++ii的值是:%d\n",++ii);  // Increment before use
  printf("ii的值是:%d\n",ii);

  return 0;
} 

运行效果
在这里插入图片描述

练习题:打印被10整除的数

打印100以内(包含100)能被10整除的正整数,How to use the increment operator in the right place to achieve this,Please choose the correct answer(1)处.

#include <stdio.h>

#define UPPER 100

int main(int argc, char** argv)
{
    int i = 1;
    (_____1_____)

    return 0;
}

 答案:

    while (i++ < UPPER)
        if (i % 10 == 0)
            printf("%d\n", i);

版权声明

以上文章来自C语言技术网(www.freecplus.net

作者:码农有道


版权声明
本文为[mooczhimahu]所创,转载请带上原文链接,感谢
https://cdmana.com/2022/218/202208060731512484.html

Scroll to Top