Dec 192017
 
C News #17 -- Newsletter for C programmers.
File CNEWS017.ZIP from The Programmer’s Corner in
Category C Source Code
C News #17 — Newsletter for C programmers.
File Name File Size Zip Size Zip Type
CNEWS017.DOC 31394 9932 deflated
DICE.ZIP 2223 1452 deflated
PEP.ZIP 4448 4301 deflated

Download File CNEWS017.ZIP Here

Contents of the CNEWS017.DOC file



Issue 17 C News 1


*--------------------------------------------------------------*
| C NEWS - International C Electronic Newsletter/Journal |
| "Dedicated to the Art of C Programming" |
| |
| Founded 12/27/87 |
*--------------------------------------------------------------*


Table of Contents

THE HEAP: Messages from the Editor ...................... 2

COMMAND LINE ARGUMENTS IN C by Jim Singleton .............. 4

BOOK REVIEW by Jim Singleton ........................... 6

BEGINNERS CORNER: By Wayne Dernoncourt ................. 8

PEPTO: A SHAREWARE SOURCE CODE COMPRESSOR by Jim Singleton. 13

ARTICLE SUBMISSION STANDARDS ......................... 15

HOW TO GET HOLD OF US HERE AT CNEWS........................ 16



"C News" is an Electronic Journal published by the C BBS in
Burke, VA on a monthly basis. The subject for C News is the C
programming language, as well as any derivatives like C++.

All readers are encouraged to submit articles, reviews, or
comments for submission. C News is freely distributed, but can
not be sold for a profit, or cannot have a charge assessed to
cover distribution costs. All articles, and reviews become the
property of C News and cannot be included in other
publications without written permission from the C News
Editorial Staff. To do so is in direct violation of the C News
License agreement. Copies of which are available from the C
BBS. This publication is Copyrighted under U.S. Copyright
Law.



















Issue 17 C News 2


=================================================================
THE HEAP: Messages from the Editor
=================================================================



Welcome to another issue of C News. Jim, Dan, and Wayne
have worked hard to put this issue together, and deserve a round
of applause for their fine efforts.

A couple of weeks ago, I placed a message in the Fidonet C
Echo asking for ideas on articles for C News. The response was
excellent. The C News staff and I will be reviewing the ideas
that have been submitted and determine which subjects we can
cover and in what issue they will appear. As always, you are
encouraged to submit ideas to the C BBS, via postal mail or MCI
mail.

One area that we will begin a series on in the next issue
is sort and search algorithms, to include: Quicksort,
Bubblesort, B-Trees, and Linked Lists. Each algorithm will be
presented in theory, pseudocode and a 'c' code example.

Also, if time permits in the next few months, I will begin
a beginners column in MS Windows programming. I would also like
to cover OS/2 programming as the two are closely related.
However the cost of the OS/2 development kit prohibits me from
exploring the latter. Maybe in time, Microsoft may decide to
lower the cost to something reasonable.

Sometime later in the week, I will be sending all of the C
News issues to date to the "C Users Group" for inclusion in
their library. I'll let you know whether CUG accepts the disks
or not in the next issue of C News.

C News also has a new "European" C News BBS and Editor.
The newcomer to the C News staff is Rob Koel, a young computer
professional in Holland. Rob runs a BBS in holland that has
it's own C News section which contains all of the issues of C
News, and a message section where you can leave messages to us.
Rob will pack of the message sand forward them to us here in the
US. As soon as Rob, let's me know his BBS number I will post it
here in this column.

Finally, Jim Singleton represented C News at the East Coast
SOG in York, PA this month. Jim presented C News and discussed
the future of the newsletter and solicited thoughts and ideas on
C News. Jim and I have not had a chance to talk since the SOG,
I will fill you in on the details in the next issue.

Thanks for the continued support, and we look forward to a
continued relationship with our readers and friends around the





Issue 17 C News 3


globe. Keep those postcards coming!!!

Barry Lynch

Editor - C News




















































Issue 17 C News 4


=================================================================
COMMAND LINE ARGUMENTS IN C by Jim Singleton
=================================================================



