PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

I am officially boycotting any books published by SAMS.

I recently started using a C book entitled "Absolute Beginner's Guide to C". Apparently, it is the second edition.

The reason I am so mad about this book is simply because this book is FULL of mistakes. Obviously, the author did not intend the reader to type in his code. This is not a compiler problem, either.

Code:

#include <stdio.h>
main()
{
int ctrl = 1;
while (ctr <= 20)
{
printf("%d \n", ctr);
ctr++;

ctr = 1;

do
{
printf("%d \n", ctr);
ctr++;
}
while (ctr <= 20);
return 0;
}


Now what do you see wrong with this? At the end of every chapter, he always has a program. His little programs almost never work. For a complete beginner, this could be quite a hassle.

(By the way, this is an exact quote from the book. I have proofread it many times.)
Yes, it is also my experience that SAMS publishs crap.
Being new to coding, do you often indent after each set of curly braces {}? I was just wondering?

It might have been lost in the copy/paste, but it makes it a lot easier to read. Not to big of a problem here since this is a simple code block though.
Sm!rk · 12 Jul 2003, 21:14 UTC — I am officially boycotting any books published by SAMS.
Definately try indenting, this really helps find very basic logic errors.

One problem is that "ctr" isn't defined, but the fix is easy remove the "l". That's just a syntax error so is found easily. You're also missing a final bracket which is syntax again.

As for other problems, this code should print 0, then print 1 to 20. This is some pretty arcane code and you can really do the same stuff in a much better way. This is definately not a good C book. Try getting Kernighan and Ritchie's book, its the default C bible.

Code:

main()
{
int ctrl = 1;
while (ctr <= 20)
{
printf("%d \n", ctr);
ctr++;

ctr = 1;

do
{
printf("%d \n", ctr);
ctr++;
} while (ctr <= 20);

return 0;
}
Why would it print 0? ctr is set to 1, not 0 (assuming you change the declaration of ctrl to ctr). And yeah, definitely go to another book. I have no idea why he increased the value of ctr and then set it back to 1 the next line...
That code makes no sense to me at all, and I like to think I can atleast follow C code.

ctr is defined as an integer with a starting value of 1. , while it's less than or equal to 20 it prints the value of ctr, then increments it... then sets it back to one?! Then it's going to print its value again, increment again, and then in that same while statement it's going to tell it to return a value of 0 if it's less than or equal to 20? It looks to my elementary knowledge like it's going to keep printing 1 and 0 over and over while never getting to a point where the while value is reached.

Have you actually compiled and run this program? What does it do, you've said what it's supposed to do. That code goes in far too many circles :/
Code:

//Comparison of While and Do-While Structures
main()
{
int ctr = 1;
while (ctr <= 20) // WHILE LOOP
{
printf("%d \n", ctr);
ctr++;
}

ctr = 1;
do // DO LOOP IS LIKE WHILE
{ // Whoopee!!
printf("%d \n", ctr);
ctr++;
}
while (ctr <= 20);

//return 0;
}
I'd say Practical C Programming is probably the best book for learning C, or anything without blatant errors.