2.3. Differences from Java

Here are some notable differences between C and Java.

The main function

The main function has heading
int main(int argc, char** argv)
where argv is an array of null-terminated strings and argc is the size of argv.

The value returned by main is 0 is all went well and nonzero if there was an error.

Variable initialization

In Java, you are required to initialize a local variable before you use it.

In C, a local variable starts out holding a junk value. If you use the variable before storing anything into it, you get that junk value.

Preprocessor

Before the compiler proper sees a C program, a preprocessor makes some modifications to it.

C
program
preprocessor mangled
program
compiler machine
language

A line that begins with # is a preprocessor directive.

# define TOK_INTCONST 300
# define max(x,y) (((x) > (y)) ? (x) : (y))
defines a constant TOK_INTCONST and a macro max(x,y).

After seeing those directives, the preprocessor

Replacements are textual. For example,

# define PRIVATE static
tells the preprocessor to replace PRIVATE by static.

Header files

Rather than marking each thing as public or private, you achieve a similar goal by having two files, one that describes what is exported (or public) and the other containing both private things and implementation of the public things.

Each module x.c (except, typically, the module with a main function) has a header file x.h. The header file contains

  1. Definitions of exported types,

  2. Extern declarations of exported variables and functions.

An extern declaration of a function is the function heading followed by a semicolon.

An extern declaration of a variable is the variable declaration preceded by extern.

File x.c and each file that needs to use what is described in x.h say

# include "x.h"
The # character tells you that this is a preprocessor directive. The preprocessor insert the contents of x.h in place of this #include line.

Linkage

You compile each module into an object file. Then you link the object files into an executable file. Command
  gcc -c x.c
runs the Gnu C compiler to compile x.c and create object file x.o. Command
  gcc -o foo x.o y.o z.o
links together x.o, y.o and z.o into a single executable program called foo.