On Tue, 2022-03-29 at 20:35 -0700, Charles Mills wrote:
> > Almost all modern C/C++ compilers will flag that as a warning
> 
> That may well be. I stand by my claim that it is a common source of errors.

I agree. Which is why a lot of C programmers used to used to code if statements 
with no lvalues:

if (3 = foo) ...

> 
> I am not sure if z/OS XLC flags it. I am also not sure whether z/OS XLC is a 
> modern C/C++ compiler. <g>

xlclang does. In fact it goes further and will validate that printf() arguments 
match the format specifiers and will also check for buffer overflows. But it's 
64bit only so can can't be used with
legacy code without jumping through AMODE switching hoops. The new LLVM/clang 
compiler currently in beta solves those problems and is bang up to date as IBM 
have joined the LLVM open source commnunity
and are committing z/OS patches. It supports 31bit and C++20 with all the nice 
stuff like modules and concepts. We're hanging out for it to GA!

> 
> The intentional use of = in a condition is a favorite of old-style "how few 
> keystrokes can I use" C programmers, for example in the formulation
> 
> if (myfile = fopen(...)) ...  // executes code if fopen is successful
> 
> Charles
> 
> 
> -----Original Message-----
> From: IBM Mainframe Discussion List [mailto:[email protected]] On 
> Behalf Of David Crayford
> Sent: Tuesday, March 29, 2022 7:49 PM
> To: [email protected]
> Subject: Re: PL/I question
> 
> On Tue, 2022-03-29 at 15:10 -0700, Charles Mills wrote:
> > Well, while we are digressing ...
> > 
> > There was discussion earlier of the use of the equal sign for assignment 
> > versus comparison.
> > 
> > I am going to guess that THE most common cause of C/C++ program errors is 
> > the mis-use of = for comparison. (The comparison operator in C and C++ is 
> > ==.) In C if you code
> > 
> > if (foo = 3) bar = 5;
> > 
> > Then the compiler generates code to assign 3 to foo, test the result (3) 
> > for truth (not 0) and then execute the conditional statement. The above has 
> > the same effect as
> 
> Almost all modern C/C++ compilers will flag that as a warning. And there are 
> options to promote warnings to errors. For example:
> 
> ----------------------------------------------------------------------
> For IBM-MAIN subscribe / signoff / archive access instructions,
> send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to