A given problem may typically be broken down into a series of smaller tasks. In C these tasks are called functions. A C program is basically just a collection of functions which cooperate together to provide the solution to our original problem. It is often termed a procedural language as opposed to object-oriented languages such as C++ or Java.
f(int a, double b);The meaning of this is that f is a function which operates on two numbers (its arguments) - here an integer a and double precision real number b. Having completed its work a C function can (optionally) return a value to the program that called it - if it returns a double precision number we denote that by declaring the function as
double f(int a, double b);Notice the semi-colon here -- all lines of C which perform some definite action are terminated in semi-colons.
All C programs must contain a main function where execution starts and finishes. This main function will typically set up (or declare) the data structures necessary to the problem at the start of the code and will then call a series of other functions to operate on this data in some way. Typically it is convenient to group all function declarations like the above at the top of the file containing the C program (or source code as it is called). Also at the top are input parameters specified by statements like
#define SOMEPARAMETER 0In addition, you will commonly see statements like
#include "stdio.h"These allow the program to call a standard library of other functions (in this case I/O routines).
(void)fprintf(fp,"%lg $d\n",x,i)The first argument of the function fprintf is fp - it identifies a particular file that is to be written to. The text in double quotes tells the function what is to be printed -- a double precision number (%lg) and and integer (%d). There is a single blank between them and then a carriage return is written (\n). The numbers x and i then follow. The prefix (void) instructs the compiler that fprintf does not return a value to the function which called it. The function fscanf is similar - we will not need it here.
fp=fopen("name_of_file","w");
Substituting r for w allows a file to be opened for reading.
double x[N](N must be defined somewhere with a #define statement) This reserves space in memory for the elements x[0] to x[N-1]. Finally, and a little more obscurely, it is possible to have another data type called a pointer. You can think of this as the address in the computers's memory of a particular piece of data not the data itself. A pointer to an integer would be declared as
int *p
for(i=0;i<N;i++)
x[i]=x[i]*2;
This loop multiplies all the elements of x by two.
So-called if statements can also be useful eg.
if(i>0){ do something }
else {do something else}