English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 25 November 2016, 18:09   #1
kgc210
Registered User
 
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
Need Help - The C Programming Language 2nd ed K&R Book

I just bought the book and I have started to read through it.

For the time being I am using vbcc on WINUAE but it's setup the same as my 90mhz 060 A4000 (BetterWB).

I will start compiling on my A4000 shortly but I am really scratching my head so soon into this book.

I did the temp conversions really ok reversing the Fahrenheit to Celsius code ok and adding in headers to describe what the columns were. I thought I was an 31337 hacker hahaha

I am now on section 1.5.1 File Copying
I understand the concept that what I type into the shell is displayed. But I cant get my head around exercises:

Exercise 1-6. Verify that the expression getchar () I= EOF is 0 or 1.
Exercise 1-7. Write a program to print the value of EOF.

The given code is:

#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}


I have had a google and I am not the only one confused, there is a post on Stackoverflow but that didn't help much either
I know I must trigger an EOF but don't know how and would it display EOF or just exit the program?

Thanks in advance for any help given.
kgc210 is offline  
Old 25 November 2016, 18:55   #2
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Ok, let's start with the keyboard. "Ctrl \" I think is what sends EOF in Amiga Shell. "Ctrl d" is on linux. So the key sequence varies from OS to OS.

But you can redirect input from a file. Say you have a file foo.txt (make one, write a sentence in it and save it). Say your executable is called filecopy. Then you can do

filecopy <foo.txt

That will read the foo.txt and when the last byte has been read then the next call in getchar() will return EOF.

Now, onto your last question. The snippet you posted will just exit when EOF is seen.

EOF is really an integer. If you want to print it's value, try this:
printf("EOF's value is %d\n", EOF);
alkis is offline  
Old 26 November 2016, 09:40   #3
kgc210
Registered User
 
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
Thanks for the help.
I couldn't get filecopy to work kept getting: unknown identifier <filecopy>

I wasn't far off printing EOF then I was missing the "%d" part.

I noticed a few odd things when you type in special characters.
If i typed in say "@blablabla" and pressed Return (minus the quotes)
It would display:
@blablabla
@blablabla
and the cursor would be hear. @ seems to repeat everything on a line.

If I did "Ctrl 2" I would get an inverted @ and when pressing Return the cursor would drop down two lines.

If I try "Ctrl 2" on my PC I either switch tab on a browser and If I try to do it in say notepad nothing is displayed.

Last edited by kgc210; 26 November 2016 at 09:45.
kgc210 is offline  
Old 26 November 2016, 16:41   #4
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by kgc210 View Post
I couldn't get filecopy to work kept getting: unknown identifier <filecopy>
This was just an example. Alkis thought you would call your excercise program "filecopy". Replace "filecopy" with the actual program name.

Quote:
If i typed in say "@blablabla" and pressed Return (minus the quotes)
It would display:
@blablabla
@blablabla
and the cursor would be hear. @ seems to repeat everything on a line.
No. I doubt that. And I bet it also works with other characters.

You have to know that getchar() reads characters from the file stdin, and stdin is usually the console, as long as you don't redirect it with "<" (see above).

The console is not a normal file, like a file on disk. It is an interactive TTY, which is line-buffered. So your getchar() call is blocked until another line-buffer is complete, which happens after you pressed the Return key. Then it returns all typed characters from that line, one after another, until the '\n' Character (decimal 10 = Newline). Then it blocks again, waiting for the next line-buffer.

From the two lines you are seeing the first line is the echo of your typed characters in the shell (your program is still blocked in the first getchar() call). The second line is actually generated by your program with getchar()/putchar().


Quote:
If I did "Ctrl 2" I would get an inverted @ and when pressing Return the cursor would drop down two lines.
This depends on the system and the shell being used. On a standard AmigaOS 3.1 shell I don't see that.
phx is offline  
Old 26 November 2016, 18:06   #5
kgc210
Registered User
 
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
I am sorry but I can't figure out how to get getchar() to read <foo.txt

I have tried it in the brackets out side them. Deleted the whole lot and just had <foo.txt

Not sure if to carry on reading this book or look for a more simple "Retards guide to C" book
kgc210 is offline  
Old 26 November 2016, 18:40   #6
Locutus
Registered User
 