I was looking through a C text the other day (see the
review of "The Spirit of C: An Introduction to Modern
Programming" elsewhere in this issue) and I noticed that the
very last topic which was covered was passing command-line
parameters. I admit that passing command-line parameters is
probably not one of the first topics which a C programmer should
learn. After all, how many parameters do you need to pass to
print "Hello world"? At the same time, I can see an argument
for covering the topic a bit earlier, perhaps when discussing
functions. It's really just a special case of passing a
parameter to a function -- which happens to be main(). Anyway,
if you've ever wondered how to pass a parameter on the
command-line, just read on.

In order to pass a parameter to the program, main() must be
defined as having two arguments, as shown:

void main(int argc, char *argv[])
{
. . .
}

Where argc is an integer value representing the number of
arguments typed on the command-line. The second argument,
argv[], is an array of character pointers. While these
arguments may also be declared as:

void main(argc, argv)
int argc;
char **argv;
{
}

these arguments must be declared as the type shown.

The first value of this array of character pointers,
argv[0], is always the name of the program. For example, let's
assume we have a program called READ and it's executed, in DOS,
as:

C> read [filename]


We would enter:

C> read CNEWS017.NWS






Issue 17 C News 5


In this example, argc has a value of 2 (for the program and the
filename), argv[0] is "read" and argv[1] is "CNEWS017.NWS". If
we were to enter:

C> read CNEWS017.NWS ASCII

we would find:

argc = 3
argv[0] == "read"
argv[1] == "CNEWS017.NWS"
argv[2] == "ASCII"

You may have noticed that the arguments passed are always
stored as character strings. If we have:

C> read CNEWS017.NWS 2

We find that argv[2] is a pointer to the character string "2".
In order to use this value as a number, the program must convert
the string. This is easily accomplished by using functions such
as atof(), atoi(), etc.

The program DICE.C is an example of a program which
requires passing a parameter upon execution. (Two versions of
DICE.C, one for Turbo C and one for Microsoft C/Quick C, are
contained in the accompanying file DICE.LZH.) As the program
simulates the rolling of a user-specified number of dice, the
number of the dice is entered at the time of execution. In this
example, the number of dice, which is stored as a character
string, must be converted to an integer, which is done using
atoi(). DICE.C also shows how the value of argc can be used to
determine that the correct number of parameters has been
entered.

While we have seen that passing parameters to a program
upon execution isn't hard, don't get carried away with command-
line arguments. We wouldn't want something like:

C> read CNEWS017.NWS -ascii -double-spaced -odd-pages
-titled -two-per-screen

would we? (Okay, okay, if we were using Berkeley Unix we would,
but otherwise, we would not.) Besides, the more parameters that
can be passed, the more complicated the program is for the
user.











Issue 17 C News 6


=================================================================
BOOK REVIEW by Jim Singleton
=================================================================


Title: The Spirit of C
An Introduction to Modern Programming (Student Copy)

Author/s: Henry Mullish and Herbert L. Cooper

Publisher: West Publishing Company
50 West Kellog Boulevard
P. O. Box 64526
Saint Paul, MN 55164-1003

ISBN #: 0-314-28500-8

Unlike most of the books which have been reviewed in the C
NEWS, "The Spirit of C An Introduction to Modern Programming" is
somewhat different because it is a textbook. (The only other
books reviewed in the C NEWS which I would consider textbooks
might be Thomas Plum's "Learning to Program in C" and "Reliable
Data Structures in C".) Before you say, "Oh, it's a textbook, it
must be boring" and stop reading this review, hold on a minute.
Of all the textbooks I own, from all the classes I've taken,
this is the only one which is usually within reach. I say
"usually" because I normally keep it at work, as one of my two C
references. (There are other C books, a number of them, sitting
above my computer at home.)

One thing, which I have to point out, that separates "The
Spirit of C" from all other C books I have read or looked
through is the first example program. Rather than the normal
"Hello world" program, the first program is an interactive
program to calculate the hypotenuse of a right triangle. Had
this been the first C program I had ever seen, I'd have probably
gone back to whatever language I was using before C! Luckily,
the first program for the reader is asked to enter is much
simpler.

"The Spirit of C" is a very readable text. It's not a book
I might take to the beach (then again, I have taken programming
books on vacation, so...); but it is a lot less dry than most
textbooks, especially the textbook I learned FORTRAN from. The
sixteen chapters are well organized and filled with example
programs. Virtually every example program has an example of its
output immediately following the program.

Each chapter is divided into four parts. The first part
goes into the topics which are covered in that particular
chapter. The second part is titled, "The Spirit of C" and is
used to illustrate the material covered in the chapter by





Issue 17 C News 7


demonstrating the solution of a real life programming problem.
Also included in this section are discussions of programming
style, debugging (starting with chapter 5), and other related
topics. The third section of each chapter is a series of
questions concerning the information presented in the chapter,
followed by the answers. The fourth and final section of each
chapter is a number of programming exercises. Unfortunately,
the solutions to these programming problems are not in the text,
but are included in the instructors edition.

At the end of the book, where the answers to the
programming problems should be, are six appendices: a C
reference guide, common library functions, ASCII character
codes, a discussion of UNIX, a listing of keywords, and a guide
to the programming debugging hints presented elsewhere in the
book. The appendices are followed by a good index.

Easy to read and loaded with examples, "The Spirit of C"
sounds like the ideal book to learn C from. While I am sure you
could learn to program from this book, without an instructor
(look at the people who have learned C from K&R), I wouldn't
recommend it as a first book. Why? That's hard to say. It
wasn't the first book on C I had read, but when I spoke to some
students who were using it in a class, who had not been exposed
to C, almost all had reservations about it and had gotten some
other introductory C book. Another strike against it is that it
is rather hard to find, especially outside of a college
bookstore.





























Issue 17 C News 8


=================================================================
BEGINNERS CORNER: By Wayne Dernoncourt
=================================================================



In the last article we discussed the concept of passing
back multiple pieces of data from a function. As part of this
discussion the concept of an address was introduced, along with
reading and writing data at this address. This was all
discussed along with scope - that is, how long data is good
for. This month we're going to step back and look at how
functions that are supplied with the operating system pass data
back and forth (this will be done primarily through the use of
file system calls). Next month we're going to continue to look
at how file I/O is performed. The month after that we're going
to discuss writing and using your own data structures.

One of the most elementary things that a program does is
create a file. That's one item that we haven't done yet. This
will be remedied in this article.

To start off with, the operating system (most operating
systems, anyway) needs some information before it can work with
files. For example, the operating system has to know the name
and location (i.e. the pathname) of the file, and it has to
know how you're going to call it (i.e. which one of several
files or printers that you're currently using - which one do you
want to use). Also you should tell it whether you're going to
read the file or write the file and whether you want to treat
the file as a text file or as a binary file. All of this
information is usually specified in the 'open' statement. For
our first example we are going to come up with a program to fill
the following need. A user has a file of a scanned (digitized)
drawing stored in the computer in a known format (i.e.
AutoCad). However, the scanner software didn't block the file
into the needed 80 character records, it left it as one record
that was 350,000 characters long. The requirement is to read in
the original file and insert a new line character after every 80
characters. In our first example, instead of an 'open'
statement, we're going to use an 'fopen' statement. It performs
the same function, but is specifically intended for buffered
file I/O.

The following pseudo-code is designed to fill this need.
As the name implies, pseudo-code isn't real program code. There
is more English, but it doesn't have all of the control
structures that will be needed. But it serves as a stepping
stone between the problem statement and definition and the
program source code.







Issue 17 C News 9


/* This program is to demonstrate how to use files */
/*
Open an input file <-on error quit->
open an output file <-on error quit->
initialize a counter to zero that will track the number of
characters going into the output for each record
until you hit the end of file (EOF), do the following:
read a character
write a character
increment counter
if counter is greater than 79 then
write a new line
reset counter to zero
end-if
end-until

This is the C source code that has been written from the
pseudo-code above. Notice that there are some changes between
the two, but that on the whole the program follows the
pseudo-code.

#include

main()
{
int char_cnt = 0; /* This is our increment counter */
char in_char; /* This is the character variable */
FILE *in, *out; /* Set up pointers to file data structures */

if ((in = fopen("notes.log", "rt")) == NULL)
{
fprintf (stderr, "Oops, where is input file at?\n");
return(1);
}


if ((out = fopen("notes.new", "wt")) == NULL)
{
fprintf (stderr, "Oops, how about the output file");
return(1);
}

while (!feof(in))
{
in_char = fgetc(in);
fputc (in_char, out);
char_cnt++;
if (char_cnt > 79)
{
fputc('\n', out);
char_cnt = 0;
}
}





Issue 17 C News 10


fclose(in);
fclose(out);
}

