APL\360

Fred Cisin cisin at xenosoft.com
Fri Jan 29 18:32:57 CST 2021


>>> if ( !(myfile = fopen( filename, "r"))

On Fri, 29 Jan 2021, Guy Sotomayor via cctalk wrote:
> In a lot of industry standard coding practices (MISRA, CERT-C) that type of 
> statement is prohibited and *will* result in an error being reported by the 
> checker/scanner.
> The if statement in your example has at least 2 errors from MISRA's 
> perspective:
> * assignment within a conditional statement
> * the conditional not being a boolean type (that is you can't assume 0
>   is false and non-0 is true...you actually need to compare...in this
>   case against NULL)

That particular structure has become an industry standard.
MOST dialects of C return a NULL pointer on fopen error.
Similarly the code in strcpy has an assignment and is using the numeric 
valus of each character as if it were boolean, with the terminating NULL 
ending the while condition.

There are some situations where some code might not be obvious to all, in 
which case good comments provide the explanation.
For example, when I use DAA or AAM, etc. in X86 assembly language, I 
always comment heavily since my uses for breaking up and/or printing 
numbers are not the original intent for those instructions.



More information about the cctalk mailing list