c语言运算符_C语言运算符

c语言运算符

C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulation. Operators are used in programs to manipulate data and variables.

C语言支持一组丰富的内置运算符。 运算符是一个符号,告诉编译器执行某种数学或逻辑运算。 程序中使用运算符来操纵数据和变量。

C operators can be classified into following types:

C运算符可以分为以下几种类型:

  • Arithmetic operators

    算术运算符

  • Relational operators

    关系运算符

  • Logical operators

    逻辑运算符

  • Bitwise operators

    按位运算符

  • Assignment operators

    赋值运算符

  • Conditional operators

    条件运算符

  • Special operators

    特殊运营商

算术运算符 (Arithmetic operators)

C supports all the basic arithmetic operators. The following table shows all the basic arithmetic operators.

C支持所有基本算术运算符。 下表显示了所有基本算术运算符。

OperatorDescription
+adds two operands
-subtract second operands from first
*multiply two operand
/divide numerator by denominator
%remainder of division
++Increment operator - increases integer value by one
--Decrement operator - decreases integer value by one
操作员 描述
+ 加两个操作数
-- 从第一个减去第二个操作数
* 两个操作数相乘
/ 分子除以分母
除法余数
++ 增量运算符-将整数值增加1
- 减量运算符-将整数值减一

关系运算符 (Relational operators)

The following table shows all relation operators supported by C.

下表显示了C支持的所有关系运算符。

OperatorDescription
==Check if two operand are equal
!=Check if two operand are not equal.
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand
操作员 描述
== 检查两个操作数是否相等
!= 检查两个操作数是否不相等。
> 检查左侧的操作数是否大于右侧的操作数
< 左边的检查操作数小于右边的操作数
> = 检查左操作数是否大于或等于右操作数
<= 检查左侧的操作数是否小于或等于右侧的操作数

逻辑运算符 (Logical operators)

C language supports following 3 logical operators. Suppose a = 1 and b = 0,

C语言支持以下3个逻辑运算符。 假设a = 1b = 0

Operator DescriptionExample
&& Logical AND(a && b) is false
|| Logical OR(a || b) is true
! Logical NOT(!a) is false
操作员 描述
&& 逻辑与 (a && b)为假
|| 逻辑或 (a || b)是真的
逻辑非 (!a)是错误的

按位运算符 (Bitwise operators)

Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of bits from right to left. Bitwise operators are not applied to float or double(These are datatypes, we will learn about them in the next tutorial).

按位运算符在位级别执行数据操作。 这些运算符还执行从右到左的位移位 。 按位运算符不适用于floatdouble (这些是数据类型,我们将在下一个教程中了解它们)。

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
操作员 描述
按位与
| 按位或
^ 按位异或
<< 左移
>> 右移

Now lets see truth table for bitwise &, | and ^

现在让我们看一下真值表中的& |^

a ba & ba | ba ^ b
0 0000
0 1011
1 0011
1 1110
一个 b a和b 一个| b a ^ b
0 0 0 0 0
0 1个 0 1个 1个
1个 0 0 1个 1个
1个 1个 1个 1个 0

The bitwise shift operator, shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.

位移位运算符,对位值进行移位。 左操作数指定要移位的值,右操作数指定值中必须移位的位数。 两个操作数具有相同的优先级。

Example :

范例

a = 0001000
b = 2
a << b = 0100000 
a >> b = 0000010

赋值运算符 (Assignment Operators)

Assignment operators supported by C language are as follows.

C语言支持的赋值运算符如下。

Operator DescriptionExample
= assigns values from right side operands to left side operanda=b
+= adds right operand to the left operand and assign the result to lefta+=b is same as a=a+b
-= subtracts right operand from the left operand and assign the result to left operanda-=b is same as a=a-b
*= mutiply left operand with the right operand and assign the result to left operanda*=b is same as a=a*b
/= divides left operand with the right operand and assign the result to left operanda/=b is same as a=a/b
%= calculate modulus using two operands and assign the result to left operanda%=b is same as a=a%b
操作员 描述
= 将值从右侧操作数分配给左侧操作数 a = b
+ = 将右操作数添加到左操作数,并将结果分配给左 a + = b与a = a + b相同
-= 从左操作数中减去右操作数,并将结果分配给左操作数 a- = b与a = ab相同
* = 将多个左操作数与右操作数相加,并将结果分配给左操作数 a * = b与a = a * b相同
/ = 将左操作数除以右操作数,并将结果分配给左操作数 a / = b与a = a / b相同
%= 使用两个操作数计算模数并将结果分配给左操作数 a%= b与a = a%b相同

条件运算符 (Conditional operator)

The conditional operators in C language are known by two more names

C语言中的条件运算符还有另外两个名称

  1. Ternary Operator

    三元运算符

  2. ? : Operator

    :运算符

It is actually the if condition that we use in C language decision making, but using conditional operator, we turn the if condition statement into a short and simple operator.

实际上,这是我们在C语言决策中使用的if条件,但是使用条件运算符,我们将if条件语句转换为简短的运算符。

The syntax of a conditional operator is :

条件运算符的语法为:

expression 1 ? expression 2: expression 3

Explanation:

说明:

  • The question mark "?" in the syntax represents the if part.

    问号“?” 语法中的if部分。

  • The first expression (expression 1) generally returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3)

    通常,第一个表达式(表达式1)返回true或false,然后根据该表达式确定执行(表达式2)还是(表达式3)

  • If (expression 1) returns true then the expression on the left side of " : " i.e (expression 2) is executed.

    如果(表达式1)返回true,则执行“:”左侧的表达式,即(表达式2)。

  • If (expression 1) returns false then the expression on the right side of " : " i.e (expression 3) is executed.

    如果(表达式1)返回false,则执行“:”右侧的表达式,即(表达式3)。

特殊操作员 (Special operator)

Operator DescriptionExample
sizeof Returns the size of an variable sizeof(x) return size of the variable x
& Returns the address of an variable&x ; return address of the variable x
* Pointer to a variable*x ; will be pointer to a variable x
操作员 描述
大小 返回变量的大小 sizeof(x)返回变量x的大小
返回变量的地址 &X ; 变量x的返回地址
* 指向变量的指针 *X ; 将是指向变量x的指针

翻译自: https://www.studytonight.com/c/operators-in-c.php

c语言运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值