problems with logic operations within loops

E

Ersek, Laszlo

WARN_BASE=-Wc,-Wall -Wc,-W
WARN_BASE += -Wc,-Wno-unknown-pragmas
WARN_BASE += -Wc,-Wpointer-arith
WARN_BASE += -Wc,-Wshadow
WARN_BASE += -Wc,-Wwrite-strings
WARN=$(WARN_BASE)
WARN += -Wc,-pedantic
WARN += -Wc,-Wbad-function-cast
WARN += -Wc,-Wcast-align
WARN += -Wc,-Wcast-qual
WARN += -Wc,-Wconversion
WARN += -Wc,-Wformat-nonliteral
WARN += -Wc,-Wmissing-declarations
WARN += -Wc,-Wmissing-prototypes
WARN += -Wc,-Wnested-externs
WARN += -Wc,-Wstrict-prototypes
WARN += -Wc,-Wundef

The "pedantic" reader will notice that the "-Wall" option to gcc
doesn't really get you "all" warnings, as you would rightfully expect.
When using the "-Wall" option with gcc, think of it as something like
"-Wmost" or "-Wmany". Go figure.

Interesting. I notice you don't pass -ansi or -std=c[89]9, even though
you pass -pedantic. Maybe you do this exactly in order to enable C++
style comments:

