The Pit through C/

Source: Internet
Author: User
Tags integer division

1. Signed int vs. unsigned int

#define Totol_elements (sizeof (a)/sizeof (A[0)), int main () {int a[] = {23,24,34};int d = -1;if (d<=totol_elements) prin TF ("true\n"); elseprintf ("flase\n"); return 0;} The result is flase and not true

Result analysis: sizeof () returns an unsigned integer, so the value totol_elements in the above code is the unsigned int type. The signed int is converted to the unsigned int variant, compared to the D in the IF statement for the signed int type.

1 will translate into a very large number---65535

*************************************************************

Original code, anti-code, complement:

Original code: Binary fixed-point notation, that is, the highest bit is the sign bit. 0 is positive and 1 is negative.

Anti-code: The inverse code of positive number is the same as the original code, negative number of the inverse code is to its original code bit-wise, except for the sign bit.

Complement: The complement of a positive number is the same as the original code; The complement of a negative number is added 1 to its inverse code.

In the computer, the values are all stored in the complement, because it can be the symbol bit and the value of uniform processing.

********************************************************************

Take the 1 of the int type as an example:

-1 is a negative number, the original code is 10000000 00000001 (int is 2 bytes)

Anti-code is 11111111 11111110

Complement is 11111111 11111111

So how do symbols turn into unsigned? Just take the absolute complement of a straight line. Arithmetic resolution: |a|*2-|a| = unsigned a

int a =-1;

(unsigned int) a=?

(1) First Take-1 of the absolute value 1 of the original Code 00000000 00000001

(2) Convert it to anti-code 01111111 11111110

(3) A complement to the number of +1 to be asked. The complement is a value of unsigned char, converted to a decimal number of 65535

SO (unsigned int) a=65535;

Also, note: The printf function outputs unsigned values!!

Attached: (turn) C language symbol extension knowledge explained http://blog.csdn.net/u010069213/article/details/24012649

2. Symbolic problem of bit-domain variables

#include <stdio.h>struct data{int flag:1;//; or, all are allowed int other:31;}; int main () {struct Data Test;test.flag = 1;if (Test.flag = = 1) printf ("Test.flag =1,it is true\n"); elseprintf ("Test.flag!=1 , it is flase\n "); return 0;}

Output: Test.flag!=1,it is flase

Analysis: Flat is a bit-domain variable of type int, when a bit is used to represent an int, this one is to represent the sign bit, and the value range of the bit domain variable of the signed bit is 0 or 1 (the value range of the bit field variable of unsigned bit is 0 or 1)!!

When 1 is assigned to Test.flag, Test.flag overflows and becomes-1 ...

Make the structure a bit more code:

struct data{unsigned int flag:1;//; or, both are allowed int other:31;};

  

3. Accuracy of the integer division

int main () {float Result;result = 1/6;printf ("result=%f\n", result); return 0;}

Output: 0

Analysis: 1 and 6 are integer variables, and two integer variable results are still integral, and the integer part is not preserved. (After all, it is the first operation and then the equal)

Modify: Change at least one 1 or 6 to a floating-point type.

There is an implicit type conversion in the C language:

(1) When the assignment is the right value is converted to the left, but the right is an expression, the operation is performed before the result of the operation of the data type conversion.

(2) When different types of variables are calculated, the principle of low-to-high-level conversion is followed. such as: Char-->int, short-->int, float-->double ....

4. The problem of relative accuracy of floating-point numbers

int main () {float F = 1.0/3.0;float Expect_f = 0.333333333;double D = 1.0/3.0;double expect_d = 0.333333333;printf ("f =%f, Expect_f =%f, d =%lf, Expect_d =%lf\n ", f,expect_f,d,expect_d), if (f==expect_f && d = = expect_d) printf (" equal! !\n "); elseprintf (" Not Equal!!! \ n ");}

Output:

f = 0.333333, Expect_f = 0.333333, d = 0.333333, Expect_d = 0.333333
Not Equal!!!

Analysis: Floating-point numbers indicate a limited number of digits, and cannot accurately represent a decimal (IEEE754: single-precision float data type 7-bit valid digits, DOUBLE16-bit valid digits)

===== " when comparing floating-point numbers, it is common to compare the difference between them within a certain range

Change the condition portion of the IF to the following:

if (Fabs (f-expect_f) <0.000001 && fabs (d = = expect_d) <0.000001) printf ("equal!! \ n ");

  

5. Minimum integer inverse number overflow

int main () {int a = 0xffffffff;if (a<0) a =-a;printf ("a =%d \ n", a); return 0;}

Result: A = 1

Analysis: Signed data types, have positive or negative points, such as int,double,float ... To reverse after overflow, get a = 1

Workaround: To de-reverse the INT data type, you need to handle this situation extra, add the following code:

else if (a = = 0xFFFFFFFF) printf ("A =%d\n", a);

  

6. Temporary variable overflow problem

Long multiply (int m,int n) {long score;score = M*n;return score}

Analysis: The multiplication result of m,n will be stored in a temporary int variable, and then assigned to the long variable score, this temporary variable is easy to overflow. Therefore, the data conversion of M and N is required before the expression operation.

(64-bit platform, int 4bit, long 8bit--------------do not rule out some 32-bit software, such as dev-c++

32-bit platform, int 4bit,long 4bit)

No matter what: be sure to pay attention to data overflow problem!!

7. Distinguishing between continue and return

Return returns a value and exits the program;

Continue for loop, end of this cycle

8. Pointer constants and constant pointers

pointer constant: The pointer precedes the constant. int *const pointer name ------------- pointer itself is a constant, it points to the address can not be changed, but the data within the address can be changed according to the pointer's solution!!

Constant pointer: Constant in front of the pointer, const INT * pointer name-----------Pointer to a constant, as the name implies, the pointer to a constant, can not point to the variable! So you can't change the content of the point to the address!! , but the pointer itself is not a constant, its own value can be changed, that is, which constants to point to can be changed!

int main () {int a =2;int b =4;int *const pa = &a;   Pointer constants, which can only change the contents of an address const int *PB = &b; Constant pointer, can only change address std::cout<< *pb <<endl;std::cout<< *pa <<endl;pb=pa;std::cout<< *PB < <endl;*pa = 343;std::cout<< *pa <<endl;

Output: 4 2 2 343

That's right!!

9. Character arrays and pointers are not always equal

When different files are referenced with extern, the character array and pointers are not equal!!

************************************************

Not available in A.cpp:

Char a[]= "Hello world!";

Then in the B.cpp:

extern char *a; //wrorg

***********************************************

Strange problems caused by mixing 10.cin>> and getline

CIN is an object of the C + + standard output stream istream type, representing the standard output device, equivalent to the stdin in C. The program contains a iostream header file that can use both CIN objects. The IStream class overloads the extract operator ">>" to read the various underlying data types in C + +, and the extract ">>" reads the data based on the type of the subsequent variable, starting with the non-whitespace symbol without encountering the enter,space,tab end

The Std::getline function reads a row of data from IStream and encounters a "\ n" End

#include <iostream>using namespace Std;int main () {int a;string b;cout<< "Please input A:" <<ENDL;STD:: Cin>> A;//cin.ignore ();   Default Cin::ignore (1,eof) cout<< "Please input B:" <<endl;std::getline (cin,b);cout<< "A:" <<a <<endl;cout<< "B:" <<b<<endl;return 0;}

Output: value of B cannot be entered

Workaround: Activate the comment phrase

Reminder: Input/Output statements in one way, don't mix.

The Pit through C/

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.