In programming, including C++, octal numbers (base-8) are represented using a leading 0. Octal digits range from 0 to 7, and the prefix 0 helps distinguish an octal value from decimal or hexadecimal.
For example:
- 012 in octal is equal to 10 in decimal.
- 075 in octal is equal to 61 in decimal.
Hexadecimal is a base-16 numbering system widely used in computing. The prefix 0x is commonly used to indicate that a number is in hexadecimal format. For example:
- 0xA represents the decimal value 10.
- 0xFF represents the decimal value 255.
- 0x1A3 represents the decimal value 419.
In hexadecimal, the digits range from 0 to 9 and A to F, where A equals 10, B equals 11, and so on up to F, which equals 15.
In C++, the suffix U (or u) is used with integer literals to indicate that the literal is of type unsigned int.
This can be particularly useful when working with numbers that should never be negative, such as array indices, bitwise operations, or certain calculations.
In C++, the suffix f (or F) is used to denote a float literal explicitly. When you write 2.5f, it tells the compiler to treat 2.5 as a float instead of the default double type.
In C++, single quotes (' ') are used to represent character literals. A character literal stores a single character, which can be a letter, number, punctuation, or even an escape sequence, in a variable of type char.
Global variables:
- Scope: A global variable is declared outside of all functions, typically at the top of the file. It is accessible throughout the program, across all functions.
- Lifetime: Global variables exist for the entire duration of the program.
- Use case: Useful for data that needs to be shared across multiple functions. However, overusing them can lead to poor code structure or unexpected bugs.
Local variables:
- Scope: A local variable is declared within a function or block. It is accessible only within that specific function or block.
- Lifetime: Local variables exist only during the execution of the function or block in which they are declared.
- Use case: Preferred for temporary or function-specific operations to avoid unintended side effects.
compiler: (computing) a program that translates instructions from one computer language into another for a computer to understand
In C++, automatic variables are simply local variables that are automatically created when their scope is entered and destroyed when their scope is exited. By default, all local variables in a function are automatic unless explicitly specified otherwise.
Making declarations anywhere in the code but not just at the beginning of a scope is a great feature of C++.
Microsoft Copilot
沒有留言:
發佈留言