The Structure of C Programs
- The C compiler ignores all whitespaces
i.e. spaces, tabs and newline are all stripped and ignored
This is different to Python where spacing has meaning! - A program written in C is input to a pre-processor which then output’s to a C compiler
- Code with a ’#’ is processed by the pre-processor
- Comments: (ignored by C compiler)
- /* block comments */
- // comments to the end of a line
- Unlike Python, C code must be written inside a function
Compiling and Linking our C Programs
- C programs are human-readable text files
- Also known as source-code files
- Makes them easy to copy/read/edit
- Before a C program can be run (executed), it must be compiled into a form an operating system can better manage
- A compiler translates source-code files into object-code files
- Compilers are the most complicated programs that exist
Python is known as an interpreted language where each line of code is executed one by one by an interpreter. For Python, there is no separate compilation step. The Python interpreter itself is written in C!
- Object-code files are linked together to produce an executable program
- Also known as a binary, executable or an exe file
- This is done by a program known as a linker

- Because of how C works, you are encouraged to correct all errors and warnings before running the program
Variables
- Variables are locations in a computer’s memory
- Any variable can only hold a single value at a time
- Variables are only used within the block they are defined
- Global variables defined outside a block can be used anywhere
Naming our Variables
While variable names can be almost anything (but not the same as the keywords in C) there’s a simple restriction on the permitted characters in a name:
- They must commence with an alphabetic or the underscore character (_ A-Z a-z), and:
- Be followed by zero or more alphabetic, underscore or digit characters (_ A-Z a-z 0-9)
- C variable names are case sensitive
- It’s preferred that variable names do not consist entirely of uppercase characters
- We’ll consistently use uppercase-only names for constants provided by the C pre-processor, or user-defined type names: MAXLENGTH, AVATAR, BUFSIZ, and ROT
- Older C compilers limited variable names to ~8 unique characters
- Thus, for them, turn_nuclear_reactor_coolant_on and turn_nuclear_reactor_coolant_off are the same variable!
- Keep this in mind if ever developing portable code for old environments
Switch Statements
- When the same expression is compared against a number of distinct values, it’s preferred to evaluate the expression once, and compare it with possible values:

For more information about basic C coding, see slides
(i.e. conditions, Boolean values, bounded and unbounded loops, etc.)
Overall, very similar to Python:
-
Break functions similarly
-
Just slightly different syntax for most things
-
Floating-point data types are very rarely used in systems programming
- As C didn’t even have Boolean data types originally
Lab Information
- You need an operating system that can execute C programs
- e.g. Linux or MacOS
- Ubuntu has been set up on laptop
- Can be accessed from Ubuntu app on start menu