LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2003, 11:34 PM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
logical operator problem.


Does the line:
Code:
if (c == ' ' || c == '\n' || c = '\t')
right?

Here is the source code I copy from <<the c programming language>>
Code:
     #include <stdio.h>

   #define IN   1  /* inside a word */
   #define OUT  0  /* outside a word */

   /* count lines, words, and characters in input */
   main()
   {
       int c, nl, nw, nc, state;

       state = OUT;
       nl = nw = nc = 0;
       while ((c = getchar()) != EOF) {
           ++nc;
           if (c == '\n')
               ++nl;
           if (c == ' ' || c == '\n' || c = '\t')
               state = OUT;
           else if (state == OUT) {
               state = IN;
               ++nw;
           }
       }
       printf("%d %d %d\n", nl, nw, nc);
   }
however, when I compile it, the compiler said:
"[root@localhost second]# gcc c.c -o c
c.c: In function `main':
c.c:17: invalid lvalue in assignment"

What is wrong with the program?
Thank you.

Last edited by Xiangbuilder; 09-06-2003 at 11:35 PM.
 
Old 09-07-2003, 12:05 AM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Sure it should be (... c == '\t' )
because it wants to ignore white spaces.
Also in my book (second edition), it's corrected.
 
Old 09-07-2003, 12:31 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Sorry for my simple question.
Now I know it is a publishing mistake.
 
Old 09-07-2003, 11:34 AM   #4
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
You're fortunate that you have a compiler that would even warn you about this. Many C compilers wouldn't! This is such a common mistake that many programmers recommend doing:

Code:
if( '\t' == c )
instead of

Code:
if(c == '\t')
so that you guarantee that the compiler will catch the "= instead of ==" mistake. I personally don't follow that advice because it's never been a problem for me, but you can see that even authors of C books can make the mistake! What page is that mistake on? My book is at work, so I can't look it up. I'm surprised that K&R published that mistake!
 
Old 09-07-2003, 03:13 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by eric.r.turner
What page is that mistake on? My book is at work, so I can't look it up. I'm surprised that K&R published that mistake!
In the second edition the mistake is not there, but the program is.
In the second edition it's on page 20, chapter 1, section 1.5 ("Character input and output").
 
Old 09-07-2003, 10:02 PM   #6
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you teach me many things.
I downloaded the book from web and pay nothing for it.
In other words, my action is a kind of software crime.
Perhaps that is why the publishing mistake occur.
My English is poor, perhaps I don't explain my meaning exactly.

Last edited by Xiangbuilder; 09-07-2003 at 10:26 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Operator Precedence C++ Problem rovitotv Programming 9 03-21-2005 10:02 PM
Operator Overloading problem ashwinipahuja Programming 1 06-23-2004 05:59 AM
Script file problem (or operator problem) HELP! lostone Programming 2 05-23-2001 02:30 PM
Script file problem (or operator problem) HELP! lostone Linux - General 1 05-17-2001 06:11 PM
Script file problem (or operator problem) HELP! lostone Linux - Newbie 0 05-15-2001 07:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration