Hi
Having done a first pass on learning C using the K&R The C programming
Language I am
Now doing the examples in ILE C for AS/400(R) Programmer's Guide.
The below is an example of a program in the IBM Guide.
The IBM code seems to me to be a trivial example of what C is capable of.
Scott you mentioned before when I posted a question on C Syntax Errors that
the
Code posted was 'Absolutely horrible code'. At the time your comment did
not really
register with me. I well respect your opinion and I have learnt a lot from
you.
I would be grateful to hear your views on how to write C code.
From my reviews of what is said on the net about C code,
I have stumbled on these points.
Professional C programmers use a subset of C that they have found
best suits their needs.
It is easy to introduce errors in C codes that lead to the infamous
'memory leaks' and such errors can be very hard to find.
So hard in fact that some C projects have failed.
Debugging C can be hard because a single line can be a looping function and
the error can be inside the loop but the debugger cannot see that level of
detail unless you debug intermediate source and the intermediate source
can be so arcane you still cannot find the error.
So I am prompted to ask, is anyone doing serious coding in C on the AS400.
And if so what is the application and more interesting to me what style of
C code is being used?
Regards
Frank Kolmann
http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.pgmgd.doc/cpprog162.htm
/* This function writes an audit trail. To write the audit trail
the */
/* file field structure is retrieved from the DDS file T1520DD1
and */
/* the taxrate data item is imported from service program
T1520SP2. */
/* Retrieves the file field structure.
*/
#pragma mapinc("myinc", "MYLIB/T1520DD1(*all)", "both", "p z","")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <decimal.h>
#include <recio.h>
#include <xxcvt.h>
/* These includes are for the call to QWCCVTDT API to get the
system */(1)
/* date to be used in the audit
trail. */
#include <QSYSINC/H/QWCCVTDT>
#include <QSYSINC/H/QUSEC>
/* DDS mapping of the audit file,
T1520DD1. */(2)
#include "myinc"
/* Tax rate is imported from service program
T1520SP2. */(3)
const extern decimal (2,2) taxrate;
void write_audit_trail (char user_id[10],
char item_name[],
decimal (10,2) price,
short int quantity,
char formatted_cost[22])
{
char char_item_name[21];
char char_price[11];
char temp_char_price[11];
char char_quantity[4];
char char_date[6];
char char_taxrate[2];
/* Qus_EC_t is defined in
QUSEC. */
Qus_EC_t errcode;
char get_date[16];
int i;
double d;
/* File field structure is generated by the #pragma mapinc
directive. */
MYLIB_T1520DD1_T1520DD1R_both_t buf1;
_RFILE *fp;
/* Get the current date.
*/
errcode.Bytes_Provided = 0;
QWCCVTDT ("*CURRENT ", "", "*MDY ", get_date, &errcode);
memcpy (char_date, &(get_date[1]), 6);
/* Loop through the item_name and remove the null
terminator. */
for (i=0; i<=20; i++)
{
if (item_name[i] == '\0') char_item_name[i] = ' ';
else char_item_name[i] = item_name[i];
}
/* Convert packed to zoned for audit
file. */
d = price;
QXXDTOZ (char_price, 10, 2, d);
QXXITOZ (char_quantity, 4, 0, quantity);
d = taxrate;
QXXDTOZ (char_taxrate, 2, 2, d);
/* Set up buffer for
write. */
memset(&buf1, ' ', sizeof(buf1));
memcpy(buf1.USER, user_id, 10);
memcpy(buf1.ITEM, char_item_name, 20);
memcpy(buf1.PRICE, char_price, 11);
memcpy(buf1.QTY, char_quantity, 4);
memcpy(buf1.TXRATE, char_taxrate, 2);
memcpy(buf1.TOTAL, formatted_cost, 21);
memcpy(buf1.DATE, char_date, 6);
if ((fp = _Ropen("MYLIB/T1520DD1", "ar+")) == NULL)
{
printf("Cannot open audit file\n");
}
_Rwrite(fp, (void *)&buf1, sizeof(buf1));
_Rclose(fp);
}
date: Fri, 17 Jan 2014 20:13:16 -0600
from: Scott Klement <c400-l@xxxxxxxxxxxxxxxx>
subject: Re: [C400-L] C syntax errors
Not to mention that it uses global variables (and even extern variables)
throughout instead of parameters...
Absolutely horrible code. Definitely not to be used as an example!.
As an Amazon Associate we earn from qualifying purchases.