Today, I’m going to talk about semantic errors (in an abstract way) in C++.
```cpp
char n1, n2;
n3 = n1 * n2;
n3 = n1 + n2;
```
What happens when you compile this code in a C++ compiler? Is this code runnable?
You might expect two possible outcomes:
1. **Compile-time Error:**
The code won’t run and will instead throw an error message, as it seems logical to assume that character types cannot handle operations like addition or multiplication.
2. **Runtime Behavior:**
The code runs without any compile-time error, but the programmer might encounter unexpected runtime behavior.
Which is the correct answer?
In C++, `char` literals are internally treated as `int` types. When you perform operations on `char` values, they are implicitly converted to their corresponding ASCII values, and the operation is executed as if they were integers.
### Conclusion
In C++, using operators on character types is not considered a compile-time error. This demonstrates that C++ doesn’t strictly enforce catching semantic errors during compilation (though this might vary depending on specific cases).
This flexibility is one of the characteristics of the C++ language.
reference
https://stackoverflow.com/questions/2816238/semantic-errors
Semantic errors
Can semantic errors be detected by the compiler or not? If not when do the errors get detected? As far as I know semantic errors are those errors which result from the expressions involving opera...
stackoverflow.com
Compilers: Principles, Techniques, and Tools