[C ++ Basics] Details few people know about C ++

Source: Internet
Author: User
Document directory
  • 3. Macro-defined operations
  • 4. sizeof (union)
  • 5. Array Memory Allocation
  • 6. Char <=> int
1. Unsigned int automatic conversion
Void foo () {unsigned int a = 6; int B =-20; (a + B> 6 )? Puts ("> 6"): puts ("<= 6"); // output> 6/* test whether you understand the principle of automatic Integer Conversion in C, I found that some developers know little about these things. In any case, the answer to this unsigned integer is "> 6 ". The reason is that when there are signed and unsigned types in the expression, all operands are automatically converted to the unsigned type. Therefore,-20 is a very large positive integer, so the result calculated by this expression is greater than 6. This is very important for embedded systems that should frequently use the unsigned data type. */}

2. array community
Union {int I; char x [2];} a; void fun () {. x [0] = 10;. x [1] = 2; printf ("% d-% d \ n",. i,. x [0],. x [1]); // 522-10-2: Description. x [0] and. x [1] has no influence on each other, so their addresses are different. A. I =. x [0] +. x [1] * 256 (Power 8 of 2);. I = 1027; printf ("% d-% d \ n",. i,. x [0],. x [1]); // 1027-3-4 1027 = (4) * 256 + (3). x [0] = 300;. x [1] = 10; printf ("% d-% d \ n",. i,. x [0],. x [1]); // 2604-44-10,. x [0] = 300-256 = 44;. I =. x [0] +. x [1] * 256;}/* union is a space used by internal variables. allocated according to the size, int I occupies 4 bytes, char x [2] occupies 2, so a total of 4 bytes are allocated. A total of 4 bytes of memory, corresponding to x is equivalent to taking up 2 lower bytes, and 1, and 10 assigned to x, there are two and ten digits (hexadecimal) the public body shares the size of the sizeof (a) in a memory area that is the longest member in the shared body. That is, I int: (| _ |) (| _ |) (| _ |) char x [2]: (| _ |) ^ high address and low address analysis:. x [0] = 10 ======================================== ===================> (| 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |) a. x [1] = 2 ====================================== ========> (| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |) if the length of the Public sizeof (int) is 4-byte 32, the shared memory is: (| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |) (| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |) (| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |) (| 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |). if I is a 4-byte integer, I = 2 ^ 9 + 2 ^ 3 + 2 ^ 1 = 256 + 8 + 2 = 522 */
3. Macro-defined operations
#include <iostream>using namespace std;#define SQR(X) X*Xint main(void){int i=10,j=5,n=10;n*=i+j;//n=n*(i+j)cout<<n<<endl;//150int a = 10,k = 2, m=1; a /= SQR(k+m)/SQR(k+m); //a/=(k+m*k+m/k+m*k+m)cout<<a<<endl;//1return 0;}
4. sizeof (union)
#include <iostream>using namespace std;union a {int a_int1;double a_double;int a_int2;};typedef struct{a a1;char y;} b;class c{double c_double;b b1;a a2;};int main(void){cout<<sizeof(a)<<endl;//8cout<<sizeof(b)<<endl;//16cout<<sizeof(c)<<endl;//32return 0;}
5. Array Memory Allocation
Char str1 [] = "abc"; char str2 [] = "abc"; const char str3 [] = "abc"; const char str4 [] = "abc "; const char * str5 = "abc"; const char * str6 = "abc"; char * str7 = "abc"; char * str8 = "abc "; cout <(str1 = str2) <endl; cout <(str3 = str4) <endl; cout <(str5 = str6) <endl; cout <(str7 = str8) <endl; Result: 0 0 1 answer: str1, str2, str3, str4 are array variables, they have their own memory space; str5, str6, str7, and str8 are pointers pointing to the same constant area.
6. char <=> int
// One integer to Char char x = 5 + '0 '; // '5' // a char to int y = '5'-'0 '; // 5 // obtain the 'D' char z = 'A' + 3 after 'a'; // 'D'

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.