The 'fopen' call takes the information from the function
arguments and creates a data structure of type FILE. Some of
this information is supplied by the operating system when the
'fopen' call is used. Examples of the information stored in the
data structure include the file status (read only, write only,
read write, etc.), the error status, a file descriptor, buffer
size (the buffer size on a floppy is usually smaller than that
of a hard disk), the address of the data transfer buffer and a
currently active pointer.

The values returned by the 'fopen' calls are pointers to
the memory locations where the data structures reside. These
are assigned to the variables 'in' and 'out'. These variables
are then checked for equality to 'NULL'. NULL is a macro
defined in the stdio.h header file that is the value returned by
'fopen' when an error has occurred. In many C implementations
this macro is a synonym for 0, but portable code should not rely
on this as it is not always true.

Anyway, if the 'in' and 'out' pointer variables aren't
NULL, this implies that the files could be opened. More
specifically, the first file existed and could be opened and
that the second file could be created. The double equal sign in
the 'if' statement is how the C language denotes the equality
condition check, i.e. make sure that the variable 'in' is equal
to 'NULL'. If either variable is equal to NULL, print out an
error message and stop the program. You may have noticed that
three operations actually take place in these first two 'if'
statements:

1. A file is opened for either reading or writing
2. An assignment to a pointer variable is made
3. The pointer variable is checked for validity

