The C Programming Language Intro My favorite C Reference is "The C Programming Language Second Edition / ANSI C" by B. Kernihhan and D. Richie, Prentice Hall 1988. It is brief and a complete introduction to ANSI C. I also has a complete ANSI C specification! It does not adress the latest ANSI C extensions, but that is not really bad on a first go. Note that a "well written" ANSI C program is also a C++ program and can be compiled by a C++ compiler as though it was a C++ program. But beware of a potential prefromance difference in the executablke code (gcc/g++ show this dramatically sometimes!) ----------------------------------------------------------------------- The structure of a C program is represented as a group of files (it could be all in one file), usually with name entensions .c and .h . Files with c function definitions have .c extensions. Files with .h extensions are called "header" or "include" files. They should only contain declarations and preprocessor macros that need to be shared between multiple .c files. Each .c c program file is a sequence of declarations, definitions, and function definitions. It is "good practice to put all declarations and definitions needed in the file at the top of the file. FILE: prog.c macros, declarations and definitions (not function definitions}; function-definition; function-definition; ... function-definition; FILE: prog.h macros and declarations only --------------------------------------------------------------------- The C preprocessor (macros) Two pass process A useful feature that is not part of the ANSI C++ standard. --------------------------------------------------------------------- Declarations vs Definitions #define N 223 #define M N+6 #define N 223*6 #define N (223*6) #define DEBUG #ifdef DEBUG ... #endif #ifndef DEBUG #elif #if constat-expression #undef DEBUG Macros with arguements... #define MAX(a,b) ((a)>(b)?(a):(b)) Other preprocessor features ----------------------------------------------------------------------- The C "main()" function int main(int argc, char *argv[]) { ... } int main() main() K&R C: int main(int argc, char *argv[], char *envp[]); ANSI C: int main(int argc, char *argv[]); POSIX.1: extern char **environ getenv(3c), putenv(3c) --------------------------------------------------------------------- Functions - all arguements are passe3d by call by value --------------------------------------------------------------------- Blocks { declarations and definitions (not function definitions}; statements } It would be bad practice to put declarations at the top of a block. They really should go at the top of the file outside the scope of all functions. C++ allows declarations that are not at the top of a block, such as in the first part of a for(;;) loop, as in for(int i=0; i