- Fastest Environment Setup
- Install the C/C++ Extension Pack in
vscode, which includesC/C++, C/C++ Themes, CMake Toolsextensions. - For Windows systems, use the
Winlibsstandalone build package, which includes a completegcc, g++compiler environment forWindows. Just configure the environment variables to use it.
- Why Do We Need a Compiler?
- C++ is a compiled language, meaning that source code must be converted into machine code by a compiler to generate an executable file, hence the need for a compiler.
- In contrast, Python and Lua are interpreted languages that read, parse, and execute code line by line through an interpreter, allowing the source code to run directly without compilation.
- Difference Between Local and Cross Compilation Chains
- Since
C++does not define a unified virtual machine runtime environment likeJava, it requires direct compilation of source code into machine code for the corresponding platform. Therefore, compiled programs cannot be ported across different operating systems and hardware architectures without recompilation, which is why there is a distinction between local and cross compilation chains. - A local compilation chain means that the compilation environment and the execution environment of the executable file are the same; a cross compilation chain means that the compilation environment and the execution environment of the executable file are different. If a file compiled under the
X86_64architecture needs to run in theX86_64architecture, a local compilation chain is chosen; if it needs to run under thearmarchitecture, a cross compilation chain is selected.
- Why Do Java and Go Not Have Local and Cross Compilers?
- Both Java and Go use a “compile-interpret” hybrid model, possessing both compilation and interpretation characteristics, but they handle them differently.
- Java source code is first compiled by the
Javaccompiler into bytecodebytecode, stored in.class/.jarfiles. During execution, this bytecode is interpreted by theJavaVirtual Machine(JVM), which translates the bytecode into machine code for the specific platform for execution. - Thanks to the high consistency and standardization of the
Javaecosystem, there is virtually no environment for cross-compilation. - Go achieves cross-platform capability through local compilation that supports multiple platforms. The
Gocompiler can directly compile code into machine code for the target platform, meaning the compiled executable is purely local and does not require an intermediary layer like theJVM. Additionally, theGotoolchain has built-in strong support for cross-compilation. - The executable files of
Goare statically linked by default, compiling the dependency libraries directly into the program, which eliminates the need for external environment dependencies at runtime. However, this independence and compilation completeness can increase the size ofGoprograms.
Basic Data TypesInteger: Represents whole numbers; Character: Represents characters; Boolean: Represents boolean values; Floating-point: Represents decimal numbers.
#include <iostream>
#include <cstdint> // For fixed-length integer types
int main() { // Integer int a = 42; short b = 32767; long c = 1234567890; long long d = 1234567890123456789LL;
// Unsigned integer (requires <cstdint> for specific width types) uint32_t e = 0xFFFFFFFF; // Maximum value: 4294967295
// Character char f = 'A'; wchar_t g = L'Ω'; // Using wide character
// Boolean bool h = true;
// Floating-point float i = 3.14159f; double j = 2.718281828459; long double k = 1.4142135623730950488L;
// Output some variables std::cout << "int a: " << a << std::endl; std::cout << "short b: " << b << std::endl; std::cout << "long c: " << c << std::endl; std::cout << "long long d: " << d << std::endl; std::cout << "uint32_t e: " << e << std::endl; std::cout << "char f: " << f << std::endl; std::wcout << L"wchar_t g: " << g << std::endl; // Using std::wcout to output wide character std::cout << "bool h: " << std::boolalpha << h << std::endl; // Output boolean value name std::cout << "float i: " << i << std::endl; std::cout << "double j: " << j << std::endl; std::cout << "long double k: " << k << std::endl;
return 0;}
Control StructuresConditional Statements
if (condition1) { // Execute code block 1}
else if (condition2) { // Execute code block 2}
else { // Execute code block 3}
Multi-condition Judgments
switch (expression) { case constant1: // Execute code block 1 break; case constant2: // Execute code block 2 break; default: // Execute default code block, can be omitted break;}
Loop Statements
// for loop, executes a fixed number of iterations
for (int i = 0; i < 10; ++i) { // Repeat this code 10 times}
// while loop, executes an uncertain number of iterations, continues as long as the condition is true
while (condition) { // Repeat this code as long as condition is true}
// do while loop, executes the loop body first, then checks if the condition is true. For example, in a login scenario, first get user input for the password, then check if the password is correct
do { // Execute this code first} while (condition); // Then check if the condition is true
Jump Statements
break // Exit loop or switch statement
continue // Skip remaining code in the current loop, proceed to the next loop check
return // Return value from function and exit function
goto // Force jump to a certain location, can disrupt execution logic, use with caution
