GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Aug-2005, 18:41
shirlz59 shirlz59 is offline
New Member
 
Join Date: Aug 2005
Posts: 1
shirlz59 is on a distinguished road

"proper" way to write hello world


I'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:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char *argv) {

    /* check input */
    if(argc != 1) {
        fprintf("Too many input arguments!");
        exit(1);
    }

    /* lock stdout */
    if(ftrylockfile(stdout) != 0) {
        perror("lock file");
        exit(1);
    }

    /* print string */
    if(printf("Hello, world!") != 0) {
        perror("");
        exit(1);
    }

    /* forces write from stdout */
    if(fflush(stdout) != 0) {
        perror("");
        exit(1);
    }

    /* unlock stdout */
    funlockfile(stdout);

    return errno;
}
Got any ideas on what else can go wrong in an hello world problem or do you see anything wrong with my program?
Thanks!
  #2  
Old 18-Aug-2005, 19:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by shirlz59
I'm supposed to write a hello world program in c with as much error checking as I can.
Got any ideas on what else can go wrong in an hello world problem or do you see anything wrong with my program?
Thanks!

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:
int main(int argc, char *argv) 

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:
       fprintf("Too many input arguments!");

Maybe something like:
Quote:
9: warning: passing arg 1 of `fprintf' from incompatible pointer type
9: error: too few arguments to function `fprintf'

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:
    if(printf("Hello, world!") != 0) {
        perror("");
        exit(1);
    }

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:
    if(fflush(stdout) != 0) {
        perror("");
        exit(1);
    }

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:
  exit(1);

CPP / C++ / C Code:
    return errno;

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 GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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 07:44
which language ? onauc C++ Forum 2 19-Nov-2004 02:53
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.