C Language Common C programming idioms and developer practices Comparing literal and variable

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Suppose you are comparing value with some variable

if ( i  == 2) //Bad-way
{
    doSomething;
}

Now suppose you have mistaken == with =. Then it will take your sweet time to figure it out.

if( 2 == i) //Good-way
{
    doSomething;
}

Then, if an equal sign is accidentally left out, the compiler will complain about an “attempted assignment to literal.” This won’t protect you when comparing two variables, but every little bit helps.

See here for more info.



Got any C Language Question?