View Single Post
  #62   Report Post  
Posted to rec.crafts.metalworking
James Waldby[_3_] James Waldby[_3_] is offline
external usenet poster
 
Posts: 257
Default Beginning programming question

On Sat, 05 Mar 2011 23:47:21 -0600, Jon Elson wrote:
....
C is really a CRAPPY language, and has a number of syntactic "features"
that make it VERY easy to make hard-to-find mistakes. The meanings of =
and == is a good example.

if (a==b) { do something}
is the normal conditional statement.
However :
if (a=b) {do something}
not only does the "something" based only on the value of b, it assigns
the value of b to a. In a couple dozen pages of code, I defy anyone to
find such a bug in less than a few hours. There are dozens more like
this I know about.


Problems like that take mere seconds to find if one uses reasonable
compiler options. For example, command "gcc iftest.c -Wall -o iftest"
produces the following message for the program shown below.

iftest.c: In function €˜main:
iftest.c:5: warning: suggest parentheses around assignment used as truth value

#include stdio.h
int main (void) {
int a=0, b=1;
if (a=b)
printf("if %d = %d\n", a, b);
return 0;
}

Program output is the same ("if 1 = 1") with or without the -Wall
("All Warnings") option. In my experience it is reasonable to always
compile with -Wall, and work on the program until all warnings go
away. Note, -Wall doesn't actually produce all possible warnings;
for that you need switches like -ansi and -pedantic; see eg notes
at http://tombarta.wordpress.com/2008/05/25/gcc-flags/

On the other hand, C generally produces efficient code, and is portable
to hundreds of different platforms. C++ adds a number of advanced
features that make it possible to create functions that perform
operations not built into the language, but I don't use them enough to
be really comfortable with them.


--
jiw