C Pitfalls and Pitfalls Chapter I.

Source: Internet
Author: User

1. Use the E1=e2 assignment method as the internal judgment of the conditional statement, use the judging of the displayDo not use:
if (x =y)     foo ();

Instead, use:

if 0 )     foo ();

2. Note The coding specification, must be on the assignment number "=" on both sides, write the above grid, to avoid the following unconscious error.

Originally wanted to write:
A =-1;

But there are no spaces to add:

a=-1;

This is translated by the compiler: (This issupposed to be an old compiler with this explanation, and now it's all using the "+ =" and "=" operators)

1;

3. Representation of the binary

intA; a=141;/*decimal*/printf ("a:%d \ r \ n", a); a=0215;/*octal, starting with the number "0"*/printf ("a:%d \ r \ n", a); a=0x8d;/*hexadecimal*/printf ("a:%d \ r \ n", a);

Now 0195 "The representation of the method will be error by the compiler
0195;   /* octal */

4. Characters and strings

The first thing to note is that for string constants, it is stored in the constant area, read-only data area. Rodata inside. (recommend an article about variable storage location http://blog.163.com/[email protected]/blog/static/404330272007102012451957/) and, for the same string, there is only one copy, when looking for a string, will first go to the read-only data area to find the storage address with or without the corresponding string, if so, the direct output ; no new string will be created:
Char* str ="test string address.";Char* STR1 ="test string address."; cout<int) (str) <<endl;/*output in 16-in unsigned form*/cout<< (int) (STR1) <<Endl;cout<< (int)("test string address.") <<Endl;; printf ("ox%x \ r \ n", str);/*output in 16-in unsigned form*/printf ("ox%x \ r \ n", str1);p rintf ("ox%x \ r \ n","test string address.");

Output "test string address" in C and C + + output streams, respectively. Address, found to be the same.

For the printf () function, the internal pass-through is the address of the string, so be clear character(' n ' \ R ' ... In single quotes, the value is passed, and the character is equivalent to an integer, corresponding to its ASCII code) and string("n", "\ r" ...) With double quotes, pass the address, this example from the above, directly"test string address."to the printf () function can be seen) the difference
printf ('\ n'  );/* ERROR */printf ("\ n" );

5. Assigning a value to int by character                
int'yes' ;    

                     

cout << (char ) a<<<< (char ) (a>>8) <<< < (char ) (a>>) <<<< (char ) (a>>)< < Endl;    

If the character is less than 4, then the high is 0;                
cout << (char ) a<<<< (char ) (a>>8) <<< < (char ) (a>>) <<<< (int ) (a>>)<< Endl

If you have too many characters, you will get an error.
int'abcde'

C Pitfalls and Pitfalls Chapter I.

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.