catalogue
6. Subscript references, function calls, and structure members
1. Monocular operator
The ternary operator (?:) known before refers to having three operands
For example: 3 + 5
Where + is an operator
3 is the left operand
5 is an operand
+Is a binocular operator
So what is a unary operator, that is, one with only one operand
Our common operators are:
Here, let's introduce sizeof, ~, + + and--
1.sizeof
Sizeof is often used to calculate the length of types, such as array types, int, char, short, etc. Meanwhile, sizeof is an operator, not a function, so the following parentheses can be omitted. But you can't save when calculating the type length, which is a syntax requirement.
Here are some common uses of sizeof
#include<stdio.h> int main() { //sizeof int a = 10; printf("%d\n", sizeof(a)); printf("%d\n", sizeof(int)); printf("%d\n", sizeof(float)); printf("%d\n", sizeof(double)); int arr[10] = { 1,2,3,4,5,6,7 }; printf("%d\n", sizeof(arr)); printf("%d\n", sizeof(int[10]));//Calculates the length of the entire array printf("%d\n", sizeof a); //printf("%d\n", sizeof int); Writing like this will report an error return 0; }
The calculated results are as follows:
2.~
~The main function of is to reverse the binary bit of a number, but similarly, the original code is printed out at last
#include<stdio.h> int main() { int a = 0; //~Reverse by (binary complement in memory) //00000000000000000000000000000000 //11111111111111111111111111111111111111 - complement //11111111111111111111111111111111111110 - inverse code //1000000000000001 - original code -- > - 1 printf("%d\n", ~a); return 0; }
The operation results are as follows:
Here is a ~ application:
How to restore a number
#include<stdio.h> int main() { int a = 10; a |= (1 << 2); printf("%d\n", a); a &= ~(1 << 2); printf("%d\n", a); return 0; //If you only want to change the penultimate number, that is, 0 to 1, you can do the following //00000000000000000000000000001010 //000000000000000000000000000 1 < < 2 (realized by a and 1 < < 2 XOR) //00000000000000000000000000001110 //11111111111111111111111111111111011 ~ (1 < < 2) (to restore a, change the penultimate number to 0) //00000000000000000000000000001010 }
3. + + and--
The main attention here is divided into pre and post
Pre + + / -: Add / subtract before use
Post + + / -: use first and then add / subtract
a it will be recorded every time it is added. It has been added twice in total, so it is 3.
For b, the value of a is used first, and then + +, so it is 1
To c, a has actually become 2, and c first adds 1 to a and then its value, so c is 3.
4. * (dereference operator)
#include<stdio.h> int main() { int a = 10; int* pa = &a; *pa = 20;//Access a's address through * printf("%d\n", a); printf("%d\n", *pa);//At this point, * pa is the value of a int* px = &*pa;//px accesses * pa through *, that is, a *px = 30; printf("%d\n", a); return 0; }
2. Relational operators
3. Logical operators
(only focus on the true and false variables)
Look at a 360 written test question and judge the result of the program output.
Let's first look at the first one. A is used first and then used + +. Here a is 0, and if it is 0, it is false. Then don't look at the later ones. It's still the original result, but a is changed++
Let's look at the second. For example, I want Zhang San or Li Si to come to my office. If Zhang San comes, it doesn't matter if Li Si doesn't come. The same is true here. A uses + + first and then + +, then a=0, which means that no one comes and then continues. b uses + + first and then, b=3. It's true, that is, when someone comes, it doesn't matter whether the latter comes or not Therefore, d will not proceed, and the final output is 1334
(to make a brief summary: & & --- the left operand is false and the right operand is not calculated ||--- (the left operand is true and the right operand is not calculated)
4. Conditional operator
(also called ternary operator -- > Exp1? Exp2: EXP3)
#include<stdio.h> int main() { int a = 10; int b = 20; int max = 0; /*if (a > b) max = a; else max = b;*/ max = (a > b ? a : b);//Equivalent to the code above return 0; }
5. Comma expression
int a = 1; int b = 2; int c = (a > b, a = b + 10, a, b = a + 1);
Let's look at the formula in parentheses. A > b and a have no effect on a and B, which can be ignored directly. Then a=b+10, where a becomes 12, and finally b=a+1=13. So the result is 13
It is not difficult to find that the result of the last expression works at last.
6. Subscript references, function calls, and structure members
1. [ ] Subscript reference operator
Operand: an array name + an index value
2. () function call operator
#include<stdio.h> void menu() { printf("*************************\n"); printf("****** hello ******\n"); printf("****** friends ******\n"); printf("*************************\n"); } int main() { menu(); return 0; }
3. Visit members of a structure
. structure. Member name
->Structure pointer - > member name
#include<stdio.h> struct Stu { char name[10]; int age; double score; }; int main() { struct Stu s = { "zhangsan",20,85.5 }; //. printf("%s %d %.1lf\n", s.name, s.age, s.score); //-> struct Stu* ps = &s; printf("%s %d %.1lf\n", (*ps).name, (*ps).age, (*ps).score); printf("%s %d %.1lf\n", ps->name, ps->age, ps->score); return 0; }
The output results of these three cases are the same. Interested students can try it by themselves!
7. Implicit type conversion
For example:
#include<stdio.h> int main() { short s = 20; int a = 5; printf("%d\n", sizeof(s = a + 4)); printf("%d\n", s); return 0; }
The output of sizeof is 2, in which a of int type is converted to short type.
8. Operator properties
When calculating, because * has higher priority than +, it can only be guaranteed that * is calculated earlier than + but the priority is not