Date: Sat, 31 Jul 93 17:13:11 JST From: Stephen Turnbull To: shipmanm AT cs DOT arizona DOT edu Cc: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: Hello program Your program is as follows: >>Date: Fri, 30 Jul 93 21:50:40 -0700 >>From: Michael S Shipman >> >> >> >>HI ! >>I am back! I am sorry to bother you, but I am getting closer to my >>C-compiler for msdos. >> >>I want to first thank aeppert AT ideanet DOT doe DOT state DOT in DOT us for answering my >>last question. >> >>The problem: >>When I compiled hello.c, the compiler liked it. (gcc hello.c -o hello, >>I then typed aout2exe hello) >> >>Here is my program: >> >>#include >>main() { >> >>int n; >> >> printf("Hello, world!\n"); >> >> for (n = 0; n < 2; n++) >> printf("HI!\n"); >> >> scanf("%d", n); ^^^ This is a typical mysterious C error. scanf needs to know *where* to put the number. So you need &n which is the *address* of the variable n. What you put is n, and the compiler automatically goes to the address &n and gets the *value* there, which is probably either 0 or something random. It then uses this as an address to put the number you type at. "Segmentation error" means that you tried to get the value at an illegal address. Even experts make this mistake on occasion. It is easy to make, because printf is asymmetric: it wants the value. >> >> printf("%d", n); >> >>} >> >>But when I run the program by typeing hello I get: >> >>Hello, world! >>HI! >> >>HI! >> >>34 { I then typed the number 34, no real reason } >> >>Segmentation violation in >>Page fault at eip=31ed >>Call frame traceback EIPS >> 0x00031ed >> 0x0002dfa >> 0x0000183 >> 0x000010d >> >> >> Since I don't know that much about computer, I am not able to figure out the >> above error. What does EIPS mean? eip is "extended instruction pointer", EIPS is "extended instruction pointer stack", or the list of addresses of subroutines which have been called. >> What can I do to fix. This has nothing to do with DJGPP. You probably ought to find another forum to learn about C. You have successfully cleared the "hello, world" hurdle. Although it didn't do what you wanted, your program compiled and executed 100% correctly. I would suggest getting a lint program. What is does is check your program for errors. The C compiler is not responsible for catching the error you made, because pointers and integers are almost interchangeable. Also, it is a point of Unix philosophy to avoid having any given tool do too much work. The compiler is responsible for generating correct code. However, lint is not responsible for creating code. It only gives the programmer warnings about silly things he's probably doing. Alas, I don't know where to get one in the public domain. Textbooks: Kernighan & Ritchie "The C Programming Language" Stroustrup: "The C++ Programming Language" Get these two. Stroustrup is especially helpful on programming style in C++. Many things he says apply usefully to C style, it's just harder to do it without the C++ enhancements. But stay away from almost everything else on C++, especially if it is an update of a C book. Get a reliable friend to help you to pick one out. Go to a GNU mirror and pick out one of the smaller packages (find will teach you a lot about manipulating files and directories on Unix---this is a very fast way to learn how DOS is different). Go to ftp.math.niu.edu and look at Backus's ports of the GNU utilities such as ls and rm. >> >> I have the following files from ftp.clarkson.edu: >> >> gcc241bn.zip >> gcc241dc.zip >> gas221bn.zip >> gas221dc.zip >> bnu22bn-coff.zip >> bnu22bn.zip >> bnu22dc.zip >> djdev110.zip >> djemu110.zip >> >>Do I need more? No. I don't think you need all of these. If you've got a '486 or a '386 + '387, you don't need djemu100.zip at all. Don't bother with bnu22bn-coff.zip; it's a set of utilities that uses COFF for object files instead of a.out. If you don't know what that means, you don't need it :-) I'm not sure what the *dc.zip files are, but I don't think you need them (they may be diffs of DJGPP from GCC, but I don't know---the packaging has changed and I haven't reinstalled v 1.10 yet). >> >>Thank you very much, >> >>Michael Shipman >>