📜  C语言中的表达式

📅  最后修改于: 2020-10-22 09:48:45             🧑  作者: Mango

C表达式

表达式是一个公式,其中操作数通过使用运算符相互链接以计算值。操作数可以是函数引用,变量,数组元素或常量。

让我们来看一个例子:

a-b;

在上述表达式中,减去字符( – )是一个运算符,和a和b是两个操作数。

C中存在四种类型的表达式:

  • 算术表达式
  • 关系表达
  • 逻辑表达式
  • 条件表达式

每种类型的表达式都采用某些类型的操作数,并使用一组特定的运算符。对特定表达式的求值产生特定值。

例如:

x = 9/2 + a-b;

上面的整行都是语句,而不是表达式。等于之后的部分是一个表达式。

算术表达式

算术表达式是由操作数和算术运算运算符组成的表达式。算术表达式计算类型为int,float或double的值。

当一个表达式仅包含整数操作数时,则称为纯整数表达式;当它仅包含实数操作数时,则称为纯实数表达式;当它同时包含整数和实数操作数时,则称为混合模式表达式。

算术表达式的求值

通过一次执行一个运算来评估表达式。运算符的优先级和关联性决定了单个运算的评估顺序。

当执行单个操作时,可能会发生以下情况:

  • 当两个操作数均为整数类型时,将执行算术运算,并且运算结果将为整数值。例如,3/2将产生1而不是1.5,因为小数部分将被忽略。
  • 当两个操作数均为float类型时,将执行算术运算,并且运算结果将为实数值。例如,2.0 / 2.0将产生1.0,而不是1。
  • 如果一个操作数是整数类型,而另一个操作数是实数类型,则将执行混合算术。在这种情况下,第一个操作数将转换为实数操作数,然后执行算术以产生实数值。例如,6 / 2.0将产生3.0,因为将6的第一个值转换为6.0,然后执行算术以产生3.0。

让我们通过一个例子来理解。

Evaluation of expression Description of each operation
6*2/( 2+1 * 2/3 +6) +8 * (8/4) An expression is given.
6*2/(2+2/3 + 6) + 8 * (8/4) 2 is multiplied by 1, giving value 2.
6*2/(2+0+6) + 8 * (8/4) 2 is divided by 3, giving value 0.
6*2/ 8+ 8 * (8/4) 2 is added to 6, giving value 8.
6*2/8 + 8 * 2 8 is divided by 4, giving value 2.
12/8 +8 * 2 6 is multiplied by 2, giving value 12.
1 + 8 * 2 12 is divided by 8, giving value 1.
1 + 16 8 is multiplied by 2, giving value 16.
17 1 is added to 16, giving value 17.

关系表达

  • 关系表达式是用于比较两个操作数的表达式。
  • 这是用于决定是否应采取措施的条件。
  • 在关系表达式中,数字值不能与字符串值进行比较。
  • 关系表达式的结果可以是零或非零值。此处,零值等效于false,非零值等效于true。
Relational Expression Description
x%2 = = 0 This condition is used to check whether the x is an even number or not. The relational expression results in value 1 if x is an even number otherwise results in value 0.
a!=b It is used to check whether a is not equal to b. This relational expression results in 1 if a is not equal to b otherwise 0.
a+b = = x+y It is used to check whether the expression “a+b” is equal to the expression “x+y”.
a>=9 It is used to check whether the value of a is greater than or equal to 9.

让我们看一个简单的例子:

#include 
 int main()
{
    
    int x=4;
    if(x%2==0)
    {
        printf("The number x is even");
    }
    else
    printf("The number x is not even");
    return 0;
}

输出量

逻辑表达式

  • 逻辑表达式是计算零或非零值的表达式。
  • 做出决定是一个复杂的测试条件。

让我们看一些逻辑表达式的例子。

Logical Expressions Description
( x > 4 ) && ( x < 6 ) It is a test condition to check whether the x is greater than 4 and x is less than 6. The result of the condition is true only when both the conditions are true.
x > 10 || y <11 It is a test condition used to check whether x is greater than 10 or y is less than 11. The result of the test condition is true if either of the conditions holds true value.
! ( x > 10 ) && ( y = = 2 ) It is a test condition used to check whether x is not greater than 10 and y is equal to 2. The result of the condition is true if both the conditions are true.

让我们看一个简单的“ &&”运算符。

#include 
int main()
{
    int x = 4;
    int y = 10;
    if ( (x <10) && (y>5))
    {
        printf("Condition is true");
    }
    else
    printf("Condition is false");
    return 0;
}

输出量

让我们看一个简单的“ | |”示例运算符

#include 
int main()
{
    int x = 4;
    int y = 9;
    if ( (x <6) || (y>10))
    {
        printf("Condition is true");
    }
    else
    printf("Condition is false");
    return 0;
}

输出量

条件表达式

  • 条件表达式是如果条件为true则返回1的表达式,否则返回0。
  • 条件运算符也称为三元运算符。

条件运算符的语法

假设exp1,exp2和exp3是三个表达式。

exp1吗? exp2:exp3

上面的表达式是一个基于exp1表达式的值进行评估的条件表达式。如果表达式exp1的条件成立,则最终条件表达式由exp2表示,否则由exp3表示。

让我们通过一个简单的例子来理解。

#include
#include
int main()
{
    int age = 25;
    char status;
    status = (age>22) ? 'M': 'U';
    if(status == 'M')
    printf("Married");
    else
    printf("Unmarried");
    return 0;
}

输出量