Join Date: Jul 2014
Location: Finland
Posts: 1,176
the stdin redirection suggestion is more a native unix thing, i'm not sure it actually works on AmigaOS' shell.
Locutus is offline  
Old 26 November 2016, 21:24   #7
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by Locutus View Post
the stdin redirection suggestion is more a native unix thing, i'm not sure it actually works on AmigaOS' shell.
I am sure
alkis is offline  
Old 26 November 2016, 21:25   #8
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by kgc210 View Post
I am sorry but I can't figure out how to get getchar() to read <foo.txt

I have tried it in the brackets out side them. Deleted the whole lot and just had <foo.txt

Not sure if to carry on reading this book or look for a more simple "Retards guide to C" book
what's the name of your executable that is produced after you compile the example source code?
alkis is offline  
Old 27 November 2016, 01:44   #9
kgc210
Registered User
 
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
Right I get it now, I have to type the <foo.txt into the shell.

I thought I was trying to get "getchar()" to read it with in the program it's self.
kgc210 is offline  
Old 27 November 2016, 06:18   #10
kgc210
Registered User
 
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
Well I'm onto Exercise 1-8
Quote:
Write a program to count blanks, tabs and newlines.
I had a look at an ASCII table and found I needed to add \s and \t to my code.

When I tried \s (also tried escape \e) I get the error "illegal escape-sequence in string" when trying to compile the code.

Is this due to vbcc blocking \s or something Amiga specific?

Anyway I changed \s to backspace \\ and got it to compile.

#include <stdio.h>

main ()
{
int c, nl, tb, bs;

nl = 0;
tb = 0;
bs = 0;

while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
if (c == '\t')
++tb;
if (c == '\\')
++bs;

printf("%d\t%d\t%d\n", nl, tb, bs);
}


It counts newlines ok but if I press tab or backspace it doesn't record them.
I also created a txt file with ed to have some new lines and tabs in, but again it didn't see them.
I also tried to write \\, \t in the txt file.

Is this due to the way Amiga handles tab and backspace? Or because ed can't encode them in a file?

On the attached image you can see where I tried to press Tab in the shell, it displayed inverted I

Thanks Will
Attached Thumbnails
Click image for larger version

Name:	count18v3.jpg
Views:	244
Size:	92.1 KB
ID:	51052  
kgc210 is offline  
Old 27 November 2016, 10:59   #11
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Ok, lets break it down.

Quote:
Write a program to count blanks, tabs and newlines.
blanks are space " "
tabs are \t
newlines are \n

Backspace is not blank, so you should not count it. Even if you needed to count backspace you couldn't, because backspace doesn't leave a char behind, it just erases a char left from cursor.

Also, the '\\' is not backspace is the char '\' (backslash)

The backspace is '\b', which again you'll never be able to get. (Technicaly you could if you put the console in raw mode, but that's way advanced for this exercise)

Bottom line, if you changed the
if (c == '\\')
to
if (c == ' ')

then I think you'll cover what the exercise asked you to do.

Emm, just saw the screenshot. You forgot the braces in your while. Enclose all ifs in {} like

Code:
while(...) {
   if...
   if...
   if...
}
alkis is offline  
Old 27 November 2016, 11:21   #12
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by kgc210 View Post
When I tried \s (also tried escape \e) I get the error "illegal escape-sequence in string" when trying to compile the code.

Is this due to vbcc blocking \s or something Amiga specific?
\s is no legal ANSI-C or ISO-C escape code. Here is an overview of all the legal ones:
https://en.wikipedia.org/wiki/Escape_sequences_in_C
phx is offline  
Old 27 November 2016, 23:01   #13
kgc210
Registered User
 
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
Thanks alkis and phx I owe you both a beer.

I have just rebuilt my WINUAE setup on the laptop, I forgot to copy the hard file before going on my travels to Rheda-Wiedenbruk

I will give the coding a go tomorrow evening when I arrive, I have the C book in the car.

I will keep you both posted.

Do you think all this ASCII stuff and Escape sequences were better known when the book was written or do the authors assume too much with their strong UNIX background?
kgc210 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
What programming language to write a commodity? Sim085 support.Other 18 02 April 2015 23:56
WTB: Abacus Book - Amiga Machine Language christopherpm MarketPlace 2 08 July 2013 22:07
Amiga C programming language Kenan support.Apps 3 25 June 2013 18:50
Graphics Programming Black Book Amiga Forever Coders. Language 2 20 April 2012 07:23
Most common programming language in Amiga games manicx Amiga scene 38 10 March 2004 14:20

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

Forum Jump


All times are GMT +2. The time now is 11:38.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.37669 seconds with 16 queries