Three data types of variables:
int: integer, a whole number (eg: -1, 0, 1, 2, 3)
- A whole number, with no fractions or decimals
- Most commonly uses 32 bits (which is also 4 bytes)
- This gives us exactly 2^(32) different possible values
- The maximum is very large, but it's not infinite!
char: a single character (eg: 'a', 'A', etc)
- A single character in C can also be represented as a int
- A single character variable holds an ASCII value (integers 0-127), as opposed to
the character itself
- The syntax to assign a single character is to put the character in single quotes:
"a"
- For a capital letter A: the character is 'A' and the int stored is 65
- You use a cjar to declare a character: char letter = 'a' - this will assign 79 to
the variable letter
double: floating point number (eg: 3.14159, 8,834, 7,11)
- A double-sized floating point number
- A decimal value - "floating point" means the point can be anywhere in the number
- Eg: 10567 or 105.67 (the points are in different places in the same digits)
- It's called "double" because it's usually 64 bits, hence the double size of our
integers (or 8 bytes)
We always use lower case letters to start our variable names
"return", "int" and "double" can't be used as variable names
We can split words with underscores: "long_answer"
ints and doubles: different types of numbers
- The %d and %lf are format specifiers that are used in printf statement to let the
compiler know what data type we need to output
- %d stands for "decimal integer", %lf stands for "long floating point number (a
double)
- Remeber that we have to be very prescriptive when we tell the computer what to do
and that extends to even telling it what types we are printing in C
char:
- The %c format specifier can also be used in printf statement to let the compiler
know what data type we need to output (character)
- %c stands for "character"
- Don't forget that when you declare a char, you enclose it in single apostrophes
to let the computer know that you are using a letter character
printf(): printing out to terminal
- Not just for specific messages we type in advance
- We can also print variables to our display!
- To print out a variable value, we use format specifiers: this is a % symbol
followed by some characters to let the compiler know what data type you want to
print
- %d where the output you'd like to put an int (decimal value, hence %d)
- After the comma, you put the name of the variable you want to write
scanf(): taking something from terminal
- Reads input from the user in the same format as printf
- Format specifiers (%d, %lf, %c) are used in the same way as for the printf
statement
- The & symbol tells scanf the address of the variable in memory (where the
variable is located) that we want to place the value into (more details later in
term)
maths:
- A lot of arithmetic operations will look very familiar in C: adding +,
subtracting -, multiplying *, dividing /
- These will happen in their normal mathematical order
- We can also use brackets to force precedence