Re: First C program

Mark S Waterbury <[email protected]>
Newsgroups gmane.comp.lang.as400.c
Organization ISS
Message-ID <[email protected]>
Hi, Barbara, and Frank:

This reply is mostly "for the archives". ...

I think a much improved style for C code can be illustrated by slightly 
re-formatting your last code example, as shown below:

static _RFILE *fOpenFile( const char *filename,
                           const char *openmode,
                           	int rc 		)  {
    _RFILE *f;
    f = _Ropen(filename, openmode);
    if (f EQ NULL) {
       printf("can't open file %s\n", PFILENAME);
       exit(rc);
       }
    return f;
    }

Basically, my recommendations boil down to:

1. indent just two or three spaces for each level of nesting (I prefer 3)

2. use the "{" and "}" even if only one statement or function is coded;

3. put "{" on the same line as the function header or structure declaration or "if " or "while " or "for" statement  ...

4. place the corresponding "}" on a line by itself, indented to "line up" with the code above it

Here is an example of an if statement with an "else" clause:

     if (x == y) {
        printf("do something...\n");
        }
     else {
        printf("do something else ...\n");
        }

Here is another alternative:

     if (x == y) {
        printf("do something...\n");
   } else {
       printf("do something else ...\n");
       }

Whatever you decide to do, strive for consistency.

All the best,

Mark S. Waterbury   

-- 
This is the Bare Metal Programming IBM i (AS/400 and iSeries) (C400-L) mailing list
To post a message email: C400-L-Zwy7GipZuJhWk0Htik3J/[email protected]
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request-Zwy7GipZuJhWk0Htik3J/[email protected]
Before posting, please take a moment to review the archives
at http://archive.midrange.com/c400-l.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.