[Day 19 of C Language Inspector Training Camp] Some Supplements on C Language Grammar

1. Conditional operator and comma operator

The conditional operator is the only ternary operator in C language . A ternary operator means that there are three operands; a binary operator means that there are two operands, such as a logical AND operator is a binary operator; a unary operator means that there is one operand, such as a logical NOT operator is a unary operator. Operators are also called operators. The ternary operator determines the value of the overall expression by judging whether the expression before the question mark is true or false. As shown in the following example, if a>b is true, then the overall value of the ternary expression is a, so the value of max is equal to a. If a>b is false, then the overall value of the ternary expression is b, so the value of max is equal to b.

#include <stdio.h>
int main() {
    
    
int a,b,max;
	while(scanf("%d%d",&a,&b))
	{
    
    
		max=a>b?a:b;
		//a和b中较大的那个数是
		printf(""max=%d\n",max);
	}
return 0;
}

The comma operator has the lowest priority. What we need to grasp is that the overall value of the comma expression is the value of the last expression. Please see the example below

#include <stdio.h>
int main(){
    
    
int i,j;
i=10;
j=1;
	if(i,j-1)//并不会进入f,逗号表达式整体的值是最后一个表达式的值
	{
    
    
		printf("If ecute\n");
	}
	//逗号表达式的常见使用场景,for 的表达式1初始化多个变量用的较多
	for(i=O,j=1;i<10;i++)
	{
    
    
	}
return 0;
}

2. Increment and decrement operators

There is a big difference between the self-increment and self-decrement operators and other operators, because other operators, except assignment operators, can change the value of the variable itself, which will not have this effect. Self-increment and self-decrement are the operations of adding 1 and subtracting 1 to the variable itself, so why invent such operators when there are addition and subtraction operators? The reason is that self-increment and self-decrement come from B language. At that time, Ken Thompson and Dennis M.Ritchie (the inventor of C language) retained the self-increment and self-decrement in B language in C language in order not to change the programming habits of programmers. Because self-increment and self-decrement will change the value of the variable, so self-increment and self-decrement cannot be used for constants!
The following is a program about self-increment. The self-increment and value operators are not tested in the 408 initial test, and some schools may test their own questions. It is
insert image description here
worth noting that the pre-++ and post-++ are both ++, but the implementation effect is completely different, because the underlying implementation principles of the two are different. The two are overloaded below, and it can be seen that the effect after overloading is the same as ++i, i++.

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class A{
    
    
    int n;
    public:
    A(int n):n(n){
    
    }
    int get();
    friend A& operator++(A &a);
    friend A operator++(A &a,int b);
};
A& operator++(A & a)
{
    
    
     a.n++;
     return  a;
}
A   operator++(A&a,int b)
{
    
    
    return   A(a.n++);
}
int A::get()
{
    
    
    return n;
}
int main()
{
    
    
     A a=5;
     (++(++a))++;
     cout<<a.get()<<endl;
     A b=a++;
     cout<<b.get()<<" "<<a.get()<<endl;
     system("pause");
     return 0;
}

3. Bit operations

The bitwise operators <<, >>, ~, I, ^, & are sequentially left shift, right shift, bitwise inverse, bitwise or, bitwise exclusive or, bitwise and.

位运算符只能用于对整型数据进行操作:

  • Left shift: Discard high bits, add 0 to low bits, which is equivalent to multiplying by 2 . Many times in work, you will use left shift when applying for memory. For example, if you want to apply for 1GB of space, you can use malloc(1<<30).
  • Right shift: discard the low bits, complement the high bits of positive numbers (we consider unsigned numbers to be positive numbers), complement the high bits of negative numbers with 1, which is equivalent to dividing by 2 . Shifting is more efficient than multiplication and division. Negative numbers are shifted to the right. For even numbers, it is divided by 2, but for odd numbers, it is first subtracted by 1 and then divided by 2. For example, -8>>1 gets -4, but -7>>1 gets -4 instead of -3. In addition, for -1, no matter how many bits are shifted to the right, the value will always be -1.
  • XOR: When the same number is XORed, the result is 0, and the result of any number and О XOR is itself.
  • Bitwise inversion: the number on the digit is 1 to 0, and 0 to 1.
  • Bitwise AND and Bitwise OR: Perform AND and OR with each bit of two numbers.

The left shift and right shift mentioned here all refer to arithmetic shift. There is also a logical shift, where the logical shift is shifted left and right, and all vacant parts are filled with 0.

#include <stdio.h>
//位运算符
int main() {
    
    
    short i = 5;//short是整型,是2个字节的整型,int 是4个字节
    short j;
    j=i << 1;//一个变量移动以后自身不会变化
    printf("j=%d\n", j);//左移是乘2,结果为10
//    i=-8;
    j = i >> 1;
    printf("j=%d\n", j);//右移是除2,结果是2
    printf("-----------------\n");
    i = 0x8011;
    unsigned short s = 0x8011;//在short前加unsigned
    unsigned short r = 0;
    j = i >> 1;//对i右移,对有符号数进行右移
    r = s >> 1;//对s右移,对无符号数进行右移
    printf("j=%d,r=%u\n", j, r);//结果是不一样的
    printf("-----------------\n");
    //接下来来看  按位与,按位或,按位异或,按位取反
    i = 5, j = 7;
    printf("i & j=%d\n", i & j);
    printf("i | j=%d\n", i | j);
    printf("i ^ j=%d\n", i ^ j);
    printf("~i=%d\n", ~i);
    return 0;
}

The XOR operator has two characteristics. One is that any number and zero XOR will get itself, and two equal numbers will get zero. Through these two characteristics, we can complete the following topic, find the number that appears once in a bunch of numbers, and the function of the following code is to filter out the numbers that only appear once.

#include <stdio.h>

int main() {
    
    
    int i;
    int arr[5] = {
    
     8 ,5, 3, 5, 8 };
    int result = 0;
    for(i=0;i<5;i++)
    {
    
    
        result=result^arr[i];//异或满足交换律
    }
    printf("%d\n", result);//输出为3
    return 0;
}

4. switch do-while supplement

The following two are for understanding (in fact, I feel that the second-level pointer is very easy to use, the pointer is really a good thing!!!)
如果一个case语句后面没有break语句、那么程序会继续匹配下面的case常量表达式.
insert image description here

#include <stdio.h>
//switch 日期升级
int main()
{
    
    
    int mon,year;
    while(scanf("%d%d",&year,&mon))
    {
    
    
        switch (mon)
        {
    
    
            case 2:printf("mon=%d is %d days\n",mon,28+(year%4==0&&year%100!=0||
                                                        year%400==0));break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:printf("mon=%d is 31days\n",mon);break;
            case 4:
            case 6:
            case 9:
            case 11:printf("mon=%d is 30days\n",mon);break;
            default:
                printf("error mon\n");
        }
    }
    return 0;
}

insert image description here

#include <stdio.h>

int main() {
    
    
    int i=1,total=0;
    do{
    
    
        total+=i;
        i++;
    }while(i<=100);//必须有分号,否则编译不通
    printf("total=%d\n",total);
    return 0;
}

5. Two-dimensional array & two-level pointer

insert image description here
insert image description here
insert image description here

6. Summary

At this point, the grammar involved in the C language is almost over. Although I learned several programming languages ​​and wrote some codes when I was a freshman and sophomore, I still insisted on watching Wang Dao’s C language training camp. I feel that the C language knowledge points explained in the training camp are not so comprehensive, but this also let me know what content is likely to be tested in the 408 initial exam, where I should focus on, and where I should concentrate my time. There are still 4 days left to end the learning tasks of the inspector camp stage, and the next step is to start the 408 data structure rush! ! !


insert image description here

Guess you like

Origin blog.csdn.net/apple_51931783/article/details/129268494