ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.