- In general, code represents:
- A series of instructions for a computer (i.e. what it does).
- Data for the computer to process (i.e. what to do it with).
- Computers work off a large list of numbers (bytes), represented by a binary or executable file.
- Entering these numbers directly is hard, time-consuming, and error-prone.
- Therefore, we want to instruct the computer in a more human-readable format.
- This is the idea of a programming language, that is more human-friendly.
- Languages come in many forms and can have a specific purpose or be general purpose.
- Languages are also translated into the bytes a computer can execute.
- This translation can happen before the code is ran, with a compiler.
- Or translation can happen as the code runs, with an interpreter.
- Or with a mix of these two.
- C is a low level (considered more machine-friendly than human-friendly), general purpose, compiled programming language.
- C was created by Dennis Ritchie at Bell Labs in 1972.
- C is a fundamental language to modern software.
- Originally developed for for Unix.
- Previous languages depended heavily on the computer it was ran on, so software had to be re-written for every computer.
- C has a very sussinct and a somewhat mathematics inspired syntax.
- This is something many languages take from C.
- C has variables and functions.
- Variables hold a value.
- Functions hold a section of code or series of instructions, and can have input or output.
- Variables and functions have identifiers, or names that can be used to refer to them.
- Variables also have a type, which tells the compiler how to look at the value.
- i.e.
int
only stores integers (whole numbers), andfloat
stores real numbers (with a decimal point). - Types determine the
sizeof
the variable (how much memory, in bytes, it takes), and what can be done with the variable.
- i.e.
- The following are some built-in types in C:
Type | Interpretation (use) | Range | Size (bytes) |
---|---|---|---|
int |
Integer (whole number) | -2,147,483,648 to 2,147,483,647 | 4 |
char |
Character (letter) | -128 to 127 | 1 |
float |
Real number | 1.175494351E-38 to 3.402823466E+38 | 4 |
double |
Real number | 2.2250738585072014E-308 to 1.7976931348623158E+308 | 8 |
void |
No type** | NA | 1 |
-
Note: These values are typical for a common and modern desktop computer.
-
Size, and therefore range, of types can vary between compilers and build targets (computers).
-
void
is used to show that a function doesn't return a result, or doesn't take a parameter. -
void
is used to show pointers that don't point to a type (advanced topic). -
int
andchar
are integral datatypes, meaning they are integers and can be used in conditions. -
There are more types, particularly these mixed with
long
andshort
;signed
andunsigned
.- These affect the size and range of the type.
-
The following are standard types in C, from
stdint.h
:
Type | Interpretation (use) | Size (bytes) | Size (bits) | Range |
---|---|---|---|---|
int8_t |
Signed integer | 1 | 8 | −128 to 127 |
int16_t |
Signed integer | 2 | 16 | –32768 to 32767 |
int32_t |
Signed integer | 4 | 32 | -2,147,483,648 to 2,147,483,647 |
int64_t |
Signed integer | 8 | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
uint8_t |
Unsigned integer | 1 | 8 | 0 to 255 |
uint16_t |
Unsigned integer | 2 | 16 | 0 to 65,535 |
uint32_t |
Unsigned integer | 4 | 32 | 0 to 4,294,967,295 |
uint64_t |
Unsigned integer | 8 | 64 | 0 to 18,446,744,073,709,551,615 |
- The following is a standard type in C, from
stdbool.h
Type | Interpretation (use) | Size (bytes) | Size (bits) | Range |
---|---|---|---|---|
bool |
Boolean | 1 | 8 | true or false |
bool
is meant to be used in condition expressions.- Note: Boolean values need only be
true
orfalse
, taking 1 bit. But modern computers address memory by bytes (8 bits).- Multiple boolean values can be stored into the bits of an integer (intermediate topic).
- Syntax describes the basic rules for writing programs for a C compiler (or other comilers or interpreters).
- Source code contains comments, variables, functions, preprocessor directives, reserved keywords, literals, control sequences, etc.
- The following C code shows basic syntax and output:
#include <stdio.h> // Use standard io library (stdio.h).
// main() is a special function.
int main() { // This is where execution starts!
printf("Hello World!"); // Print a message.
return 0; // Leave main().
} // This is where execution ends.
- This outputs:
Hello, world!
- Comments are ignored by the compiler.
- Comments are used to document code, or temporarily "disable" a portion of the code.
- Inline comments are ignored for the rest of the line after
//
:// I am a comment!
.- A code statement can be on the same line before the
//
.
- Multi-line comments are ignored between
/*
and*/
:
/*
I am a multi-line comment!
Anything in here is ignored.
*/
- Variables store values. They are a chunk of memory.
- Variables have types, which define their size (in bytes) and the operations that can be done with them.
- Variables also have names or identifiers,which you can use to refer to the variable later.
- Note: Think of variables as "chunks of memory" or some bytes.
- Note: Think of the type as "what the memory holds.
- i.e. it hold an integer, real (floating-point) number, picture, audio, a memory address (pointer), or something else.
- This is what determines the sizeof the variable or it's type.
- When a variable's identifier is used (after it is declared), it is "replaced" by it's value.
- Variables are declared with the syntax
type name;
.- This tells the compiler
name
exists and has a typetype
.
- This tells the compiler
- Variables are defined by assigning a value to the variable.
- This can be done assigning a value:
name = value;
. - This tells the compiler
name
has a value ofvalue
.
- This can be done assigning a value:
- Multiple variables with the same type are declared with the syntax:
type name1, name2;
- Note: A variable can be assigned to (declared) when it is defined:
- For one variable:
type name = value;
- This tells the compiler
name
exists and has a type oftype
and a value ofvalue
. - For multiple variables:
type name1 = value1, name2 = value2;
- For one variable:
- Functions are named sections of code.
- Functions take in parameters (input), and return a value (output).
- Functions can have 0 or more parameters.
- When a function has no parameters, the parameter list can be omitted or replaced with
void
.
- When a function has no parameters, the parameter list can be omitted or replaced with
- Functions can return a value or return without a value.
- Declaration syntax:
return_type name(type name, ...);
.- The declaration tells the compiler what this function does, as a series of statements.
- Definition syntax:
return_type name(type name, ...) { ... }
.- The definition tells the compiler this function exists, with it's inputs and outputs.
- The definition contains the declaration, with a series of statements between curly brackets:
{ ... }
. - The declaration isn't always needed, i.e. if the definition is compiled before the function is called.
-
Statements can include declarations, assignments, conditional execution, arithmetic, etc.
-
Statements are executed in order, from top to bottom*.
-
Statements end with
;
. -
Statements are generally on a new line, but can be separated by any white-space.
- The following are both valid sequences of statements:
int x = 0; x = 1; // On one line, seperated with ;
int x = 0; // On multiple lines
x = 1; // seperated with ; and arbitrary white space
-
- Compilers can actually change the order of statements, as long as the result produces the same output (very advanced topic, compiler optimization).
- Preprocessor directives happen before the code is compiled, or as the first stage of the compilation process.
- They start with
#
. - Note: This is commonly used to bring in external code (
#include <stdio.h>
). - Note: This is also used to replace text in the code, i.e. a constant (
#define CONSTANT 5
). - Note: This is also used for guard code (
#pragma once
) (moderate topic).
- They start with
- Keywords are used by the C language, and you cannot name any identifier (function or variable name) the same as any keyword.
- Keywords, generally, have a special purpose such as conditionals, loops, etc.
- Some keywords are
if
,else
,for
,while
,break
,continue
, andreturn
.
- Literals are values within the code.
- String literals are a sequence of characters between quotation marks:
"Something."
.- They may have escape sequences, denoted with
'\'
. Here are a few:
- They may have escape sequences, denoted with
Character | Interpretation (use) |
---|---|
'\0' |
Null character* |
'\n' |
Newline |
'\b' |
Backspace |
'\t' |
(Horizontal) Tab |
'\v' |
(Vertical) Tab |
'\\' |
Backslash (just "") |
'\'' |
Apostrophe (just "'") |
'\"' |
Quotation mark (just ") |
-
- The null character ('\0') is used to denote the end of a string.
-
Integer literals are integers:
42
, floating-point literals are real numbers:123.456
. -
Literals can be defined for other types, depending on their definitions.
- Control sequences provide a nice way to control the order of instructions.
- They allow control "if" statements are executed and how many times they are executed ("looped" through).
- Conditional execution executes statements "if" their condition is nonzero.
- Note: This shows some weird aspects of C.
- A value of 0 means
false
, and anything else meanstrue
. - Conditions are expressions composed of Boolean algabra with logic operators and bitwaise operators, and integral datatypes.
- A value of 0 means
int cond = 0, cond2 = 0;
if (cond) {...} // if cond is nonzero, ... is executed
// Then execution resumes from here. Jumps here if cond is 0
if (cond) {...} // only do this if cond is nonzero
else {...} // only do this if cond is 0
if (cond) {...} // only do this if cond is nonzero
else if(cond2) {...} // only do this if cond2 is nonzero
else {...} // only do this if cond is 0, then cond2 is zero
- Loops execute statements repetitively, based on a condition.
while
loops execute statements "while" a condition istrue
.for
loops function aswhile
loops, but give you an initialization expression (that happens before the repetition), condition expression (that determines if the code is repeated), and itteration expression (that happens at the end of every loop).do
-while
loops function likewhile
loops, but the statements within the loop are executed at least once.
// WHILE LOOP example
bool do_loop = true;
int i = 0;
printf("About to go into while loop!\n");
while(do_loop) {
printf("\tIn while loop, step %d.\n", i);
i = i + 1;
if(i >= 10) do_while_loop = false;
}
printf("While loop over!\n\n");
// FOR LOOP example
printf("About to go into for loop!\n");
for(int j = 0; j < 10; j++) // "repeat 10 times, putting the current step in j"
printf("\tIn for loop, step %d.\n", j);
printf("For loop over!\n\n");
// See that the for loop combines many lines into one when itterating through numbers like this.
// Because the body of the loop only contains one expression, the {} can be omitted.
// DO-WHILE LOOP example
do_loop = false;
printf("About to go into do-while loop!\n do_loop is %b.\n", do_loop);
do {
printf("\tIn do-while loop!\n");
} while(do_loop);
printf("Do-while loop over!\n\n");
// This shows the loop being executed once, and only once, as the condition is checked before the second itteration