Here are some notable differences between C and Java.
The main function
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 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
replaces an occurrence of TOK_INTCONST (as a word, not as part of a longer word) by 300,
It replaces max(0,z) by (((0) > (z)) ? (0) : (z)).
Replacements are textual. For example,
# define PRIVATE statictells the preprocessor to replace PRIVATE by static.
Header files
Each module x.c (except, typically, the module with a main function) has a header file x.h. The header file contains
Definitions of exported types,
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
gcc -c x.cruns the Gnu C compiler to compile x.c and create object file x.o. Command
gcc -o foo x.o y.o z.olinks together x.o, y.o and z.o into a single executable program called foo.