Summary of symbols in C language

Source: Internet
Author: User
Tags bitwise bitwise operators logical operators

1. Comments///And/* ... */

2, continue the line symbol \

3, Escape symbols commonly used: \ R and \ n and so on

4. one character enclosed by single quotation marks represents integers.

5, double quotes " enclosed characters represent a pointer .

The single quotation mark ' enclosed character represents an integer, and the double quotation mark ' encloses a pointer. So while the C compiler accepts comparisons between characters and strings, it makes sense to be wrong, and the C compiler allows strings to be assigned to character variables, meaning it's ridiculous.

6. Logical operator:&& | | !

(1) logic with && and logic or | | Short-circuit rules in the program:

&& starts from left to right, and when it encounters a false condition, stops the calculation, the entire expression is false, and all the conditions are true when the expression is true.

|| Starting from left to right, when a condition is encountered that is true, the entire expression is true and the expression is false when all conditions are false.

  The classic case, as shown below, prints the result as 1,0 . If you change the sixth line to the IF (++i>0 && ++j>0) print result 1, 1.

1#include <stdio.h>2 intMain ()3 {4     inti =0;5     intj =0;6     if(++i >0|| ++j >0 )         7     {8printf"%d\n", i); 9printf"%d\n", j);Ten     } One     return 0; A}

(2) Logical non-

! 0 = 1! 1 = 0! 100 = 0! -1000 = 0

7, trinocular operator (A?B:C)

(1) Three mesh operators can be considered as vectors of logical operators

(2) Rule: When A is true, the value of B is returned, otherwise, the value of C is returned.

8, bitwise operators & | ^ ~ << >>

bitwise AND, bitwise OR, bitwise XOR, bitwise negation, left shift, right shift are the symbols commonly used in bitwise operations.

Shift left: High drop, low 0.

Shift Right: Low drop, high fill sign bit. (Note that the high level is not a complement 0)

1 voidTestvoid)2 {3     inta=-8;4A=a>>3;5printf"%d\n", a);//results: -16 }7 voidTest1 (void)8 {9     intA=8;TenA=a>>3; Oneprintf"%d\n", a);//Results: 1 A}

Tip: Moving the n -bit to the left is equivalent to multiplying by 2 , but with higher efficiency than the mathematical operator. the right shift N is equal to the n - squared by 2 , but the efficiency is higher than the mathematical operator.

XOR: Different or a very powerful symbol, the position of the previous exchange of two numbers is seen. In addition, the different or satisfies the Exchange law and the binding law, the concrete example see as below question.

Interview questions: There is a series, where the number of natural numbers are in the form of even, only a natural number of occurrences of the odd number of times, the program to find this natural number.

Algorithm 1: Iterate through the array and find the singular. This approach is the easiest to consider, but it takes time and space.

Algorithm 2: According to the different or satisfies the Exchange law and the Union law, uses the different or the method, both saves the time, but also saves the space. The code is as follows, and the macro DIM (a) is used to calculate the length of the array, where sizeof (a) represents the number of bytes in a array, and sizeof (*A) is the equivalent of a[0] bytes.

1#include <stdio.h>2 #defineDIM (a) (sizeof (a)/sizeof (*a))3 intMain ()4 {    5     intStr[] = {2,3,5,7,2,2,2,5,3,7,1,1,1};6     intFind =0;7     intI=0;8      for(i=0; I<dim (str); i++)    9     {TenFind = find^Str[i];  One     } Aprintf"%d\n", find); -     return 0; -}

9, + +--

Expression + + and--the reading skill, greedy method: From left to right as much as possible contain characters. Because the compiler is to read as many characters as possible from left-to-right in the order of one, until the character that is about to be read is not possible and the characters that have been read are made into a legal symbol.

1#include <stdio.h>2 voidTestvoid);3 voidTest2 (void);4 voidTest3 (void);5 voidTest4 (void);//Supplemental: comma operator6 intMain ()7 {   8 test ();9     return 0;Ten } One  A /************************************************************************ - the gray area of the C language, determined by the compiler, is 6+6+6=18 if the parentheses are first calculated; - if the previous two times Plus, in + (++i), the result is 16--this compiler (dev-c++) This is the way to get the ************************************************************************/ - voidTestvoid) - { -     intI=3, J; +J= (++i) + (++i) + (+ +)i); -printf"%d\n", j);//The result is + } A  at #if0 - //j= in Test (++i) + (++i) + (++i), parentheses out analysis--error - voidTest2 (void) - { -     intI=0, J; -j = ++i+++i+++i;  in     //Analysis-greedy method: From left to right as much as possible contains characters: The preceding part is equivalent to: (++i) + + is equivalent to 1++, a value + +, will be error.  -     //with j=++i++, and 1++; The result of compilation error is the same, so this understanding is correct.  to  +printf"%d\n", J);  - } the #endif  *  $ voidTest3 (void)Panax Notoginseng { -     intA=1; the     intb=1; +     intC; AC=a+++b;//Greedy Method theprintf"%d\n", a);//result is 2 +printf"%d\n", c);//result is 2 - } $  $ //The comma-operator rule evaluates from left to right, and the value of the last expression is the value of the comma expression.  - voidTest4 (void) - { the     intx; -     intI=3;WuyiX= (++i,i++,i+Ten); theprintf"%d\n", x);//The result is -}

Summary of symbols in C language

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.