# problems with logic operations within loops

Discussion in 'C Programming' started by Interrupt, Jan 26, 2010.

1. ### InterruptGuest

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*************************************

Interrupt, Jan 26, 2010

2. ### Andrew PoelstraGuest

On 2010-01-26, Interrupt <> wrote:
> 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)

You almost certainly do not want <= here. Think about how
arrays are indexed in C.

> {
> if(from==' ' || from=='\0' || from=='\t'|| from
>=='\n')

#include <ctype.h>
....and use isspace() instead.

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

You can't use // comments in most C compilers today, and even
if you could you shouldn't on Usenet because they wrap around
like this and then nobody can compile your code.

Also, how do you know i + 1 is a valid index into from?

> i++; //>>>>>>>>>>>>>>>>>>>> problems start here

You can't use the same index into to[] and from[], if you
expect to be referencing different offsets in each array.

> }
>
> 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

Of course. When do you expect that (X != 1 || X != 2) will be false?
Perhaps if you would space your code decently it would be clearer.

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

....and you still haven't fixed the original problem.

> }
> }
>
> }
> ******************************CODE2*****************************
>

nb I have removed sixteen stars from each of these lines.

Andrew Poelstra, Jan 26, 2010

3. ### Tom St DenisGuest

On Jan 26, 10:40 am, Andrew Poelstra <>
wrote:
> You can't use // comments in most C compilers today, and even
> if you could you shouldn't on Usenet because they wrap around
> like this and then nobody can compile your code.

Um what? // are standard in C99 aren't they? GCC certainly will
accept them except when in -ansi mode. Though yes, it's good to use /
* */ instead, mostly for backwards compatibility but also to avoid
wrapping.

Tom

Tom St Denis, Jan 26, 2010
4. ### Andrew PoelstraGuest

On 2010-01-26, Tom St Denis <> wrote:
> On Jan 26, 10:40 am, Andrew Poelstra <>
> wrote:
>> You can't use // comments in most C compilers today, and even
>> if you could you shouldn't on Usenet because they wrap around
>> like this and then nobody can compile your code.

>
> Um what? // are standard in C99 aren't they? GCC certainly will
> accept them except when in -ansi mode. Though yes, it's good to use /
> * */ instead, mostly for backwards compatibility but also to avoid
> wrapping.
>
> Tom

They are standard in C99, but gcc is not standard in C99 mode

(It is close enough for most purposes, though. I perhaps should
have been clearer.)

Andrew Poelstra, Jan 26, 2010
5. ### Richard BosGuest

Andrew Poelstra <> wrote:

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

>
> You can't use // comments in most C compilers today,

Wrong (and it was wrong in many cases even before C99 made them
official), but...

> and even if you could you shouldn't on Usenet because they wrap around
> like this and then nobody can compile your code.

....right, as demonstrated.

Richard

Richard Bos, Jan 26, 2010
6. ### Tom St DenisGuest

On Jan 26, 10:53 am, Andrew Poelstra <>
wrote:
> On 2010-01-26, Tom St Denis <> wrote:
>
> > On Jan 26, 10:40 am, Andrew Poelstra <>
> > wrote:
> >> You can't use // comments in most C compilers today, and even
> >> if you could you shouldn't on Usenet because they wrap around
> >> like this and then nobody can compile your code.

>
> > Um what?  // are standard in C99 aren't they?  GCC certainly will
> > accept them except when in -ansi mode.  Though yes, it's good to use /
> > * */ instead, mostly for backwards compatibility but also to avoid
> > wrapping.

>
> > Tom

>
> They are standard in C99, but gcc is not standard in C99 mode
>
> (It is close enough for most purposes, though. I perhaps should
>  have been clearer.)

My point is GCC [and other compilers] silently accepted // by
default. You can only get gcc to whine about it when you enter ANSI
mode [e.g. C89/C90].

Though the rest of your point stands, C developers should use /**/ by
default.

Tom

Tom St Denis, Jan 26, 2010
7. ### Andrew PoelstraGuest

On 2010-01-26, Tom St Denis <> wrote:
> On Jan 26, 10:53 am, Andrew Poelstra <>
> wrote:
>> On 2010-01-26, Tom St Denis <> wrote:
>>
>> > On Jan 26, 10:40 am, Andrew Poelstra <>
>> > wrote:
>> >> You can't use // comments in most C compilers today, and even
>> >> if you could you shouldn't on Usenet because they wrap around
>> >> like this and then nobody can compile your code.

>>
>> > Um what?  // are standard in C99 aren't they?  GCC certainly will
>> > accept them except when in -ansi mode.  Though yes, it's good to use /
>> > * */ instead, mostly for backwards compatibility but also to avoid
>> > wrapping.

>>
>> > Tom

>>
>> They are standard in C99, but gcc is not standard in C99 mode
>>
>> (It is close enough for most purposes, though. I perhaps should
>>  have been clearer.)

>
> My point is GCC [and other compilers] silently accepted // by
> default. You can only get gcc to whine about it when you enter ANSI
> mode [e.g. C89/C90].
>

