Dissection C Chapter 2

Source: Internet
Author: User

This article summarizes the discussion of symbol usage.

 

2010-10-20 wcdj

This article is followed:Dissection C Chapter 1_3

Http://blog.csdn.net/delphiwcdj/archive/2010/10/18/5948666.aspx

 

1. annotator
2. linefeed and Escape Character
3. single quotation marks and double quotation marks
4. logical operators
5, bitwise Operator
6. curly brackets
7, ++, -- operator (Common menstruation stickers on csdn)
8. What is the value of expression 2/(-2?
9. Operator priority

[Thinking] Where is the '/' symbol used in the C language? (Can the answer be complete)
There are more than 20 basic symbols (28) in C language. Each symbol may have multiple meanings at the same time, and the combination of these symbols makes the symbols in C language more complex.
The following code is an award for the 1988 International C language Garbled text Contest (ioccc). The author is Ian phillipps (one of the world's top C language programmers ).
The analysis of this Code is as follows:
Http://wenku.baidu.com/view/b246f27931b765ce05081452.html

# Include <stdio. h> <br/> main (T, _, A) char * A; {return! 0 <t? T <3? Main (-79,-13, A + Main (-87,1-_, <br/> main (-86,0, A + 1) + a): 1, T <_? Main (t + 1, _, A): 3, main (-94,-27 + T, A) & t = 2? _ <13? <Br/> main (2, _ + 1, "% S % d/N"): 9: 16: T <0? T <-72? Main (_, T, <br/> "@ n' +, # '/* {} W +/W # cdnr/+, {} r/* de} +, /* {* +,/W {% +,/W # Q # N +,/# {L +,/n {n +,/+ # N +, // #/<br/>; # Q # N +,/+ k #; * +,/'r: 'd * '3 ,} {W + k W 'K: '+} e #'; DQ # 'l/<br/> q # '+ d' K #! /+ K #; q # 'r} ekk #} w'r} ekk {NL] '/#; # Q # n') {) #} W '){) {NL] '/+ # n'; d} RW' I ;#/ <br/>) {NL]! /N {n # '; r {# w'r NC {NL]'/# {L, + 'K {RW 'ik {; [{NL] '/W # Q # n'wk NW'/<br/> IWK {kk {NL]! /W {% 'l # W # 'I;: {NL]'/* {q # 'ld; R'} {nlwb! /* De} 'C/<br/>; {NL '-{} RW]'/+, }##' *} # NC ,', # NW] '/+ KD' + e} +; # 'rdq # W! NR '/')} +} {RL # '{n'') #/<br/>} '+ }##(!! /") <Br/>: T <-50? _ = *? Putchar (31 [a]): Main (-65, _, A + 1): Main (* A = '/') + T, _, A + 1) <br/>: 0 <t? Main (2, 2, "% s"): * A = '/' | main (0, main (-61, * a, <br/> "! Ek; dc I @ BK '(q)-[w] * % N + R3 # l, {}:/nuwloca-O; M. vpbks, fxntdceghiry "), A + 1 );}

The following summarizes some knowledge points that need attention in this part.

1. annotator


[Question 1] Can C language comments appear anywhere in C language code?
The compiler will remove comments, but not simply delete them. Instead, it uses spaces to replace the original comments. Therefore, the following comment [4] is incorrect:
# Include <cstdio> <br/> int main () <br/> {<br/> // [1], OK <br/> int /*... */I; <br/> // [2], OK <br/> char * s = "ABCD // efgh"; <br/> // [3], OK, note that there is no blank after '/' (backslash) character <br/> // is it a/<br/> valid comment? <Br/> // [4], error, because it equals to "in t I;" <br/> In /*... */t I; <br/> return 0; <br/>}

[Note]/*... */annotations in this form cannot be nested. Because/* always matches the nearest.
/* This is/* wrong */*/
[Question 2] Y = x/* P; correct?
Y = x/* P, which indicates the value in the memory pointed to by X divided by P. Will the result be assigned to Y? We tested it on the compiler and found an error.
The reason is: in fact, the compiler regards/* as the beginning of a comment, and treats the content after/* as the comment content until */appears. This expression only indicates that the value of X is assigned to Y, and the content after/* is used as a comment. However, an error is prompted because */is not found. The modification method is as follows:
(1) y = x/* P; // Add at least one space character after the '/' symbol
(2) y = x/(* P); // or add brackets.
After modification, the expression means that X is divided by the value in the memory pointed to by P, and finally the result is assigned to y.
[Note] As long as there is no space between the slash (/) and the Star (*), it will start as a comment.
[Question 3] How can I write excellent comments? (Poor comments will only help you)

2. linefeed and Escape Character


[Note] There cannot be spaces after the backslash (/) (line feed.
In addition to being used as a line break, the backslash can also be used as the start identifier of the escape character.
Broadly speaking, any character in the C language character set can be expressed by escape characters. Out-of-the-box:
/DDD 1 ~ Three octal characters
/Xhh 1 ~ 2-digit hexadecimal number
DDD and HH are both octal and hexadecimal ASCII codes. For example,/102 indicates the letter 'B' (66, Dec.),/134 indicates the backslash,/x0a indicates the line feed,/x0d indicates the carriage return, and so on.

3. single quotation marks and double quotation marks


Note: The memory size varies with the mode.
1, '1', "1"
The first one is an integer constant, which occupies 4B in a 32-bit system.
The second is a character constant, which occupies the size of 1B.
The third is the string constant, which occupies the 2B size (including the string terminator ('/0 ')).
[Note] characters are stored in the memory as ascaii code. Therefore, character constants can be computed with Integer constants or variables. For example, 'A' + 1.

4. logical operators


[Note] | and & are logical operators, | and & are bitwise operators.
[Note] Be careful with "short-circuit rules ".

5, bitwise Operator


The C language supports the following median operations:
& By location
| By bit or
^ Bitwise OR (P ^ q = ~ P * q + p *~ Q)
~ Invert
<Move left
> Right shift
[Note] the XOR or ^ operator can be used to exchange two numbers. (Think about it. How can I exchange two integers? XOR, intermediate variable, first added and then subtracted ,......)
[Question 1] shift left and right
[1] Left shift operator "<" is a binary operator. The function is to remove all the binary bits of the number on the left from the left to the left. the number on the right is the number of bits to be moved, low fill 0.
[2] right shift operator ">" is a binary operator. Its function is to shift all the binary bits on the left to several right, And the number on the right is specified by the number on the left.
NOTE: For the number of symbols, the symbol bit will be moved along with the right shift. When the value is positive, the maximum bit is 0. When the value is negative, the sign bit is 1. Whether the maximum bit is 0 or 1 depends on the requirements of the compilation system. Turbo C and many systems require completing 1.
[Question 2] 0x01 <2 + 3 value? Is the result 7?
// Warning c4554: "<: Check possible errors in operator priority; use parentheses to clarify the priority (vs2008 prompt)
Int ival = 0x01 <2 + 3; // 32
Because "+" has a higher priority than the shift operation.
Think: are the following behaviors correct? (Overflow occurred !)
// Warning c4293: "<": The Shift count is negative or too large, and its behavior is undefined.
Int ival2 = 0x01 <2 + 30; // under vs2008
// Warning c4293: ">": The Shift count is negative or too large, and its behavior is undefined.
Int ival2 = 0x01> 2 + 30; // under vs2008
// Warning c4293: ">": The Shift count is negative or too large, and its behavior is undefined.
Int ival3 = 0x01> 2-3; // under vs2008
Conclusion: The number of digits in the left and right shifts cannot exceed the Data Length and cannot be less than 0. Otherwise, the behavior is undefined.

6. curly brackets


[Question] What is the role of curly braces?
Char A [10] = "ABCD ";
Char B [10] = {"ABCD "};
// Char C [10] {= "ABCD"}; // Error
To put it simply, curly braces are used to package and make the statements into a whole, which is insulated from the outside world.

7, ++, and -- operators (Common menstruation stickers on csdn -_-!)


[Question] What are the following results?
Int I = 3, J;
J = (++ I) + (++ I); // J = 18 (vs2008)

Int I = 3, J;
J = (++ I) + (++ I); // J = 18 (vc6.0)
What is the value of J? 15, 16, 18?
In fact, the C language standards are not specified for this situation.
In vs2008, the calculation process is: I first goes through three auto-increment operations, then changes to 6, and then 3 6 are added to 18.
The calculation process of vc6.0 is: Calculate the sum of the first two I. At this time, I add two times, and the sum of the two I is 10, then add the third self-added I to get 16.

8. What is the value of expression 2/(-2?


# Include <cstdio> <br/> int main () <br/> {<br/> int ival0 = (-2)/2; //-1 <br/> int ival1 = 2/(-2); //-1 <br/> int ival2 = 2% (-2 ); // 0 equals to 4% 2 <br/> int ival3 = (-2) % 2; // 0 equals to-(2% 2) <br/> int ival4 = 3% (-3); // 0 equals to 6% 3 <br/> int ival5 = (-3) % 3; // 0 equals to-(3% 3) <br/> int ival6 = 2% (-3 ); // 2 equals to 5% 3 <br/> int ival7 = (-3) % 2; //-1 equals to-(3% 2) <br/> int ival8 = 5% (-2); // 1 equals to 7% 2 <br/> int ival9 = (-2) % 5; //-2 equals to-(2% 5) <br/> return 0; <br/>}< br/>

Most compilers require that the remainder be the same as the plus and minus signs of the divisor.

9. Operator priority


[Error-prone priority problems]
[1]. The priority is higher than *, and * P. F is equivalent to * (P. F)
[2] [] is higher than *. int * AP [] is equivalent to int * (AP []) and is a pointer array.
[3] A function () is greater than *, and int * FP () is equivalent to int * (FP ().
[4] = and! = Operation above bits, (Val & Mask! = 0) equivalent to Val & (mask! = 0)
[5] = and! = Higher than the value assignment, c = getchar ()! = EOF is equivalent to C = (getchar ()! = EOF)
[6] Arithmetic Operators are higher than displacement operators, and MSB <4 + LSB is equivalent to MSB <(4 + LSB)
[7] The comma operator has the lowest priority among all operators, I = 1, 2 is equivalent to (I = 1), 2

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.