Home  >  Article  >  Backend Development  >  What are the six basic statements in C language?

What are the six basic statements in C language?

coldplay.xixi
coldplay.xixiOriginal
2020-07-06 10:47:3516123browse

The six basic statements of the C language are: 1. Arithmetic operators and arithmetic expressions; 2. Assignment operators and assignment expressions; 3. Relational operators and relational expressions; 4. Logical operators and Logical expressions; 5. Conditional operators and conditional expressions; 6. Comma operators and comma expressions.

What are the six basic statements in C language?

The six basic statements of C language are:

1. Arithmetic operators and arithmetic expressions

1. Basic arithmetic operators

(addition), - (subtraction), * (Multiplication), / (Division) and % (Remainder)

The precedence of these five operators is:

        *, / and % are at the same level, but higher than -. That is, multiply and divide first and then add and subtract.

When two integers are divided, the result is an integer; if the numerator is less than the denominator, the result is zero.

For example: 5/2 The result is 2

2/5 The result is 0

The five operators Associativity is: from left to right.

For example: 10 6- 4*2

In the first step, calculate 10 6 and get the result 16. In the second step, calculate 4*2 and get the result 8. Then use the first step to calculate Subtract the result of the second step calculation from the result to get the result 8.

Remainder calculation method:

        5%3   The remainder is 2

        5%8   The remainder is 5

                                                                                                                                                          # symbol)

2. Arithmetic expression

Arithmetic expression is an expression composed of arithmetic operators and operands. The value of an expression is a numeric value, and the type of the expression is determined by the operator and operands.

For example: 5 3*(6-2) The type of expression is int type.

          3 4.0-3/2.0 The type of expression is double.

3. Data type conversion

Usually the data types involved in the operation are not completely consistent. During operation, they should be converted into the same data type first and then operated.

2.1 Forced type conversion, directly force conversion of certain data into the specified data type

Forced type conversion, the variable value itself does not change

           (double)a;  
       (int)(x+y);注意区别:(int)x+y 先把x的值转换成int型,然后再加y
       (float)(5%3)    
        int  i;
        …
        i=i+(int)9.801;

2.2 During compilation, the compiler will automatically complete it according to certain rules without human intervention

The following conversions must be performed before the data participates in the operation:

char,short→int→ float→double

During mixed operation, the data type changes from low-level to high-level

unsigned → int → long → double

and above It does not mean that unsigned must be converted to int and then to long in sequence, but the conversion is performed by the highest level in the calculation.

2. Assignment operator and assignment expression

1. Assignment operator

Assignment symbol:

=

Assign the value of the expression on the right side of the assignment operator to a variable on the left side of the assignment operator.

If the data types of the assignment operator are inconsistent, type conversion is required. The conversion method is:

When assigning real data to an integer variable, discard the decimal part of the real number.

For example: int i; i=5.65; The value of i is 5.

When assigning integer data to a real variable, the value remains unchanged.

 float f  ;  f=23;

(First 23→23.00000 and then store it in f)

  double  d;  d=23;

(First 23→23.000000000000000 and then store it in d)


Change the characters When assigning data to an integer variable, put the character data into the lower 8 bits of the integer variable

          int i;char ch = '0';        i = ch;   // i = 48

When assigning the integer variable to character data, put the lower 8 bits of the integer variable into the character data

        int i = 4656;char ch;        ch = i;   // ch = 48

2. Compound assignment operator

Compound assignment operator: (ten in total)

 

= , -= , *= , /= , %= ,

59b6c0ac84b5cbca7160a4b88268d9d0>= , &= , ^= , |=

For example: ​

                             a += 3        等价于     a=a+3
                    x *= y+8    等价于     x=x*(y+8)
                    x %= 3       等价于     x=x%3

An expression that connects a variable and an expression by the assignment operator.

The format is: 68187049abec0c7dd53c11bf3b729d87 8438f980f7b296cf2e02ee9c8ef05b1c ffbeece48539e6983ff3249db04f27c9

For example:

int x,y,z;
      x=y=z=5+6;
 
              int  x=3,y=4;
              x*=y+1;    
              ( x=等价与  x*(y+1);)
 
             int x;
             x='a';

Assignment operation The combination rate is: "from right to left".

For example:

                         a=b=c=5       a=(b=(c=5))   a,b,c值都是5
            a=5+(c=6)      c值为6, a值为5+6 (即11)
            a=(b=4)+(c=6)    b值为4,c值为6,a值为4+6 (即10)
            a=(b=10)/(c=2)    a值为5

三、关系运算符和关系表达式

  1.关系运算符

   关系运算符用于两个数值之间的比较运算。C语言提供6种关系运算符:

d2714fbb0e49a95306c2048bc19e4f2b、>=、  优先级相同高

==、!=  优先级相同低

 

关系运算符的结合率为:“自左而右”。

(即当优先级相同时按自左而右结合a>b>c,当优先级不同时按优先级高低结合a=b+c)

关系运算符、算术运算符和赋值运算符的优先级为:

 

  例如:  1、c>a+b          等效于     c>(a+b)

  2.关系表达式

   由关系运算符和操作数组成的表达式称为关系表达式。

关系表达式的值是一个逻辑型的值,即只有两个值(真和假)。

C语言是用1作为真,用0作为假。但是进行运算时,非0即认为真,0才认为假。而表达式的结果为真时,给出真值1。为假时,给出假值0。

 例1:

有int x=2,y=3,z=5;
则:x>y     结果为0。
z>=y    结果为1。 
z==y    结果为0。

例2: 

若  a=3,b=2,c=1
     f=a>b>c 
    f=?

四、逻辑运算符和逻辑表达式

  1.逻辑运算符

   

  2.逻辑表达式

用逻辑运算符将关系表达式或逻辑量连接起来的式子。

运算结果为:“真”或“假”值。

系统在运算时以非0即为真,以0为假。

 

 

例如:  4  && 0 || 2   的值为1

                 5  && !0     的值为1

 

例如:

                   (a>b)&&(x>y)           可以写成  a>b&&x>y
              (a==b)||(x==y)          可以写成    a==b||x==y
              (!a)||(a>b)             可以写成   !a||a>b
              5>3&&2||8<4-!0          的值为     1
              &#39;c&#39;&&&#39;d&#39;              的值为      1

 

#include <stdio.h>
int main()
{
    int a = 10;
    int b = 10;
    if (++a > 100 || ++b >0)
    {
    }
    printf("a = %d, b = %d\n", a, b); 
    a = 10;
    b = 10;
    if (++a > 100 && ++b >0)
    {
    }
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

五、条件运算符和条件表达式

  1.条件运算符

   条件运算符:  ? : 它是唯一的一个三目运算符

  2.条件表达式

    条件表达式的一般格式为:表达式1  ?  表达式2  :    表达式3       

运算过程:表达式1的结果为真(非0)时,表达式2的计算结果作为条件表达式的值;否则,取表达式3的计算结果为条件表达式的值。

 

如: a>b?a:b  

 

条件运算符的优先级低于逻辑、关系、算术运算符高于赋值运算符。

如: a>b?a:b+1     相当于     (a>b)?a:(b+1)  

六、逗号运算符和逗号表达式

逗号运算符:  ,

格式: 表达式1, 表达式2, 表达式3, LL, 表达式n

优先级: 最低

从左向右计算每个表达式的值,逗号表达式的值为表达式n的值。

例如:

                     y=(x=3,5+6,x+5) 逗号表达式的值为?

相关学习推荐:C视频教程

The above is the detailed content of What are the six basic statements in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn