C Programming Language

While C programming language has many strengths, support for numerical computations is not one of them. There are many a grouse that programmers interested in numerical computations have with C. Here are some of them:
  • Arrays are indexed starting with with 0 (zero), whereas most algorithms for engineering applications use arrays indexed starting with 1 (one).
  • Array sizes must be defined as constants, both at the time of creating them as well as when passed on to functions as arguments. Further, arrays used inside a child function must have the same size as the corresponding argument in the parent function. This defeats the very purpose of writing functions, which are supposed to be developed independent of the functions in which they are expected to be used. This restriction makes it necessary for the developer of the function to notify the sizes of the arrays that must be passed into the function and the user of the function to know this information and create arrays of the required size
But C certainly has its strengths, and some of them are:
  • Small set of keywords, rich set of control statements, clearly defined scope of variables and functions, strongly typed.
  • User defined data types and data structures and algorithms
  • Dynamic memory allocation
In fact, the second grouse mentioned above can be overcome by developing a library of functions that use dynamic memory allocation, which is the way it is done in the well known Numerical Recipes in C book. But this has its disadvantage, a steep learning curve.

Then there are other benefits for scientific and engineering applications:
  • Ability to build applications with a GUI
  • Access to system information
  • Availability of libraries for 2D and 3D device independent graphics
  • Availability of libraries for access to databases, GUI based application frameworks
Let us see how a function to read data into a two dimensioned array is written in C:

#include <stdio.h>

#define MAX_R 50
#define MAX_C 50

int readmat(int m, int n, float x[MAX_R][MAX_C])
  int i, j;

  for(i = 0; i < m; i++)
    for(j = 0; j < n; j++)
      scanf("%f", &x[i][j]);
return 0;

int main()
  float a[MAX_R][MAX_C], b[MAX_R][MAX_C];
  int m, n, m1, n1;

  scanf(%d%d", &m, &n);
  readmat(m, n, a);

  scanf(%d%d", &m1, &n1);
  readmat(m1, n1, b);

You must note the following points:
  • Size of array a[MAX_R][MAX_C] in the main() function must be the same as size of the array x[MAX_R][MAX_C] in the function readmat()
  • Even when m and n are very small compared to MAX_R and MAX_C, respectively, the sizes must match