LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-03-2014, 09:37 PM   #1
G_A
LQ Newbie
 
Registered: Jun 2014
Posts: 29

Rep: Reputation: Disabled
Can't make a simple C program work..


Hello all,

I'm reading the K&R C for the first time(meaning, I went went through it w/o too much attention, but this time I aim to actually finish it..)

Though I'm not a *NIX newbie, I am at a loss, I cannot make even this simple program work.
"Exercise 1-8. Write a program to count blanks, tabs, and newlines." , Okay, so I just started with counting the tabs, aiming low.
The program behaves erratically, at times it will print "hello", and at times, not : - regardless of if a TAB char was actually entered.
I tried all sorts of combinations for the if() statement, but, none worked for me.
I read about printf() as much a I could, but, still, cannot figure out exactly how it behaves.
Thank you in advance for your input.

Code:
#include <stdio.h>

main()
{

char t;
t=getchar();
        while(t != EOF)
        {
                if( t == '\t' && '\n')
                {
                printf("hello");
                t=getchar();
                }

        }
}
 
Old 06-03-2014, 09:57 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

I think the main problem here is that you need to call t=getchar(), every iteration of the loop, not just if the if statement succeeds.

Evo2.
 
2 members found this post helpful.
Old 06-03-2014, 10:07 PM   #3
G_A
LQ Newbie
 
Registered: Jun 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Resolved

Quote:
Originally Posted by evo2 View Post
Hi,

I think the main problem here is that you need to call t=getchar(), every iteration of the loop, not just if the if statement succeeds.

Evo2.
Thank you Evo2!
That did it. I carried the placement of the t=getchar() from a previous program, and did not pay any attention to its location, only messed with the if() statement, which proved to be a mistake.
 
Old 06-04-2014, 02:39 AM   #4
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
That if statement looks wrong. Firstly, you're testing whether "\n" is true (i.e. on the right-hand side of your and operation). That's likely not what you want - you probably meant t == "\n". Secondly, since a character can't be both a tab and a new line, that test would always fail. You probably meant to check whether one or the other was true (i.e. t == "\t" || t == "\n").
 
Old 06-04-2014, 03:04 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
You mean: t=='\t' || t=='\n'

Note: 't' should be an 'int' not a 'char', otherwise character '\xff' will be treated as EOF (or vice versa, depending of signedness of 'char' type)
 
1 members found this post helpful.
Old 06-04-2014, 06:16 AM   #6
G_A
LQ Newbie
 
Registered: Jun 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
You mean: t=='\t' || t=='\n'

Note: 't' should be an 'int' not a 'char', otherwise character '\xff' will be treated as EOF (or vice versa, depending of signedness of 'char' type)
I see,
Thank you.
BTW, does getchar() hold only one char at a time ? or can it hold a string, as well ?
 
Old 06-04-2014, 06:45 AM   #7
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
Quote:
Originally Posted by G_A View Post
BTW, does getchar() hold only one char at a time ? or can it hold a string, as well ?
The name of the function tells you all you need to know - getchar() returns only a single character.
 
Old 06-10-2014, 02:10 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Move the t=getchar() into the while loop and get rid of the one in the if()
Keep in mind getchar() is (annoyingly) buffered which means if your input source is the keyboard, then it will not recognize the input unless you hit enter or type a whole lot
 
Old 06-11-2014, 07:54 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,876
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Other's have pointed out the errors in the original IF statement. That's really the biggest one.

Suggestions are that you find ways to get information about each library call and understand what range of behaviors to expect from them; those being: getchar(), and printf(). I find the linux.die.net manual pages to be very common ones I encounter when I search for thing like "getchar man page". However also, you can just type:
Code:
man getchar
in your Linux terminal a.k.a. Linux command prompt.

What you'll notice is the following:
  1. getchar() requires stdio.h
  2. getchar() returns an int however it "interprets" whatever it read as an unsigned char and casts that to an int in order to return it to the calling function
  3. getchar() does read from stdin and is the equivalent of getc(stdin)
  4. getchar() will return EOF at end of file or on error
What I don't see in either the online variation or my local man page is what possible ERRNO values you could encounter if getchar() did encounter an error; however a number of other library calls will indicate possible ERRNO values and their meaning.

printf() is pretty encompassing, as is scanf() therefore I suggest experimentation as well as investigation about the behavior of the function. Further, you could use printf() to output the value of "t" just after you take it in via getchar()
Code:
t = getchar();
printf("Character was: %d(%#02x)\n", t, t);
What does that do for you? It prints out the character as it was typed, and if it was carriage return, newline, or backspace then you'll see much of nothing; however it next prints out the character in HEX, the # makes it use "0x" to lead it off, the "02" makes it always a 2-digit printout; those are merely my preferences when printing an ASCII character in HEX. You can check the character with an ASCII table, like An ASCII Online Reference I use. Following this technique and doing excessive output as your program progresses will give you insight as to what you may have done wrong, misunderstood, or just general information to validate that steps are working as you expected they would. I think what you may be seeing is that there are a number of possible things you could type which affect that program's operation. Like if you type ESCAPE, SPACE, ENTER, CTRL-B, and so forth.

Other suggestions, especially if you're using C in the Linux command line and learning C programming is to learn how to use GDB and learn compile options for GCC.

GDB is the GNU debugger and you can basically use that to run your program, set a breakpoint, step through your program and examine data as it changes. It's very helpful, especially as your programming sophistication grows.

GCC is the compiler and there are plenty of options to consider when you compile a program; maybe even consider creating a Makefile so that you don't have to type lengthy arguments all the time. What I'm talking about here are enabling switches in your compile to detect warnings, such as -Wall. If you look at GCC you'll see that there's a lot included as well as a lot not included in the -Wall switch. I continually grow my list of compiler switches and look to have no warnings in my compilations. Warnings are an indication that you've gone slightly, or fully out of bounds or scope for acceptable programming; heed them. But note that if you don't put in any switches to your compile, then you'll see very few real compile complaints excepting severe errors.
 
1 members found this post helpful.
  


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
Why does this simple Java program work correctly under Windows but not Fedora 10? arashi256 Linux - Newbie 7 09-05-2014 08:22 PM
[SOLVED] what program would you use to make a simple floor plan drawing - drafting rmknox Linux - Software 2 02-25-2013 04:53 PM
[SOLVED] Simple syntax that I fail to make work, argh this is irritating. Any help please ? :) Sabinou Linux - Newbie 8 04-26-2012 04:16 PM
[SOLVED] Need a simple graphic program to make some patterns for cutting Dafydd Linux - General 4 12-05-2011 10:22 AM
I want to make a simple curosr program using C please help thinice15 Programming 9 04-24-2008 10:47 AM

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

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