----v----
`-ansi'

[...] For the C compiler, it disables recognition of C++ style `//'
comments as well as the `inline' keyword.
----^----

I use -Wformat=2 instead of -Wformat-nonliteral, as the former includes
the latter and more. I also pass -Wfloat-equal -Wlarger-than-32767
-Wlong-long -Wredundant-decls -Wunreachable-code.

Cheers,
lacos
 
M

Michael Foukarakis

On Tue, 26 Jan 2010 17:02:58 +0000, Richard Heathfield


[snip]
In other implementations, you have to invoke the compiler in a
non-conforming mode in order to use them.

Wrong.

I use // comments in C code and invoke the (gcc) compiler in a
conforming mode. The trick to getting this to "work" is to pipe the
output from the compiler into a script that weeds out the warning
about "C++ style comments". The filter applied to the output of gcc is
like this:

grep -v 'warning:.*C\+\+ style comments'

Then one day, you might get introduced to -Werror (aka. The
Sledgehammer), or discover warnings that SHOULD'VE been errors
(especially if you're using someone else's code) and your "trick" will
give you so much grief.. :) Please send me a picture of your facial
expression then. TY.
The "pedantic" reader will notice that the "-Wall" option to gcc
doesn't really get you "all" warnings, as you would rightfully expect.
When using the "-Wall" option with gcc, think of it as something like
"-Wmost" or "-Wmany". Go figure.

No. Go RTFM. "Note that some warning flags are not implied by -Wall".
 
J

Joachim Schmitz

jacob said:
Richard Heathfield a écrit :

This is yet another lie. Please name one compiler that doesn't
accept // comments by default.

c89 on NonStop Kernel. And no, there is no c99 nor c95 available on that
platform.

There is, however, a switch to enable C++ style
comments, -Wallow_cpluplus_comments
There is also a switch -Wc99lite to ebable some, but by far not all c99
features.
And no, putting gcc in pedantic mode doesn't count.

This is standard C, and it is widely implemented. I bet you can't
even name one compiler that doesn't accept those comments.

Bet lost.

Bye, Jojo
 
J

jacob navia

Joachim Schmitz a écrit :
c89 on NonStop Kernel. And no, there is no c99 nor c95 available on that
platform.

There is, however, a switch to enable C++ style comments,
-Wallow_cpluplus_comments
There is also a switch -Wc99lite to ebable some, but by far not all c99
features.

Well, you see?

// comments are an universal feature in all C compilers, please let's stop
this stupidity. It is the most widely used C99 feature.
 
A

Antoninus Twink

Clearly not. You have already been shown some examples.

Another lie to add to the tally, Heathfield.

Every single example presented so far has been of a compiler that
accepts // comments by default, but rejects them if you go out of your
way to provide options to run the compiler in a crippled mode with
certain features disabled.
Be my guest.

Why is the real world such an unappealing place to you, Heathfield?
 
A

Antoninus Twink

I do think you're being unnecessarily dogmatic, and confusing for
newcomers, here.

No shit, Sherlock.
Firstly, in the case of just about every compiler I've come across, the
question isn't one of "invoking in a non-conforming mode", it's a case
of "not very carefully specifying half-a-dozen flags to force it into a
conforming mode".
Exactly.

Even if there are no C99 compilers, what the OP posted was perfectly
good C99 as far as I can see (certainly it appeared to be perfectly good
C with no features that worked differently between C standards, but with
// comments).
Exactly.

To claim you'd like to widen topicality, and then claim that any code
with // comments in it is equivalent to Fortran seems a bit rich, IMO.

Exactly.
 
P

Paul N

Any compiler invoked in C90 conforming mode is required to issue a
diagnostic message when translating a translation unit containing at
least one syntax error or constraint violation. Since // comments are
always a syntax error in C90 (pathological cases excepted), all
C90-conforming compilers are required to object to // comments.


If by "accept" you mean "fail to issue a diagnostic message for", then
no C90-conforming implementations accept them. I will win the bet by
naming Borland C.

I think most people would regard "Borland C accepts // comments in its
default mode" as being a proof, rather than a disproof, of the
statement "Borland C accepts // comments".
 
P

Phil Carmody

Tim Streater said:
if (isspace(ch)==true) continue;

I don't remember seeing anyone jump on that line. isspace() returns
something which is either zero or non-zero. Do not pretend you can
guess what non-zero value it might have. (And cast not-unsigned
chars to unsigned char before passing them to it, of course.)

Phil
 
S

Seebs

I don't remember seeing anyone jump on that line. isspace() returns
something which is either zero or non-zero. Do not pretend you can
guess what non-zero value it might have. (And cast not-unsigned
chars to unsigned char before passing them to it, of course.)

Good catch.

Interestingly, you can safely do (in C99):
bool x = isspace(ch);
if (x == true) ...

-s
 
B

Ben Pfaff

Seebs said:
Interestingly, you can safely do (in C99):
bool x = isspace(ch);
if (x == true) ...

Yes.

"bool" is tricky though. Suppose that you instead wrote this:

bool x = isspace(ch);
if (x) ...

This will always do the right thing on C99. It usually works on
C89, too, if "bool" is a typedef to, say, unsigned char. But not
always: if isspace() returns 0x1000 for "true", and unsigned char
is an 8-bit type, then it will misclassify spaces as non-spaces.

So I always (if I'm thinking carefully) write assignments to bool
like this:

bool x = isspace(ch) != 0;
 
K

Keith Thompson

Seebs said:
Good catch.

Interestingly, you can safely do (in C99):
bool x = isspace(ch);
if (x == true) ...

Yes, but there's still no good reason to write
if (x == true)
rather than
if (x)

Of course, a more meaningful name than "x" makes this clearer.
 
K

Keith Thompson

Tim Streater said:
I expect you can tell I normally do all my work in PHP these days.

<OT>
Not really. As far as I can tell, the "== true" is no more necessary
(or sensible) in PHP than it is in C.
</OT>
 
N

Nick Keighley

I don't remember seeing anyone jump on that line. isspace() returns
something which is either zero or non-zero. Do not pretend you can
guess what non-zero value it might have. (And cast not-unsigned
chars to unsigned char before passing them to it, of course.)

there's an easy work around just define "true" as non-zero!

#define true !0






:)
 
D

Daniel Giaimo

there's an easy work around just define "true" as non-zero!

#define true !0

I don't know if you're joking, but that won't work. !0 is 1
not any value other than 0, so, for example, 2==!0 is false.
 
K

Keith Thompson

Daniel Giaimo said:
I don't know if you're joking, but that won't work. !0 is 1
not any value other than 0, so, for example, 2==!0 is false.

Apparently you missed (and snipped) the smiley.
 
A

Antoninus Twink

When I refer to a particular C compiler, you may generally assume that
I'm talking about its conforming mode unless I say otherwise.

Sure, Heathfield.

And the rest of us will keep on assuming we're talking about its
fully-functional mode.
After all, if it's not being invoked in a conforming mode, it's not
really a C compiler, is it?

Did anyone ever tell you you're a complete dick?
 
B

Barry Schwarz

Hi folks I’m working though some exercise trying to teach myself C,
I’m having problem with a function I’ve written. It takes and array
and removes the white spaces.  I’m having problems with logic
operations within loops.

My first problem is once If statement executes and “i” is incremented
the else statement doesn’t execute again!! WHY???
*****************************CODE******************************************­**

/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
    int i;

    for(i=0;i<=maxi;++i)
    {
     if(from==' ' || from=='\0' || from=='\t'|| from
=='\n')
        {
           to = from[i+1];       // if there is white space copy
the next value
           i++; //>>>>>>>>>>>>>>>>>>>> problems start here
        }

      else
        {
        to = from;            // if no white space copy
        }
    }

}

********************************CODE***************************************­***

So I thought I’d approach the problem from the opposite angle if it’s
not white space, but the if statement executes whether there is white
space or not…..WHY???

********************************CODE2**************************************­***
/* copy: copy 'from' into 'to'removing white space */
void copy(char to[], char from[], int maxi)
{
    int i;

    for(i=0;i<=maxi;++i)
    {
     if(from!=' '||from!='\0'||from!='\t'||from!='\n')>>>>>>>>executes whether there is white space or not

        {
           to = from;       // if no white space
copy
        }

        else
            {
            to = from[i+1];   // if there is white space copy the
next value
            i++;
            }
    }

}

**************************************CODE2********************************­*****


There are several problems with your code. Many have been mentioned
but they may have been lost due to the low signal/noise ratio that
this thread has degenerated to.

1 - The limits of your for loop are suspect. If maxi is the maximum
index then it is fine but the ususal C idiom for functions like this
is to use a length. In that case, the maximum index would be maxi-1
and you should use <, not <=.

2 - Your if expression evaluates to true under all defined
conditions. Probably not what you had in mind.

3 - Your test against '\0' is suspect. You did say array of
characters and not string but most white space removal tools work on
strings. If you did mean strings, then you should stop at the first
'\0' because by definition that is the end of the string.

4 - Your if expression is not exhaustive. There are other white space
characters. You might want to look up the isspace standard function.

5 - You blindly copy the character following a white space character.
You don't know if this character even exists (you could be at the end
of the array). If it does exist, you don't know if it is also white
space and if so your function should not copy it either.

6 - You use the same index for source and destination. Once you skip
over the first white space character, the two arrays no longer have
the same number of characters.

7 - Since '\0' is currently not special to you, you have no way to
tell the calling function how much of the target array contains valid
data.
 
C

Curtis Dyer

<OT>
Not really. As far as I can tell, the "== true" is no more
necessary (or sensible) in PHP than it is in C.
</OT>

<OT>
Indeed, but perhaps Tim was referring to the cases when you need
to use "$x === true" to also ensure strict type comparison. Just
a thought.
</OT>
 
B

Ben Bacarisse

Tim Streater said:
And I prefer to write "==true" because it makes my code more readable
to me.

You are probably still talking about PHP, but just in case this gets
taken as a more general point, it is unwise to do that in C because
true must be just one value and any non-zero values is acceptable as
"not false" (such as those that the isxxxx functions might return).
If I have "if (x>3) { ..." I can read this as "if x is greater
then 3 then do so-and-so". I read "if (x) { ..." as "if x then do
so-and-so". WTF? If x *what*? Forgot to put the cat out? Needs its
hair cut?

I'd say that is the fault of whoever chose x as the name. If x is a
boolean, the name should indicate WTF: if (x_needs_hair_cut)... If x
is not boolean (say it is the number of until a hair cut is required)
I would test against 0: if (x_days_to_hair_cut == 0)...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top