NO

Author Topic: Is old code still current practice?  (Read 3328 times)

David L Morris

  • Guest
Is old code still current practice?
« on: December 14, 2011, 05:47:41 AM »
I am in my 70s and enjoying learning C to keep my mind exercised and working from a 1985 book "The C Compendium by David Lawrence and Mark England ISBN 0-946408-86-6".  This code is from the book and with a few adjustments I have it working.  My question is whether this code is in line with current practice for console applications?

/****************************************************************************
 *                                                                          *
 * File    : main.c                                                         *
 *                                                                          *
 * Purpose : Console mode (command line) program.                           *
 *                                                                          *
 * History : Date      Reason                                               *
 *           00/00/00  Created                                              *
 *                                                                          *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
char *err_mgs [] = {
            "\nMessage 1"      ,
            "\nMessage 2"      ,
            "\nMessage 3"      ,
            "\nMessage 4"      ,
            "\nMessage 5"      
};

#define PROG_WARN 1
#define PROG_FATAL 2
#define DATA_WARN  3
#define DATA_FATAL  4
#define ERROR_MAX  sizeof(err_mgs)/sizeof(char *)

//FUNCTION: ERROR - PROCEDURE WITH NO RETURN VALUE

void error (int num, int type, int extra)
      {
         extern char *err_mgs[];
         switch (type) {
            case PROG_WARN:
               printf ("\n\n***** WARNING : possible error in program *****");
               break;
            case PROG_FATAL:
               printf ("\n\n***** FATAL ERROR in program *****");
               break;
            case DATA_FATAL:
            case DATA_WARN:
               break;
            default:
               error (-2,PROG_FATAL, type);
            }
         if (num >= 0 && num < ERROR_MAX)
            printf ("%s", err_mgs[num]);
         else
            switch (num) {
            case -1:
               printf("\nUnknown error number: %d",extra);
                  break;
            case -2:
               printf("\nUnknown error type: %d",extra);
                  break;
            default:
               error (-1,PROG_FATAL, num);
            }
            if (type == PROG_FATAL || type == DATA_FATAL) {
               printf("\n\nProgram Execution Terminated");
               abort();

            }
      }


/****************************************************************************
 *                                                                          *
 * Function: main                                                           *
 *                                                                          *
 * Purpose : Main entry point.                                              *
 *                                                                          *
 * History : Date      Reason                                               *
 *           00/00/00  Created                                              *
 *                                                                          *
 ****************************************************************************/

int main(int argc, char *argv[])
{
    /* TODO: Enter code here */
   int n, num, type;
   for (n=0;n<10;n++)
      { printf ("\n\nError numbers 0-%d,error type 1-4", ERROR_MAX-1);
         printf ("\nInput Error number to print, error type ? ");
         scanf ("%d, %d", &num, &type);
         printf("\n\n num = %d  type = %d", num, type);
         error (num, type,0);
      }

    return 0;
}


CommonTater

  • Guest
Re: Is old code still current practice?
« Reply #1 on: December 14, 2011, 06:04:30 AM »
That code should work... But quite a bit has changed...

I would look for newer tutorials based on the C-99 standard used by Pelles C.  They will be closer to today's reality.
It's all C, but C got a little smarter in the mean time :D




Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Is old code still current practice?
« Reply #2 on: December 15, 2011, 01:39:49 AM »
Well, one could content that today, nobody writes console applications anymore... ;-)

Being more of an oldtimer myself (ok, I am still 2 decades younger), I would say that C is C, be it in 1985 or in 2011/2012.
Few things have really changed, the most obvious ones that I have encountered myself are
- that in C99 "//" style "to-end-of -line" comments are now allowed
- there is no implicit "int" assumed on previously undeclared variables or functions without a return type specified
-  there is now a new integer type "long long", which is commonly a 64bit (8 bytes) integer

And if you are going by an older C book, make sure you do not "assume" any integer size in defining contants or doing constant math in expressions. Just had someone in a different project stumble over that one. Back in '85, DOS was still prevalent, which means that all "int" and int expressions are assumed 16 bit, nowadays, in the days of 32/64 bitWindows and Linux, the default is commonly 32bit...

Ralf

David L Morris

  • Guest
Re: Is old code still current practice?
« Reply #3 on: December 15, 2011, 07:18:01 AM »
Thanks for both your comments Tater and Ralf.  In my former life from 1992 - 2006 I did much work for a large advertising agency, downsizing from mainframe COBOL systems to Windows using Visual Basic 3/16bit and other development tools, such as Crystal report writers.  It was a fun part of my experiences, however, for speed of development we could not come close with C.  Once one got the hang of event driven VB, we stayed with that environment.

The conversion covered over 300 COBOL programs for Finance, Production and Media booking systems.  The end result was a major success.  In 2005, following mergere and takeovers by other companies, a commercial system was purchased and the InHouse system retired.

I do find the console a good learning tool and this forum has been most helpful.  I hope it keeps going and does not become abandoned.

My best wishes to you for Christmas and the New Year.

David