Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problems with logic operations within loops

Reply
Thread Tools

problems with logic operations within loops

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-26-2010
Rob Kendrick <(E-Mail Removed)> writes:
> On Tue, 26 Jan 2010 18:20:07 +0100
> jacob navia <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Nick
Guest
Posts: n/a
 
      01-26-2010
Richard Heathfield <(E-Mail Removed)> writes:

> Richard Bos wrote:
>> Andrew Poelstra <(E-Mail Removed)> wrote:
>>
>>
>>>> to[i] = 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
// comments).

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
 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      01-26-2010
On Jan 26, 8:49*am, Tim Streater <(E-Mail Removed)> 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[i];
> * * * 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-26-2010
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.
 
Reply With Quote
 
Michael Angelo Ravera
Guest
Posts: n/a
 
      01-26-2010
On Jan 26, 7:29*am, Interrupt <(E-Mail Removed)> wrote:
> Hi folks Im working though some exercise trying to teach myself C,
> Im having problem with a function Ive written. It takes and array
> and removes the white spaces. *Im having problems with logic
> operations within loops.
>
> My first problem is once If statement executes and i is incremented
> the else statement doesnt 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[i]==' ' || from[i]=='\0' || from[i]=='\t'|| from[i]
> =='\n')
> * * * * {
> * * * * * *to[i] = from[i+1]; * * * // if there is white space copy
> the next value
> * * * * * *i++; //>>>>>>>>>>>>>>>>>>>> problems start here
> * * * * }
>
> * * * else
> * * * * {
> * * * * to[i] = from[i]; * * * * * *// if no white space copy
> * * * * }
> * * }
>
> }
>
> ********************************CODE************** *****************************
>
> So I thought Id approach the problem from the opposite angle if its
> 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[i]!=' '||from[i]!='\0'||from[i]!='\t'||from[i]!='\n')>>>>>>>>executes whether there is white space or not
>
> * * * * {
> * * * * * *to[i] = from[i]; * * * // if no white space
> copy
> * * * * }
>
> * * * * else
> * * * * * * {
> * * * * * * to[i] = 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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-26-2010
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[i]))


IHYM `isspace( (unsigned char) from[i] )'.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-26-2010
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.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-27-2010
On 2010-01-26, jacob navia <(E-Mail Removed)> 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 //
comments.

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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      01-27-2010
On Tue, 26 Jan 2010 17:02:58 +0000, Richard Heathfield
<(E-Mail Removed)> 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,-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.

--
jay
 
Reply With Quote
 
frank
Guest
Posts: n/a
 
      01-27-2010
Richard Bos wrote:
> Andrew Poelstra <(E-Mail Removed)> wrote:
>
>
>>> to[i] = 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.
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Logic Map/Logic Flow Chart. (Example Provided) spike Python 8 02-09-2010 12:31 PM
Asynchronous Logic Gates and Analog Logic Gates Jyoti Ballabh Software 3 11-26-2009 06:48 PM
Successive arithmetic operations within a process dennismccarty VHDL 1 03-15-2009 08:20 AM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM



Advertisments