The C Programming Language
What is C? "High-level" programming language developed by Dennis Ritchie with Brian Kernighan Bell Labs, New Jersey, 1970s Developed in conjunction with Unix Intended to provide support and control of computer hardware "Systems programming", e.g. operating systems Reflects machine language instructions and behavior quite closely Detractors call it a "low-level" programming language, or a "high-level assembler"
Evolution 1972 first version introduced 1978 1st publication of "K&R" 1988 "ANSI C" standardizes language 1999 "C99" (ISO/IEC 9899:1999) inline functions; single-line comments new data types, variable length arrays (VLAs) 2011 "C11" (ISO/IEC 9899:2011) Uni support anonymous structs and unions alignment, bounds-checking; VLAs optional C99, C11 additions not always implemented!
C is an Imperative, Procedural Language It is not object-oriented No classes Class methods are replaced by globally visible functions well, not really globally visible, but not local to a class, either It is a starting point for object-oriented C++ It is not "functional" Most behavior would be considered side effects in a real functional language Functional style is possible, but ill-advised
C is a Compiled Language Source files are not executed directly Contrast with Basic, Javascript, other interpreted languages No intermediate form to interpret Contrast with Java ".jar", Python ".pyc" Executables are in hardware's native "machine language" Source must be recompiled for each target architecture Only the executable needs to be distributed
What is Compilation? Producing an executable program from (one or more) C source file(s) is a multi-step process Edit the source file Compile to produce an object file Link to produce an executable file Load/run the executable as often as desired On Linux systems the gcc program does the compile and link steps. Visual Studio does this on Windows; it also builds other files needed for the GUI (icons, other resources, etc.)
Consider: Making Java Programs Edit source.java Compile to byte.jar Syntax errors? Run the program Python is similar execute in JVM Load JVM into RAM DLLs
Building a C Program Edit source.c source Compile to object.o object Syntax errors? Run the program Link to make executable Execute as a process Load into RAM program stored on standard libraries
Complex C Programs Edit Edit source Edit source source.c source.c source saved.c source on Compile Compile to object Compile to object to object.o object.o object saved.o object on Syntax errors? Run the program Execute as a process Load into RAM DLLs program stored on Link all to make executable standard libraries
C Compile step Run gcc with flags: -Wall "Warn all" -std= choose version of C to accept -c "Compile only", produce object file -g "save symbol table for debugging Object file is created unless there are syntax errors Another possible flag: -S "compile to assembly " e.g. ansi, c99, c11 (default is a "gnu" variant)
Producing an object file Example gcc Wall c g mysource.c Remember, the source file's name must end with ".c" The compile step creates "mysource.o" unless there were syntax errors Repair any reported errors, starting with the first one Repeat until all syntax errors are fixed, and for as many source files as needed
Link step Run gcc again, with different flags -o executable_name -lm if needed (not always) brings in the standard math library Examples gcc o myprog mysource.o gcc o mathprog mymathsource.o -lm
Compiling and Linking together gcc can combine the compile and link steps: gcc Wall g o myprog mysource.c gcc Wall g o mathprog mymathsource.c -lm This is the common way to do it for smaller programs The intermediate.o file is deleted after linking
Execution Two-part process Load into memory Transfer control to beginning of Usually performed together Type executable-file s name, or click on a link Operating system loads and begins running the Examples./myprog./mathprog The./ part helps locate the file on
Final Remark: Some Free C Compilers gcc Gnu Compiler Collection Unix-like systems, MS-Windows w/ Unix emulator, other systems tcc Tiny C Compiler Unix-like systems, DOS/MS-Windows, others pcc Portable C Compiler Unix-like systems, DOS/MS-Windows, others Visual C++ Express MS-Windows development environment Borland Turbo C old DOS/MS-Windows compiler