![]() |
|
#1
|
|||
|
|||
"proper" way to write hello worldI'm supposed to write a hello world program in c with as much error checking as I can. Here's my code so far:
CPP / C++ / C Code:
Thanks! |
|||
|
#2
|
|||
|
|||
|
Quote:
Non standard library functions ftrylockfile() and funlockfile(). Not present in all compilers/operating systems. I would say that programs that can't be compiled on standard C compilers have limited usefulness, and I would avoid non-standard anything unless there is a really, really good reason. I can't see where locking the availability of stdout is a very good reason. CPP / C++ / C Code:
Now, the second argument to main() can be declared in the form char ** argv or char *argv[], but char *argv is incorrect. Since you don't use argv for anything, I doubt that this will ever cause problems, but it's a bad habit to put incorrect code in a program even if you would like to claim "no harm, no foul". What happens if someone modifies the program some day and decides to use argv without noticing that it is declared incorrectly? Didn't you get compiler warnings (at least) from this: CPP / C++ / C Code:
Maybe something like: Quote:
Moving right along--- What do you expect to be the return value from printf? For the following, if printf() worked OK, the program exits now. If it didn't work ok, the program exits now. Is that what you had in mind? If it is, then what happens, since you don't get to the funlockfile()? CPP / C++ / C Code:
Supposing you "fix" this, so that the program continues if printf() has the correct value. Since funlockfile() is non-standard, can you tell what happens if there is an abnormal exit (exit(1)) from any of the other various places without funlocking it? If nothing bad happens, then why bother with it at all? Having a bunch of code that does nothing is not technically wrong, but it plays holy heck when you or someone modifies the program later and can't figure out what all of that extra stuff is supposed to be. In this vein, I don't really see the point of the following (but, I guess it isn't technically "wrong") CPP / C++ / C Code:
When a file is closed all the buffers associated with it are automatically flushed. If the program terminates, every buffer is automatically flushed. Finally, a really, really minor point: CPP / C++ / C Code:
CPP / C++ / C Code:
The C standard only gives two possible return values for main() (And therefore for exit()). They are EXIT_SUCCESS and EXIT_FAILURE defined in <stdlib.h> A value of 0 is always acceptable. Now, different operating systems do different things with other return values, and I doubt that returning anything other than one of the officially sanctioned values will cause the program to crash, but you did ask if anything was "wrong". This is a pretty picky point, and I won't call the "return" commandos on you, but you did ask. Regards, Dave |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| which language ? | onauc | C++ Forum | 2 | 19-Nov-2004 03:53 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The