PDA

View Full Version : a website for simple c programs for beginners



Unregistered
02-06-2002, 11:40 AM
Hi,
There is a site which has simple c programs for beginners.
Check the site and let me know what you think about it.
Please feel free to give your feedback about this site.

www.geocities.com/ctechzone

Govtcheez
02-06-2002, 11:46 AM
It's not bad, although I suggest using something more portable than the clrscr() function to clear the screen. It's not included in all compilers.

Prelude
02-06-2002, 12:46 PM
Just browsing the first few I wouldn't recommend this to people just learning the language...or anyone for that matter, it uses both unportable and undefined constructs as well as some bad style.
Here are just a few things I saw:
1. main()
This is nonstandard, the ISO C standard defines main to be declared as either int main( void ) or int main( int argc, char **argv ).
Anything else is just wrong.

2. clrscr()
Not everyone has a compiler that defines this function, conio.h is nonstandard and programs using it that are ported to other compilers will flag errors. Learners shouldn't be thrown into that kind of debugging early on.

3. getch()
See above comment on clrscr(). I don't recommend getch at all unless you write it yourself, getchar() does the same thing and is a standard function so it will work regardless of the compiler or system.

4. fflush( stdin );
This is undefined, anyone who wants to write good programs should think of undefined as illegal. You can't flush stdin, this was stated explicitly in the ISO C standard. A better way to eat the garbage left by scanf if is to either not use scanf or use a loop.
while( getchar() != '\n' );

5. len = strlen( word );
len is declared as an int, strlen returns size_t. This is nitpicking, but truly portable code will declare len as size_t len;

6. gets( word );
Normally I wouldn't even comment on code that uses gets, but I'll make an exception. NEVER use gets, it's been proven to be broken and caused serious problems in the past. gets doesn't check for overflow, you could declare an array of size 20 and enter 500 characters as input and gets will happily write them all to memory that you don't own, possibly crashing your system or worse.

Note that all of these errors were found in a program that is less than 15 lines. If I wrote code like this I wouldn't have a job for very long. On the positive side the logic seems sound, on the writer's particular system I'm sure these work just fine. Please don't consider this flaming btw, consider it as forceful constructive criticism ;)

-Prelude

CoderBob
02-06-2002, 09:38 PM
Why not submit it to the directory on this site?