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
What is a switch statement and what is the important thing you need to remember about it?
switch (argument){
case 1:
break;
case 2:
break;
default:
}
So above is the structure of the switch case is a faster version of a nested if-else statement. The break statement is necessary as otherwise it will keep looping and the default is optional but it is essentially the base case
How does a for loop work?
for ( int i = some number, i is < / > / <= / >=, i++/i–){
contents of loop here
}
A for loop takes a initialised variable, a terminate variable (so when the loop needs to stop and an increment variable
How does a while loop work?
int i = 0
while (arguement){
contents of loop here
}
A while loop will basically say while true run loop, once the loop is not true it will end/break
How does a do-while loop work?
int i = 0
do {
contents of loop here
}
while (arguement);
Similar to a while loop but the do is only done as long as the while condition is true
What are pointers?
Pointers are another data structure/type, the represent the addresses in memory space and not an actual value. To get set a pointer we use *variable to get the address we use &variable. For example if i have int x = 7, meaning 7 is an integer and do return x it will give me 7. If i do &x it will return the address of x. Now if i set x as a pointer int *x and called x it would give me the address but if i called *x it would give me 7 as in this case *x is a deferencing of the value stored in the address which is 7 in this case
What do functions do in C?
Functions are sub-programs that take a number of parameters and return a results IF it needs to. I say if because in the function you need to set a return type which is int, float, char, etc which then means that function needs to have a return that will return the respective value. But you can also have void, void essentially means nothing needs to be returned (no return required)
What do arrays do in C?
Arrays are intialised by doing int name[size of array) for example int field[10] which means there is an array of size 10 that starts a position 0 and ends at position 9.
You can add values to array by doing name[location] = value and you can loop through arrays. REFER TO EXAMPLE ON SLIDES
What are strings in C?
Strings in C are not a data type/structure but rather an array of characters. Each string much be terminated by a null character \0. Note that the null character needs and/or takes a position in the the array and thus needs to be accounted for when setting the size or adding to the array.
EXAMPLES
REFER TO SLIDES FOR EXAMPLES