I (implicitly) meant that in default mode, gcc is not a C
compiler. (Rather a "GNU-C" compiler, which is a slightly
different beast.)

My bad - I try to word things to avoid these discussions,
since anyone who cares has in all likelihood heard them a
thousand times before.

> Though the rest of your point stands, C developers should use /**/ by
> default.
>
> Tom

Andrew Poelstra, Jan 26, 2010
8. ### jacob naviaGuest

Richard Heathfield a écrit :
>
> Yes. Most C compilers, however, are not C99 compilers.
>

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

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.

jacob navia, Jan 26, 2010
9. ### Keith ThompsonGuest

jacob navia <> writes:
> Richard Heathfield a Ã©crit :
>> Yes. Most C compilers, however, are not C99 compilers.

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

Accepting // comments doesn't make a C compiler a C99 compiler.
If Richard posted anything untrue, it wasn't in the text that
you quoted.

You need to stop throwing the word "lie" around.

[...]

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 26, 2010
10. ### Rob KendrickGuest

On Tue, 26 Jan 2010 18:20:07 +0100
jacob navia <> wrote:

> Richard Heathfield a Ã©crit :
> >
> > Yes. Most C compilers, however, are not C99 compilers.
> >

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

It is not a lie if you include the context you must have accidentally
snipped and not read.

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

The Norcroft/Codemist-based compiler I am occasionally forced to use
doesn't.

B.

Rob Kendrick, Jan 26, 2010
11. ### Keith ThompsonGuest

Rob Kendrick <> writes:
> On Tue, 26 Jan 2010 18:20:07 +0100
> jacob navia <> wrote:
>
>> Richard Heathfield a Ã©crit :
>> >
>> > Yes. Most C compilers, however, are not C99 compilers.
>> >

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

>
> It is not a lie if you include the context you must have accidentally
> snipped and not read.

It is not a lie given the context he actually quoted. Most C
compilers are *not* C99 compilers.

[...]

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 26, 2010
12. ### NickGuest

Richard Heathfield <> writes:

> Richard Bos wrote:
>> Andrew Poelstra <> wrote:
>>
>>
>>>> to = from[i+1]; // if there is white space copy
>>>> the next value
>>> You can't use // comments in most C compilers today,

>>
>> Wrong

>
> Wrong.
>
>> (and it was wrong in many cases even before C99 made them
>> official), but...

>
> No, in C99 you can use them - but only six people have a C99
> compiler. In other implementations, you have to invoke the compiler in
> a non-conforming mode in order to use them. If we accept as C
> something that a compiler can only accept in non-conforming mode, we
> can cheerfully accept Fortran as C, which is not something I'm
> prepared to do.

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

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".

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

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.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

Nick, Jan 26, 2010
13. ### FredGuest

On Jan 26, 8:49 am, Tim Streater <> wrote:
> On 26/01/2010 15:29, Interrupt wrote:
>
> > 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.

>
> If you use a "for" loop for this, it should look like:
>
> for (i=0;  i<maxi;  i++)
>
> Are you processing the whole string? If so then a "while" loop would be
> better, with your test being about whether the current character is a null.
>
> More importantly, you need to have a pointer moving along the input
> string, and another moving along the output string. These can't be the
> same pointer as they don't move along at the same rate necessarily (in
> general they won't).
>
> So it might be something like:
>
> j = 0;
> for (i=0;  i<maxi;  i++)
>       {
>       ch = from;
>       if  (isspace(ch)==true)  continue;
>       to[j] = ch;
>       j++;
>       }
>

Not necessarily. The OP did not include the definition of maxi
in the original posting, so it is possible that the "<=" is
correct (the name could be taken to imply that maxi is
the maximum value that i is allowed to have).
--
Fred K

Fred, Jan 26, 2010
14. ### jacob naviaGuest

Richard Heathfield a écrit :
> 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.
>
> #include <stdio.h>
>
> int main(void)
> {
> // get a diagnostic message
> puts("Hello, diagnostic message.");
> return 0;
> }
>
> When I compile this in conforming mode, I get this output:
>
> Error E2188 scratch.c 5: Expression syntax in function main
> *** 1 errors in Compile ***
>

Sorry but which "borland C" did you use?
I downloaded Turbo C from 1995 (the earliest I could get
that is still available) and compiled that without any problems.

You are using turbo C from 1989, or similar rubbish

You are lying again heathfield.

jacob navia, Jan 26, 2010
15. ### Michael Angelo RaveraGuest

On Jan 26, 7:29 am, Interrupt <> wrote:
> 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********************************­*****

OK. First, most array functions would have the signature as follows:

void copy(char to[], char from[], int from_size, int to_max_size, int
* to_size)

or

int copy(char to[], char from[], int to_max_size, int from_size)

or even

int copy(char to[], char from[], int from_size)

It is important to know how large the resultant array is, since it can
not be predicted from the size of the input how large the output will
be, and you will want to know how big the result is.

If I were writing the function, and had access to the standard
library, I'd write this function as follows:

