1、Expression sizeof(‘a’) always results in 1 , because ‘a’ is a character.
【Analysis】
Answer:×
Key Point:Type of character literal andsizeof behavior
Analysis:
InC++, the type of character literal‘a’ ischar,sizeof(char) guarantees1
However inC, character literals areint type,sizeof(‘a’) may be4
Since the question clearly states it isC++, the result is indeed1, but the statement“always“ is inaccurate, becauseC language differs
Note:C andC++ have differences in character literal types.
2、InC++, all global variables, if not explicitly initialized, will be automatically initialized to0.
【Analysis】
Answer:√
Key Point:Initialization rules for global variables
Analysis:
Global variables and static variables have static storage duration
If not explicitly initialized: basic types (int,float etc.) are initialized to0; pointer types are initialized tonullptr; class types call the default constructor
Trap:Local variables are not automatically initialized, values are undefined.
3、The statement do { … } while (false); will execute the statements in the loop body at least once.
【Analysis】
Answer:√
Key Point:do-while loop characteristics
Analysis:
Thedo-while loop executes the loop body first, then checks the condition
Even if the condition is initiallyfalse, the loop body is executed at least once
while(false) causes the loop to execute only once before exiting
Application scenario:Commonly used in macro definitions to create scope.
4、InC++, ++i is a left-value expression, while i++ is a right-value expression.
【Analysis】
Answer:√
Key Point:Return value types of prefix and postfix increment operators
Analysis:
++i returns a reference to the variable, which is a left-value (can appear on the left side of an assignment)
i++ returns a temporary value, which is a right-value (cannot appear on the left side of an assignment)
int i = 0;++i = 5;// Valid, i becomes 5i++ = 5;// Invalid, compilation error
5、Forenum Color { RED, GREEN, BLUE }; , RED has the typeint.
【Analysis】
Answer:×
Key Point:Base type of enumeration type
Analysis:
InC++, the type of enumeration items is the enumeration type itself (Color), notint
Although enumeration items have corresponding integer values, the types are different
C++11 introduced scoped enumerations:enum class Color { RED, GREEN, BLUE };
Color c = RED;// c's type is Color, not int
6、The macro definition#define SQUARE(x) x * x is not a safe macro definition, SQUARE(2+3) will incorrectly calculate 25 .
【Analysis】
Answer:×
Key Point:Traps in macro definitions
Analysis:
SQUARE(2+3) expands to2+3 * 2+3, resulting in2+6+3=11, not25
Correct usage:#define SQUARE(x) ((x) * (x))
Even with parentheses,SQUARE(i++) still has side effect issues
Best practice:Use inline functions instead of macros.
7、InC++, the value range of char type is always -128 to 127.
【Analysis】
Answer:×
Key Point:Sign of char type
Analysis:
char sign is defined by the implementation, it can besigned char orunsigned char
Three types ofchar types:signed char: -128 to127; unsigned char: 0 to255; char: determined by the compiler, can besigned orunsigned
Usestd::numeric_limits<char>::is_signed to check.
8、The expression a > b ? a : b = 10; is not necessarily validC++ code.
【Analysis】
Answer:×
Key Point:Precedence and associativity of the conditional operator
Analysis:
The expression resolves to(a > b ? a : b) = 10;
Ifa>b, it attempts to assigna, but ifa is a right-value, it is illegal
Ifa≤b, it attempts to assignb, but ifb is a right-value, it is illegal
int a=1, b=2;(a > b ? a : b) = 10;// May be valid, depends on whether a,b are left-values// But if it is: (a > b ? 1 : b) = 10;// Invalid, 1 is a right-value
9、The statements#include “file.h” and #include <file.h> have different search strategies when the compiler looks for header files.
【Analysis】
Answer:×
Key Point:Differences in search paths for header file inclusion
Analysis:
#include “file.h” searches the current directory first, then the system directory
#include <file.h> directly searches the system directory
The specific search paths are defined by the compiler implementation
Best practice:Use<> for system header files, and“” for custom header files.
10、In the same scope, extern declared variables can be defined multiple times.
【Analysis】
Answer:×
Key Point:Function of the extern keyword
Analysis:
extern is used to declare variables, not define them
Variables can only be defined once, but can be declared multiple times withextern
Violating the“one definition rule“(ODR) will lead to linking errors
extern int a; // Declarationextern int a;// Re-declaration, allowedint a = 10; // Definition, can only be once
Core Knowledge Summary
1. Confusing Concepts
1、C andC++ differences:Different types of character literals
2、Initialization rules:Globalvs local variables
3、Operator characteristics:Return values of prefix and postfix increments
4、Type system:Actual type of enumeration items
2. Common Traps
1、Macro definitions:Parameter expansion issues
2、char type:Sign uncertainty
3、Operator precedence:Traps of the conditional operator
4、Header file inclusion:Differences in search paths
3. Exam Preparation Suggestions
1、Understand the difference between standards and implementations
2、Master the precise semantics of various constructs
3、Pay attention to historical compatibility issues
4、Validate understanding through code practice