Solve the two warning prompts of "warning C276: constant in condition expression" and "warning C294: unreachable code"

Long time no see. Recently, because some projects are quite busy, I haven’t posted many articles. Today, I’m free to share the two warnings I encountered before.

Warning information:

First of all, let's take a look at these two alarms. The first alarm is: "warning C276: constant in condition expression", and the second alarm is: "warning C294: unreachable code" .

These two warning prompts are derived from this code:

//错误代码
if(control_code = 0x00FF)   
{
    
    
    Beep_Led(1);
}
else if(control_code = 0xFF00)  
{
    
    
    Beep_Led(3);
}

If you take a closer look, you will know that I made two mistakes. It is these two mistakes that caused the compiler to report two prompts to me. The first mistake is that I should not use = in if(), but should use == That's right, using = is equivalent to assigning a constant to a variable, so the first warning that the compiler prompts me is "warning C276: constant in condition expression" (warning C276: constant in condition expression).

Then let's look again, because the conditional expression of our if is not true, so the if cannot be judged, and the following else if is also not true, so the compiler prompts the second warning: "warning C294: unreachable code" (warning C294: unreachable code) code), and the = in the conditional expression in the second else if() should also be changed to ==.

Solution:

In this case, check whether there is a problem with your conditional expression or logic statement, and you can fix it after finding out the problem~
Therefore, the correct code above should be:

//正确代码
if(control_code == 0x00FF)     //将"="修改为"==",修改完逻辑语句正确,else if也就成立
{
    
    
    Beep_Led(1);
}
else if(control_code == 0xFF00)  //将"="修改为"=="
{
    
    
    Beep_Led(3);
}

The above example can be used as a case. If you have encountered similar problems, you can search and rectify them according to this method.

My level is limited, the above information is for learning reference only, if there is any error or inappropriateness, please advise.
In addition, it is not easy to create, please do not plagiarize, if it is helpful to everyone, I hope everyone can like it, thank you~

Guess you like

Origin blog.csdn.net/OMGMac/article/details/127838006