int copy (char to [], char from [], int from_size)
{
/*assume that we have at least from_size in the to[] array */
int res;
int idx;
for (res = 0, idx = 0; idx < from_size; idx ++)
{
if (!isspace (from [idx])
to [res ++] = from [idx];
}
return res;
}

If you have some special definitiion of what a whitespace characfter
is, you could substitute it in the test. If you want to stop on a null
or on the length, you could make the test say from "(idx < max_from)
&& from [idx]"

If instead of or in addtion to returning the length of the resulting
string, you'd rather put a null at the end of the buffer, you just do
to [res] = '\0'; ahead of the return line.

If you want to include maximum result length as an argument to the
function, you would augment the test in the for loop with "&& (res <
max_to_size)"

Lots of variations on this are possible. Have fun.

Michael Angelo Ravera, Jan 26, 2010
16. ### Eric SosmanGuest

On 1/26/2010 4:37 PM, Richard Heathfield wrote:
> Tim Streater wrote:
>> On 26/01/2010 18:32, Richard Heathfield wrote:
>>> jacob navia wrote:
>>>> Richard Heathfield a écrit :
>>>>>
>>>>> Yes. Most C compilers, however, are not C99 compilers.
>>>>>
>>>>
>>>> This is yet another lie.
>>>
>>> No, it isn't. Please don't use the word "lie" when what you really mean
>>> is "something Jacob Navia doesn't agree with".

>>
>> Ahem. Any chance all you chaps could focus on the OP's problem ?
>>

>
> I assumed it had already been dealt with. Still, since you ask:
>
> void copy(char to[], char from[], int maxi)
> {
> int i = 0;
> int j = 0;
> while(maxi--)
> {
> if(isspace(from))

IHYM `isspace( (unsigned char) from )'.

--
Eric Sosman
lid

Eric Sosman, Jan 26, 2010
17. ### jacob naviaGuest

Richard Heathfield a écrit :
> jacob navia wrote:
>> Richard Heathfield a écrit :
>>> 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.
>>>
>>> #include <stdio.h>
>>>
>>> int main(void)
>>> {
>>> // get a diagnostic message
>>> puts("Hello, diagnostic message.");
>>> return 0;
>>> }
>>>
>>> When I compile this in conforming mode, I get this output:
>>>
>>> Error E2188 scratch.c 5: Expression syntax in function main
>>> *** 1 errors in Compile ***
>>>

>>
>> Sorry but which "borland C" did you use?

>
> 5.6, in conforming mode.
>

conforming to C 1990. In 2002 Borland did not have a C99 compiler.

Still, it accepts // comments (like all compilers since ages).
And that is what I said. "Unless deliberately crippled" and
in this context you force the compiler to reject // comments by
using a conforming C89 mode.

I will stop here, since you are deliberately confusing people.

jacob navia, Jan 26, 2010
18. ### SeebsGuest

On 2010-01-26, jacob navia <> wrote:
> Richard Heathfield a écrit :
>> Yes. Most C compilers, however, are not C99 compilers.

> This is yet another lie.

No, it's not.

> Please name one compiler that doesn't
> accept // comments by default.

He didn't say they didn't accept // comments by default, he said they
were not C99 compilers.

I think the point he's making is that, for most compilers, there is no mode
where they both genuinely and plausibly claim standards conformance, and
accept // comments, because the only way they could do that would be to
have full C99 support. Since they don't, their C99 mode isn't going to
claim conformance, so you're stuck with their C89 mode, which won't take //

Now, that may not be totally responsive to your point, because it's pretty
easy to get most compilers into a mode where they accept // comments, but
it is true that doing so will often put them into a mode where some other
reasonably normal stuff gets broken.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach /
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Seebs, Jan 27, 2010
19. ### jaysomeGuest

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

[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'

The gcc options I use are specified in a makefile, and look something
like this (the "-Wc," part is particular to the gcc compiler and OS
I'm using, and simply means "pass the following option to the *real*
gcc compiler"):

WARN_BASE=-Wc,-Wall -Wc,-W
WARN_BASE += -Wc,-Wno-unknown-pragmas
WARN_BASE += -Wc,-Wpointer-arith
WARN_BASE += -Wc,-Wwrite-strings
WARN=\$(WARN_BASE)
WARN += -Wc,-pedantic
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.

--
jay

jaysome, Jan 27, 2010
20. ### frankGuest

Richard Bos wrote:
> Andrew Poelstra <> wrote:
>
>
>>> to = from[i+1]; // if there is white space copy
>>> the next value

>> You can't use // comments in most C compilers today,

>
> Wrong (and it was wrong in many cases even before C99 made them
> official), but...

Do you remember the program you used to illustrate this difference? It
basically consisted of a printf statement that would toggle a value to
indicate which decade was definitive. (??)
>
>> and even if you could you shouldn't on Usenet because they wrap around
>> like this and then nobody can compile your code.

>
> ...right, as demonstrated.
>
> Richard

Wrong, as demonstrated.

Heathfield's strarr.h got preprocessed and compiled tonight.
--

frank, Jan 27, 2010