Mostly, the C syntax is beneficial to understand before writing programs.
A C program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations done, and variables store values used during the computation.
This is all good and great you may say. C functions are like the subroutines and function of Fortran or the procedures and functions of Pascal. Normally you are at liberty to give functions whatever names you like, but "main" is special — your program begins executing at the beginning of main. This means that every program must have a main somewhere.
main() will usually call other functionsto help perform its job, some that you wrote, and others from libraries that are provided to you.
Here is a simple program written in C:
#include <stdio.h> // include information about standard library
int main() // define a function named main that receives no argument values
{ // statements of main are enclosed in braces
printf("Hello world\n");// main calls library function printf to print this sequence of characgers: \n reperesents the new line character
}
In C, a function is equivalent to a subroutine or function in Fortran, or a procedure or function in Pascal. A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation.
return-type function-name(paramater declarations, if any)
{
declarations
statements
}
That's also good, but remember functions much be called outside of main(), not inside:
#include <stdio.h>
// declaration
int power(int, int);
// main
int main() {
int i;
for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}
// actual function
int power(int base, int n) {
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}
I hope this does help better compile your code. Another thing I saw was you had a local declaration of:
An array cannot be initialzed with 0 indices. It may result in an error, so try to use non-zero integers when setting the index of an array.
Hope this helps,
-
Stack
Overflow