Key Excerpt from C language (11 ~ 14)

Source: Internet
Author: User

11. Analysis of logical operators

& | Short Circuit Rule: | calculates from left to right. If a condition is true, the calculation is stopped. The entire expression is true. If all conditions are false, the expression is false.

& Calculate from left to right. When a condition is false, the entire expression is false. If all conditions are true, the expression is true.

! The result of the operator is only 0 and 1. The value is 1 only when the operator is 0. If the operator is not 0, the result is 0.

The three-object operator (? B: C) can be used as the carrier of logical operators.


12. bitwise operators: &, |, ^ ,~, <,>

Bit operators have lower priority than mathematical operators. Therefore, when involved in arithmetic operations, use parentheses to express the order of calculation:

For example: 0x1 <2 + 3, the result of the operation is 0x1 <(2 + 3), not the expected result, it should be written as follows: (0x1 <2) + 3

The exclusive or ^ operator can be used to exchange two variables. Two Identical variables are used to change or are 0, and the variables and 0 are different or equal to their own rules.


13. ++, -- operator

int i = 3;(++i) + (++i) + (++i) = ?

There are two 16 or 18 results, and the compiler has two parsing methods. The first is that the priority in the brackets is the highest, so the calculation in the brackets is completed first, and the result is I = 6.

Then 6 + 6 + 6 = 18, and the other is to add the first two parentheses, And I goes through two ++, = 5 so the final result is 5 + 5 + 6 = 16, such as GCC, G ++.

This is related to the compiler method. Try not to write code like this.

Using the comma operator, you can enable the ++ operator in order without being optimized by the compiler.

int x;int i = 3;x = (++i,i++,i+10);

Greedy reading by the compiler: each character processed by the compiler should contain as many characters as possible.

The compiler reads as many characters as possible from left to right.

When the read character cannot constitute a valid symbol with the read character

Int I = 0; Int J = ++ I ;=== "++ I, therefore, the compiler reports an error because ++ 0 ++ is incorrect.

B = B/* P, because the compiler parses it into: B = B/* P,/* is parsed as a comment, and spaces can be added correctly. B = B/* P

A ++ B ===> A ++ B is valid

To end the greedy method: the use of spaces or Semicolons can tell the compiler not to be greedy, so it is good to use spaces more.

14. Priority and type conversion characters

Error-prone priority:


#include <stdio.h>#include <malloc.h>typedef struct _demo{    int* pInt;    float f;} Demo;int func(int v, int m){    return (v & m != 0);}int main(){       Demo* pD = (Demo*)malloc(sizeof(Demo));    int *p[5];    int *f();    int i = 0;        i = 1, 2;        *pD.f = 0;        free(pD);        return 0;}

An error is reported during compilation. Resolution: according to the priority table, int * P [5], int * F (), I = cannot get the expected results. This method can be clearly described as follows: int * P [5], int * F (). (Note: Make good use of spaces ). The compiler reports an error in * PD. f = 0; because the compiler parses it into * (PD. F)

Implicit type conversion in C language:

#include <stdio.h>int main(){ char c = 'c'; short s = 0; s= c;printf("%d\n",sizeof(s+c));}

Print result: 4

Resolution: Add char and short, and convert them to int by implicit conversion. Four bytes (char is a byte and short is two bytes)


#include <stdio.h>int main(){    int i = -2;    unsigned int j = 1;        if( (i + j) >= 0 )    {        printf("i+j>=0\n");    }    else    {        printf("i+j<0\n");    }        printf("i+j=%d\n", i + j);        return 0;}

Print result: I + j> 0, I + J =-1;

Resolution: Because the int type I and the unsigned int type J are added, the type is automatically upgraded to the unsigned int, So I + J =-1 to the integer number is 0 xffffffff, so it must be greater than 0. When printing printf

% D format. This is an int format, so you can print-1 normally. If % UD is used, a large integer is printed, % x will print the hexadecimal number 0 xffffffff


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.