LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-04-2006, 02:10 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
Newbie C Question from K&R


I been doing some C coding and have copied the following program from K&R. It compiles OK but doesn't do what it's supposed to - converting uppercase (from STDIN) to lowercase (to STDOUT).

Here's the code,

Code:
#include <stdio.h>
#include <ctype.h>

int main(void)

/* this program converts uppercase to lowercase */

{

int c;
while (( c == getchar()) != EOF)
    putchar(tolower(c));
return 0;
}
 
Old 03-04-2006, 03:34 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There's a typo in this program, double check how 'c' variable is used.
 
Old 03-04-2006, 03:40 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by jlliagre
There's a typo in this program, double check how 'c' variable is used.
i could see a bug only here;
Code:
while (( c == getchar()) != EOF)
should be
Code:
while (( c = getchar()) != EOF)
 
Old 03-04-2006, 04:02 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The original program was undoubtly correct. I'm not expecting Ritchie or Kernighan falling in such a newbie trap and publish buggy examples.
 
Old 03-04-2006, 02:49 PM   #5
gerlano
LQ Newbie
 
Registered: Mar 2006
Location: Dublin
Distribution: Suse 10.0
Posts: 2

Rep: Reputation: 0
Thumbs up C example

If you post the output you get when you run the program against a small sample I may be able to help.



Reading the code you create an unitialised variable c, then in the while statement do a boolean comparison against getchar(), which is then compared to EOF to check for the end of file. At no stage is the value read in by stdin actually assigned to the variable c.

Looking at the code I have to agree that using == to test against the getchar() function will result in unpredictable behaviour. Try changing it as kshkid suggested.

I have run the program through VC7, and on debugging got an uninitialised variable error. Only way this program is going to run is if you change the code to read:

while( (c = getchar()) != EOF)

From experience we all make mistakes in write example code, and it is usually the simple things that get through as we presume they work and don't test.

Last edited by gerlano; 03-04-2006 at 03:19 PM.
 
Old 03-04-2006, 05:17 PM   #6
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kshkid
i could see a bug only here;
Code:
while (( c == getchar()) != EOF)
should be
Code:
while (( c = getchar()) != EOF)
Sorry guys, my bad. I made a mistake exactly right here. It's working fine now.

Sorry once again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Apache newbie question.. (very newbie question) tarballed Linux - Newbie 1 02-07-2003 08:41 PM
Make & launcher newbie question ScreeminChikin Linux - General 2 11-06-2002 12:30 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 12:53 PM

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

All times are GMT -5. The time now is 03:20 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