Departments
|
This is the (ongoing) list of questions and answers I receive about the C language and my book. All questions were originally asked and answered through e-mail -- so don't be shy! Please be sure to check here first to see if your question has been answered. If not, feel free to e-mail me: Windows 2000 users: Please see the Errata page! 1.0 Questions about the book/programming 1.0 Questions About the Book/Programming1.1 C for Dummies, Vol I1.2 C for Dummies, Vol II1.3 C for Dummies, 2nd Edition1.3.1 - Will you be doing a second edition for Volume 2?No. The C for Dummies, 2nd Edition book contains as much of the original Volume I and Volume II as would fit -- which is about 90% of Vol I and 20% of Volume II. The idea behind that book was to keep the same pace as the originals, but I knew that I would not be able to cover the same quantity of material. Therefore the book stops short of what Volume II covered. If you really want a full replacement of both volumes, then get the All-In-One reference, though the pacing there is faster than in the original books. 1.3.2 - Where is page 516?In the middle of Chapter 3 there is a Pop Quiz! where one of the answers is told to be on page 516. There is no page 516. It's a joke. The answers to the quiz should be obvious if you've read the book up to page 35; the "wrong" answers are very, very silly. 1.4 C All-In-One Desktop Reference2.0 General Programming Questions.2.1 scanf() / gets()2.1.1 You don't seem to like scanf(), do you?I dislike scanf(), which is why I didn't use it to input strings for the book. For getting values, it works okay, but when reading strings, scanf() sucks. I use gets() in the book because gets() works. It's not recommended for "real" programs, but for the book it's okay. Just learn to ignore the warning in Linux -- which is legitimate, but okay to overlook while you're learning. Eventually I'll make available the source code to my own input() function, which handily replaces gets() and uses bounds checking to avoid the problem with gets(). 2.1.2 Is gets() really "dangerous"?gets() is dangerous because it does no bounds checking. That means you can easily type in millions of characters, overflowing your storage buffer and overwriting parts of the program. This exploit has been used for years by the Bad Guys to write worms and viruses, hence the warning. When you're ready to write programs for others to use, do not use gets()! For learning, however, it's okay. (I may get in trouble for saying that gets() is "okay for learning," but my opinion is that it's easier to deal with gets() than to fumble over a workaround that causes the student to stumble in the learning process.) 2.1.3 What can I use instead of gets()?Until I publish my booklet on console programming. I suggest using the fgets() function as a temporary solution and replacement for gets(). Here is the format:
buffer is the location of your string storage space or buffer. size is the number of characters to input. This sets a limit on input and therefore it's not "dangerous" as gets() is.
So:
Note that fgets(), unlike gets() also reads in the carriage return (Enter key) at the end of the line. That character becomes part of the string you input. That may screw things up a bit, but it's a simple workaround and avoids the Linux "gets() is dangerous" warning. 2.2 fflush() / fpurge() IssuesI'm bad. I should have never brought up the fflush() and fpurge() functions for clearing the text input stream. While they work, they're not proper programming and I'm advising against using them for now. The real solution to immediate input is to use a library of functions that provide immediate input. For Unix there is the NCurses library, which is simple to learn and quite effective. In Windows, there are functions specific to the compiler as well as a battery of Windows API calls that provide immediate text input. See section 2.2.2 for a good fflush() work-around. 2.2.1 Problem with fpurge() in LinuxI've gotten 2 e-mails so far from Linux users who claim that fpurge() is not available with their version of GCC. This is true; not every release of GCC is the same. In some cases, you might be trying to compile C++ code instead of C, which may cause the problem. One user of Mandrake Linux reported that the fflush() function worked well instead of fpurge(). When you can't get fflush() to work, try using the STDIO extension function __fpurge(). So to fix the problem, try replacing:
with:
Just prefix two underscores before the function name. Try that and see if it works on your compiler. Be aware that __fpurge() is generally considered bad form. While it works, and allows you to use the program, do not consider using it in any of your "real" programming projects. The reason it has the two underscores there is that the function has been deprecated and the compiler developers truly don't want you using that function any longer. 2.2.2 A good fflush() work-around for all compilers/platformsWhen you need to flush the input buffer, don't use fflush() or fpurge() or any of those functions. Instead, use this function, which was provided by a reader and helpful contributor to the (now defunct) Wambooli Forum's C language forum: void jsw_flush ( void )
Use the jsw_flush() function in your code to force all text input to expire and have just what you need remaining in the input queue. Remember to prototype this function when you use it! Just call the function: jsw_flush(); As you would any other function. It neatly despenses with any excss text input and leaves you with the input you want. IMPORTANT! To make jsw_flush() work, remember that getchar() returns an int value. This is easy to get wrong because the function's name is getchar, which makes even experienced programs want to declare its variables as char and not int. 3.0 Compilers & Etc.Note: Remember that my books are specific to the GCC compiler and it's cousins. Borland's BCC and Microsoft's whatever-it-uses were not tested with these books! 3.1 Command line programming3.1.1 Pausing the command line programs in WindowsWhen running many of the book's example programs in Windows, or in any development environment, you'll notice that a command prompt window opens, displays the program, then quickly closes. Unless you run the program at the command prompt, you'll miss its output entirely. To fix the "blinking fast command window" problem, add a getchar() function as the second to last last line in the main() function, just before
The getchar() function waits until a key is pressed on the keyboard. That way, your program will stop and wait, allowing you to examine the text, before it quits. Then just press the Enter key (as an example), to quit the program and close its window. 3.2 It Won't Compile!3.2.1 Undefined reference to `__gxx_personality_v0'The GCC error message referring to 4.0 Other Programming4.1 C++ Programming4.1.1 Is C++ really worth learning?It's not really a question of C++ but really any other programming language. You should approach it just as if you were asking yourself whether Java or Perl or Python were really worth learning. It all depends on the application and whether the programming language offers features that make the job easier to do. My advice is to learn as many programming languages as you can stand. Unlike human languages, programming langauges are easier to pick up. That's because the basic programming concepts translate from language to language. There will be "loops" and "functions" in any language, though they may not be called that. One thing is certain: Knowing C makes learning other languages easier. Many languages, such as C++, use C as their inspiration. In a way, knowing C is like learning Latin, though unlike Latin C is far from a dead language. 4.1.2 How different is C++?C++ improves upon the basic C language in many respects. One book I read on C++ claims that C++ is really about 95% C and 5% orignial stuff. I know other programmers who debate that, but in my experience the basics of C++ and C are pretty much identical. Recently the C standard has been incorporating some C++ features. For example, the 4.1.3 Will you ever write a C++ book?Despite 10 years of requests, publishers are not interested. The fellow who wrote the C++ for Dummies book is locked in and not willing to abandon the title. Even so, if I get enough requests, I may write a separate "Book 8" publication in the style of the C All-In-One Desk Reference for Dummies. That book could be an introduction to the C++ language, published and available separately. If there is enough interest in this thing, I shall create it. Please let me know. 4.2. Other Programming Languages4.2.1. Assembly LanguageFinally, there is an Assembly Language book on the market, one that I can recommend. Alas, I did not write it, but the author did a great job and created an easy-to-follow book. Click here if you're crazy enough. 5.0 Text editors6.0 General6.1 A good language to start learning6.1.1 Is C a good language to learn? What about BASIC?Traditionally, most computer folk would recommend learning how to program by starting with the BASIC programming language. BASIC was common, well-supported, and easy to learn thanks to its English-like vocabulary and syntax. But that was then. I was a big-time BASIC programmer and did several books on the language. Recently "advances" in BASIC, however, have rendered the language ugly and awkward. Years ago, I could recommend BASIC as a great language to learn programming. But today, BASIC is just too non-standard and ugly. Therefore, I recommend C -- and for many reasons. First, C is very common and very standard. You can find freebie C compilers for any computer. They implement a very common and standard version of C, meaning that many of the simple C programs you learn with can be run on a variety of computers. Learning C does not mean you're stuck with it on one computer platform. Second, many other programming languages borrow heavily from C. They include man of the same terms and a lot of the same syntax. When you learn C, then learning these other languages is easy. In a way, C is like Latin; when you learn Latin, then learning other human languages is easier. Well, learn C and learning these other languages is that much easier: C++ (obviously), Perl, Java -- and more are all very similar to C. Finally, C is not a dead language. Despite most program development being done in C++, the base of all those programs is still simple, elegant C. Most of C++ (some say 95%) is still basic C. In fact, if you learn C with my books, you'll easily be able to master C++, just as you can master those other languages with your basic knowledge of C.
Copyright © 1997-2012 by QPBC. |