This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021″
Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!
~ bitwise inverse operator
Invert the binary sequenceCopy the code
Such as:
int main(a)
{
int a = 1;
int b = 0;
b = ~a;
printf("%d\n",b); / / - 2
}
//a : 00000000 00000000 00000000 00000001
~a: 11111111 11111111 11111111 11111110- The complement symbol bit is1, negative numbers are converted to source code, %d prints the source code to complement the complement is the source code11111111 11111111 11111111 11111110- complement10000000 00000000 00000000 00000001- The symbol bit of the reverse code remains unchanged10000000 00000000 00000000 00000010- radix-minus-one complement +1- > value is2 -
Copy the code
6. Programming question: How do I reverse a digit of a binary sequence that is 0 by 1
Or operation: a bit or 0, the corresponding bit is not reversed.
One bit or one, if that bit is one, it’s not flipped. If it’s 0, flip it
Method: flip a binary number to 1, or the other digit is 0, only the binary number is 1
// Invert a bit of 0
int main(a)
{
int a = 0;
int n = 0;
scanf("%d",&n); // the number of digits flipped
int b = a | (1 << (n- 1));
printf("%d corresponds to the binary sequence %d bit inverted result %d\n",a,n,b);
return 0;
}
Copy the code
Want to n a binary 1 only require bitwise or (|) on 1 < < n – 1 can be (1 left n – 1)
7. How do I flip a 1 in a binary sequence by 0
Method: 1 moves n-1 bits to the left and then reverses the number
int main(a)
{
int a = 15; / / 0000 1111
// Flip the third (NTH) bit to 0
a &= ~(1<<2);
printf("%d\n",a);/ / 11, 0000, 1011
return 0;
}
Copy the code
Take 8bit, for example
1 Move 0 bits to the left: 0000 0001
1 Move 1 bit to the left: 0000 0010
int main(a)
{
int a =13; / / 0000 1101
a |=(1<<1); / / is equivalent to a = a | (1 < < 1) even if a second binary sequence flip to 1
printf("%d\n",a);//0000 1111 -15
/ / recovery
a &=(~(1<<1)); //a = a&(~(1<<1)) even if the second part of a's binary sequence is flipped to 0
printf("%d\n",a);/ / 0000 1101-13
return 0;
}
Copy the code
&& | | or logic and logic
In C language, 0 means false and non-0 means true
&& Short circuit phenomenon:
A &&b &&c The result is true only if A,B, and C are both true.0), terminates the post-order calculationCopy the code
int main(a)
{
int a =0;
int b = 1;
int c = 1;
int d = a++ && b++ && c++;
printf("%d %d %d %d\n",a,b,c,d);/ / 1 1 1 0
A ++ = 1; // if a++ = 1; // if a++ = 1; // if a++ = 1
return 0;
}
Copy the code
| | short-circuit phenomenon:
A | B | | | C, A, B, C as long as there is A true, the result is true for A, B, C, there is A true (non0True), terminates the post-order calculationCopy the code
int main(a)
{
int a = 0;
int b = 1;
int c = 1;
int d = a++ || b++ || c++;
printf("%d %d %d %d\n", a, b, c, d);/ / 1 1 1 2
/// cause: a++ becomes 1, b++ becomes 2, a++ becomes 1, b++ becomes 2
return 0;
}
Copy the code
Unique ternary operator? :
P? A:B ; If P is true, execute A; otherwise, execute B. For example, C= A > B? A:B; Find the maximum value of two numbers if A>B is true. C = A, otherwise C = BCopy the code
Comma expression
The result of a comma expression is the last expression
int main(a)
{
int a = 1;
int b = 3;
int c = 0;
// c =a++, b++, a + 1;
//printf("%d %d %d\n",a,b, c); // 2, 4, 1 is not 2, 4, 3
// reason: the assignment expression = has a higher priority than the comma expression, so it is equivalent to c=a++, and then the following expression
/ / positive solutions
c = (a++, b++, a + 1);
printf("%d %d %d\n",a,b, c); / / 2 3 4
return 0;
}
Copy the code
a = get_val();
count_vao(a);
while(a>0)
{
/ / processing
a = get_val;
count_val(a);
}
Copy the code
The above expression can also be expressed with a comma
while(a = get_val(),count_val(a),a > 0)
{
/ / processing
}
Copy the code
The [] operator
int main(a)
{
int arr[] = {1.2.3.4.5};
int i = 0;
for(i = 0; i <5; i ++) {printf("%p----%p\n",&arr[i],arr+i);
}
return 0;
}
Copy the code
The array name is the address of the first element, so arr+ I: the address of the element with the subscript I
arr[4] : [] is the operator, arr and4Is its two operands arr[4] == * (arr+4Addition supports commutative laws -> * (4+ arr) - >4[arr]
arr[4] and * (arr +4) for so *(4+ arr) - > about4[arr]
Copy the code
Again, [] is the operator arr and 4 are just operands
() : function call operator
Such as:
Strlen () returns an unsigned integer
// /0 does not count the length of the string
printf("%d\n".strlen("abc"));/ / 3
printf("%u\n".strlen("abc"));/ / 3
Copy the code
For the function call operator ()
Receives one or more operands. The first operand is the name of the function and the remaining operands are the arguments passed to the function
For example, my_Add(a,b) () has three operands: my_Add(function name) a,b
Structure member access operator ->.
Creating a structure type itself does not take up memory space; space is created when creating a structure variable
struct Stu
{
int age; // Struct member
char name[20]; // Struct member
}s1,s2;
struct Stu s3;
//struct Stu is a struct type
// s1,s2 is the global structure variable
// s3 is a struct local variable
Copy the code
struct Stu
{
int age;
char name[20];
};
int main(a)
{
struct Stu s[3] ={{20."zhangsan"} ,{30."lemon"} ,{10."shuai"}};int i = 0;
// Method 1: struct variable. access
for (i = 0; i < 3; i++)
{
printf("%d %s\n", s[i].age, s[i].name);
}
// Method 2: access the structure pointer with ->
struct Stu* p = &s[0];
printf("%d %s\n", p->age,p->name);
// Method 3: dereference struct access
printf("%d %s\n", (*p).age, (*p).name);
return 0;
}
Copy the code
Matters needing attention:
You cannot put strings directly into an array inside a structure. Use the strcpy function
#include<string.h>
int main(a)
{
struct Stu
{
int age;
char name[20];
};
struct Stu s;
s.age = 10;
//s.name = "mango"; //err, name is the name of the array, is the constant address, should change the string to the space pointed to by name
String copy function strcpy()
strcpy(s.name, "Data structure");
printf("%d %s\n", s.age, s.name);
}
Copy the code
Summary: Structure member access operator methods1: structure variable. Member name method2Structure pointer -> member name method3: (* structure pointer). Member nameCopy the code
6. Plastic lifting
When the evaluated operands are of type short or char, an integer promotion occurs to int
7. Arithmetic transformation
A variable of type greater than 4 bytes is evaluated, and the arithmetic change occurs, converting to the higher byte type, resulting in the type with the largest byte of the evaluated variable type.
Such as:
int a = 0;
float b = 0.0 f; //C's decimal defaults to double. If you want a float, add f to the initial value
double c = 0.0; d = a+b+c; Q: What type of D is? The result is the largest type of byte:double 8byte
int 4 byte
float 4bYte, so d isdoubletypeCopy the code
That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!