{...}
, with a unique name and a declared type. The
function must return a value of the declared type unless the type is
void
in which case it does not return anything. A function
can optionally take arguments.
For example, here is a function that takes two double arguments and returns a double value,
double multiply(double a, double b) { double r = a*b; /* do some useful stuff */ return r; /* return a value of declared type */ }Here is a function that takes no parameters and returns nothing,
void hello(void){ printf("hello, world\n"); }A function is called by its name followed by parenthesis with the arguments (if any), for example,
hello(); double x = multiply(2.0, 4.0);
C is a free form language: you can put (multiple) new-lines, spaces, and tabs anywhere between the tokens of the language.
main
. This function is called by the operating system when
the program is run. The main
function then calls other
functions in the program.
.c
such as hello.c
. Apart from definitions of functions
the source code can also contain declarations of variables and types
(often in separate header files with extension .h
), and
also preprocessor directives.
.o
. The process of translation is called compilation
and is typically done by a C-compiler such as gcc
. For
example,
gcc -c hello.c -o hello.o
-c
option means compile only and -o
hello.o
means output goes into file "hello.o".
(The last option is not strictly necessary as the file
hello.c
is compiled into hello.o
by default).
Actually before compilation the C-compiler first applies the C-preprocessor to the source code. C-preprocessor is a macro-processor used to include files, define macros and do other useful tricks.
For example, after the preprocessor directive
#define PI 3.1415927
all the instances of the token "PI" will be replaced with the token "3.1415927".
gcc
command.
Given the object code, gcc does
linking of the supplied object file(s). For example, the command
gcc hello.o -o hello -lm
-o hello
means output the resulting executable
code into the file "hello" and -lm
means link with libm
math library (in case the program uses math functions). All other
standard libraries are linked by default except for the math library
which has to be linked explicitely (it's historical, like a.out).
By default
the exectable code is put into a file with the name a.out
(for historical reasons).
The program can now be run using the command ./hello
where
./
means "the current directory".
If the program is wholly contaned in one .c
file, like
hello.c
, the gcc
can compile and link it in
one go, for example:
gcc hello.c -o hello
math.o
and
hello.o
, and the program also uses functions from a library, say the
libm
mathematical library, you have to link everything together:
gcc math.o hello.o -o math -lm
main
function must be defined according to one of the
following prototypes:
int main() {...} /* no arguments to main */ int main(void) {...} /* no arguments to main */ int main(int argc, char *argv[]) {...} /* arguments in the array-of-arrays argv */ int main(int argc, char **argv) {...} /* the same as above */
The main function must return integer zero upon succesful completion.