When writing C files in VS Code, I named a file first.c.cpp. After completing the program, I encountered errors during execution. After 30 minutes of troubleshooting, I discovered the issue lay in the file name:
The .c extension led the IDE to mistakenly identify it as a C program, causing VS Code to use gcc (the C compiler) instead of g (the C compiler) to compile my code.
Caption: How fool!
Java's design philosophy differs significantly from traditional compiled languages:
This design achieves the goal of "Write Once, Run Anywhere," whereas C executables (.exe files) are limited to running on a single platform.
Advantages:The same program can run on different computers without modification
Disadvantages:The additional step in the process can make compilation slightly slower compared to traditional methods
Write once, Run anywhere
---------James Gosling
Little Endian: The least significant byte is stored at the lowest address. This storage method emerged to facilitate CPU memory reading, which occurs from low to high addresses. Interestingly, this is opposite to humans typically write numbers.
For example:
Binary representation of 329933 is 00000000 00000101 00001000 11001101
Little Endian storage: 11001101 00001000 00000101 00000000
As we can see, Little Endian reverses the order of bytes in the binary representation. However, it's crucial to note that the bit order within each byte remains unchanged!
My favorite experiment for introducing type casting!
# includeint main() { int a; int *p; a=329933; p=&a; char *q; q=(char*)p; printf("%d\n",*p); printf("%d\n",*q); }
Output:
329933 -51
I'm curious why it outputs -51?
Is this a coincidence? Let's try two more examples
printf("%d\n",*(q 1)); printf("%d\n",*(q 2));
Try it:
When performing forced type casting, (char)p will point to the address of the first byte of the four-byte int, which is 11001101.
The leftmost 1 represents the negative sign, indicating it's a negative number. After applying two's complement, we get: 0110011 (the last 7 bits)
(Note: For positive numbers, the two's complement is simply the binary representation of the decimal number. For negative numbers, the two's complement is obtained by inverting all bits except the leftmost (highest) bit, then adding 1 to the rightmost bit.)
Converting this to decimal gives us -51. Interesting, right?
Many people wonder why it's -128. If you know the answer, feel free to share it in the comments section. This will not only help others but also help you organize your thoughts.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3