The next thing that may strike you as a bit odd is the
expression:

while (!feof(in))

What this expression means is that while the expression in
the parentheses is true, do whatever follows the expression. In
this particular case, feof(in) is a macro to determine whether
or not the end of the file has been reached, the value of the
expression is true if the end of the file has been reached,
otherwise it's false. The leading exclamation mark negates the
expression that follows. So what this whole line means in
english is "do the following until I hit the end of the input
file".





Issue 17 C News 11



The 'fgetc' function gets a character from the input file
(this is singular, not plural) and assigns it to the variable
'in_char'. The same character is then written to the output
file and the number of characters written to that output record
is then incremented by one.

Let's talk about this last operation in a little bit more
detail. Back in the second issue of this column, I talked about
the following operations:

1. +=
2. -=
3. /=
4. *=

It turns out there are another class of operators called
the increment/decrement operators. These operate just as you
might imagine, they either increment or decrement the variable
being operated on. The only variables that this makes any sense
on are integers and maybe characters. In the case of the
increment operators, the variable is incremented by one, or:

i = i + 1;

is the same as

i++;

The only difference between the increment and decrement
operators is that instead of adding one to the variable, one is
subtracted from the variable. These operators can be extremely
useful. For example, consider the following code fragment:

int i,j,k;
i = 1;
j = i++;
k = --i;
printf ("%d %d %d\n", i, j, k);

What would be printed on the screen? The number '1' three
times. In this case, the pre-increment and post-decrement
operators have two stages, the assignment phase and then either
the increment or decrement phase. Initially the variable 'i' is
assigned the value of 1. The variable 'j' is then assigned
whatever value is held by the variable 'i', then the variable
'i' is incremented (notice the plus signs after the variable,
this is called a post-increment operator). Then the variable
'i' is decremented back to 1 by the next line (notice the minus
signs before the variable, this is called a pre-decrement
operator), then the variable 'k' is assigned the value of the
variable 'i'. There are also pre-increment (++i) and





Issue 17 C News 12


post-decrement operators (i--). Obviously, you can use any
legitimate variable that you want.

Now that we've discussed the increment/decrement operators
briefly, let's get back to the discussion of this month's
program. After the counter is incremented, check to see if it
is greater than 79, if it is, put a new line out to the file and
reset the counter to zero, then go back to reading the input
file.

The files are usually automatically closed upon exit, but
to be neat, we will close them manually with the 'fclose'
function.

