05.C language operators

A, C language operators

There are six operators in C language, as follows:

1) arithmetic operators;

2) assignment operator;

3) sizeof operator;

4) relational operator;

5) logical operators;

6) Bitwise operators.

This chapter describes only the arithmetic operators, assignment operators and the sizeof operator, relational operators, and logical operators in other chapters do, bitwise operators rarely used, you can understand, not in-depth study.

Second, the arithmetic operators

The following table shows all the arithmetic operators in C language support. A variable assuming the value 18, the value of the variable B is
5, then:

Operators description Examples
+ Two numbers together The resulting 23 A + B
- Save a number to another number AB will get 13
* Multiply two numbers The resulting 90 A * B
/ Numerator by the denominator A / B obtained 3.6
% After the remainder remainder operator, divisible The resulting 3 B% A
++ Increment operator, an integer value is incremented by 1 A ++ will get 19
Decrement operator, reducing the integer 1 A-- will get 17

Example (book15.c)

/*
 *  程序名:book15.c,此程序演示算术运算符的使用。
 *  作者: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("A自增后的值是:%lf\n",A);

  B--;  // 自减1
  printf("B自减后的值是:%lf\n",B);

  return 0;
}

running result

Here Insert Picture Description
Variable increment or decrement two written:

Variable name ++; // increment expressed in this variable after use;

++ variable name; // increment represents Before using this variable;

Variable names -; // increment expressed in this variable after use;

- variable name; // representation before use variables from this reduction;

Example (book16.c)

/*
 *  程序名:book16.c,此程序演示算术运算符++和--的使用。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

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

  ii=10;
  printf("ii++的值是:%d\n",ii++);  // 在使用后自增
  printf("ii的值是:%d\n",ii);

  ii=10;
  printf("++ii的值是:%d\n",++ii);  // 在使用前自增
  printf("ii的值是:%d\n",ii);

  return 0;
} 

running result

Here Insert Picture Description

Third, the assignment operator

The following table lists the C language supported by the assignment operator:

Operators description Examples
= Simple assignment operator, the values ​​assigned to right-hand operand left operand A + C = A + B value B will be assigned to C
+= Was added and the assignment operator, the result of adding the number to the right of the left operand operations assigned to the left operand C + = A corresponding to C = C + A
-= Save and assignment operator, the left minus the right operand operations assigned to the left operand results C - = A corresponds to the C = C - A
*= And by the assignment operator, the right operand the result of multiplying the left operand assigned to left operand C * = A corresponding to C = C * A
/= In addition result and the assignment operator, the left operand is divided by the right operand to the left operand assignment C / = A equivalent C = C / A
%= And the remainder number assignment operators, sum of two operands modulus assigned to left operand, modulo the number of floating-point numbers is not applicable. C% = A corresponds to the C = C% A

Assignment operator is supported by the basic data type of C language, including int char
Double, String (character array) can not use the assignment operator.

Example (book17.c)

/*
 *  程序名:book17.c,此程序演示赋值运算符的使用。
 *  作者: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;
}

running result

Here Insert Picture Description

Four, sizeof operator

sizeof C language is a keyword, which is used to calculate the variables (or data type) occupies the number of bytes of memory in the current system.

sizeof is not a function, such a question is because writing is a bit like the sizeof function, sizeof there are two writing:

The type of data

sizeof(数据类型);

Data types must be enclosed in parentheses.

printf("字符型变量占用的内存是=%d\n",sizeof(char));   // 输出:字符型变量占用的内存是=1
printf("整型变量占用的内存是=%d\n",sizeof(int));   // 输出:整型变量占用的内存是=4

For variable

sizeof(变量名);
sizeof 变量名;

Variable names can not enclosed in brackets, parentheses usage is more common, most programmers use this form.

int ii;
printf("ii占用的内存是=%d\n",sizeof(ii));   // 输出:ii占用的内存是=4
printf("ii占用的内存是=%d\n",sizeof ii);   // 输出:ii占用的内存是=4

V. operator precedence

In ± * / four operators, the * / higher priority than ±, in the C language, other operators have precedence, I do not want to introduce their priorities, I wrote twenty years of the program, also remember the priority relationship of the various operators.

I do not recommend programmer in mind the priority of various operators, but with brackets to solve all the problems, the priority is the highest brackets, parentheses expression will take precedence, the order of execution so that each operator at a glance a.

int d = a + (b * c);

Sixth, Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

Published 29 original articles · won praise 2 · Views 691

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104629132