Lecture 9 - Programming Flashcards
What is the difference between C and Assembly (What are the advs of high level languages (ie C))?
- Machine independent - programs can be ported to different computer systems
- Easier to program and debug - cheaper software development (one line of C is several lines of assembly)
What does C do to run the code?
Essentially C checks everything first, hence detecting errors at the very beginning. C will do this by translating the code into machine code through a compiler to the assembler of the first hardware and then into machine code on the second hardware.
What is the structure of C code?
Program starts with the main function, but also has a header and a body. The header specifies return type, function name and any parameters. The body is inclosed in the curly brackets and contains the code that needs to be run in the functions as well as the return statement
What does #include do?
include essentially includes and initialises the header file that is needed
What are the 5 common operations used in C?
+ (addition), - (subtraction), * (multiplication), / (division) and % modulo (which is division but instead of returning the division is returns the remainder of said division eg 2 % 2 = 0 but 3 % 2 = 1)
What is i++ and i–?
These are known a post-increments where it essentially increments or decremenets i after some the function has run you can also do pre-increments which is the same but just increment before the function has run and look like ++i or –i
What are the common data types?
int, float and char (remember that C is case sensitive) as well as local (variable declared inside the function) and global (variable declared outside the function)
How do you print in C and what are the variable arguments for each data type?
to print in C yoiu can use printf, the arguments you need to include are:
1. %c for char values
2. %d for decimal
3. %f for floating points
4. %s for string
You can also use \n for newlines, \t for tabulator spaces and \a for bell/alert sound
What is the difference between ‘=’ and ‘==’
’=’ is to assign values to an variable whereas ‘==’ is used for comparision between values
How do you type cast in C?
If you want to type cast in C you need to add the type in front of the value of you want to change. For example char ‘A’ = A but if i do int ‘ A’ = 65 based on the ASCII values
What are the comparison operators?
Comparision operators have a boolean result either false (0) or true (1). We can use <, >, <=, >=, == and !=
What are the logical expressions?
Logical expressions have a boolean result either false (0) or true (1). We can use && to represent and, || to represent or and ! to represent not
What are the bitwise logical expressions?
Bitwise logical expressions result in an integer result. We can use & to represent and, | to represent or, ^ to represent xor (exclusive or) and ~ to represent not. An example of this is x & y which is x and y. So when x = 1101 and y = 0101 then this just means x + y given the understanding is just 1101 + 0101 which is equal to 0101
C vs Assembly comparison in code
NEED TO REFER TO SLIDE EXAMPLES
What is the structure of if-else in C?
if (argument in here) {
}
else{
}
You can also have an else if