This closes out this edition of my column. The whole point
of this column is to demonstrate that a lot of data (in this
case the data required for file use) can be passed among
functions with the programmer just having to make allowances for
it's use, and not be real concerned with how it works. In next
months column, we'll discuss file I/O some more and pointers.





































Issue 17 C News 13


=================================================================
PEPTO: A SHAREWARE SOURCE CODE COMPRESSOR by Jim Singleton
=================================================================



Recently, I was listing the new files on the C BBS and
noticed the following:

PEPTO1.ARC 14208 08-07-89 Odd Utility of the month!
Compress C Src code for Compiling

I decided to see exactly what this program did that qualified it
as the "Odd Utility of the month!" (Not to mention that the
name of the program was enough to grab my attention.) I guess
I've always had a thing for odd and interesting little utilities
and programs. So, I went ahead and downloaded PEPTO1.ARC.

Upon unarchiving PEPTO1.ARC, I found myself with three
files:

PEPTO.C,
PEPTO.EXE, and
PEPTO.TXT,

the source, executable, and text files for PEPTO.

What does PEPTO do? Well, PEPTO is designed to shrink
source code and header files for C programs. Why? To reduce
space requirements and decrease compile times by compressing the
system header files. The author also states that PEPTO can be
used for distribution of secure copies of source code by
changing major variable names and compressing the source.

PEPTO is easy to use. The commands can be entered on the
command line individually:

PEPTO DICE.C,

using wildcards:

PEPTO *.H

There is also a feature, /C, which prevents the lines in the
source file from being concatenated:

PEPTO /C HELLO.C

This last feature can be useful if the C compiler you use does
not like long command lines. After a file has been compressed,
the compressed version has the extension *.PEP.






Issue 17 C News 14


What does a program that has been compressed with PEPTO
look like? The file PEP.LZH, included with this issue of the C
News, gives a couple of examples. The program DICE.C, which was
used in the command line parameters article was compressed form
1,768 bytes to 433 bytes. The compressed file, DICE.PEP, can be
compared to the original file, DICE.C. With a small program
like this, it is easy to look at the compressed version and see
what the program is supposed to do; however, in a larger, more
complex, program it is not as easy. For example, I took the
program PROGRAM8.C, which I happened to have handy and
compressed it with PEPTO. The resulting file is just under half
as large as the original, not the 75.5% reduction I'd seen with
DICE.C, but DICE.C was mainly comments. (NOTE: Just stripping
the comments and left justifying the code in DICE.C produces a
file of 533 bytes.) PROGRAM8.PEP is hard to read because not
only are the comments missing, but there is no real structure to
the program.

Like the file description said, this an odd utility. As
with any program, there are a couple of things to remember when
using PEPTO. PEPTO will not accept *.PEP files as input. PEPTO
only understands inline assembler lines which begin with "asm",
don't use a command such as "#define A asm".

The author, "created it for my own use (necessity is a
mother!) but it is something which can benefit others so I have
decided to release it as shareware." As such, a five dollar
donation is requested if you find PEPTO useful.

More information on PEPTO can be obtained from the author:

David Stafford
Research and Development
1800 Green Hills Rd.
Scotts Valley, CA 95066

CIS: 72411,2670
MCI mail: DSTAFFORD.



















Issue 17 C News 15


=================================================================
ARTICLE SUBMISSION STANDARDS
=================================================================




All articles, reviews and letters to editor should be
submitted in a ASCII formatted file. Please use 0-65 margins,
and single space. Do not format the text in anyway, as I use
Proff to format C News. Proff takes care of the justification,
footnotes and headers.


You can send in your article on a diskette to the C BBS, or
upload it to the C BBS. See "How to Contact us here at C News"
for more information.

Barry






































Issue 17 C News 16


=================================================================
HOW TO GET HOLD OF US HERE AT CNEWS
=================================================================




The primary address for C News is as follows:

C News
% BCL Limited
P.O. Box 9162
McLean, VA 22102
USA

The primary electronic address for C News is the C BBS:

C BBS
1-703-644-6478
2400,8,N,1 23hrs a day.
1:109/307 in Fidonet.


The secondary electronic address for C News is:

MCI Mail: BCL Limited


Barry





























 December 19, 2017  Add comments

Leave a Reply