Week 1 C Flashcards
(23 cards)
What is the difference between machine code and source code?
Machine code is the 0’s and 1’s that the computer understands - the output
Source code is the code we write - the input
How would you create and enter a new directory named ‘hello’ and create a new C file named ‘hi’ using the Command-Line-Interface?
- mkdir hello
- cd hello
- code hi.c
For a C file named hello, there is one file named hello.c and one named hello. What is the difference?
- hello.c is the source code that we write as the input
- hello is the machine (binary) code that tells the computer what to do
What is a header file, and how would you include one?
include <stdio.h></stdio.h>
A header file is a file you need to include to access libraries
What are libraries?
A collection of code that somebody else wrote for you
How would you create a variable in C, what different types are there and how would you print one out?
type name = ….
- e.g., string name = …
Types: string, int, float, bool, double, long, char etc.
Need placeholders to print (%s, %i, %f) - e.g., printf(“hello, %s\n”, name)
List some terminal / command-line commands and what they do
- ls : list files in current folder
- cd : change directory
- cp : make copy of a file
- mkdir : make directory
- mv : move a file from one place to another or rename a file within a directory
- rm : remove file
- rmdir : remove directory
How would you say OR and AND when using conditionals in C?
|| means OR
&& means AND
What different ways can you continuously increment a counter by one in C?
counter += 1
counter++ (only for +/- 1)
What is the declaration domain, what does it look like and what do the different parts mean?
int main(void)
{
}
- int is where the output type is
- void is where the input type is - e.g., so can replace with string, int etc
Void means no input/output - takes no arguments
How do you format functions within a C file?
- In alphabetical order at the bottom of the file
- Prototype (first line of function) at top of file
What 3 things do you need to be thinking about when writing any code?
- Correctness
- Design
- Style
What is one potential limitation of using an integer type?
Integer overflow
- Theres only a finite number of bits - e.g., int uses 32 bits
- So once you go into the 33’rd bit, then it will go to 0 - has overflowed
- Can cause bugs - real world problem with memory - when using very large number
What is Truncation and why is a problem?
Truncation is where you use an integer input and the output is not an integer (is a decimal)
- The program truncates the number to just return the first value
- So need to convert integer to a float
How can floating points be inprecise?
Similar to integer overflow, floating points can run out out available bits - so it becomes imprecise after the 32’nd bit is used up
Name a few of the different data types and variables - what they are used for
- Int : variable used to store an integer - 4 bytes / 32 bits
- Unsigned int : an integer that can take double the amount of positive values but at the expense of no negative values
- Char : variable used to store single characters - 1 byte / 8 bits - assigned by ASCII - e.g., ‘A’ is 65
- Float : variable to store floating point values - real numbers - 4 bytes / 32 bits
- Double : variable to store floats with double precision - so are 8 bytes / 64 bits
- Bool : data type to store boolean value - true or false
- String : used to store variables that are a series of characters
What is void (in C)?
Is a type (but not a data type)
- Cannot assign a value to it but functions have a void return type
What are some arithmetic operators?
+ - / % *
++ / –
+= / -= / *=
What are the two types of boolean expressions?
Logical operators: e.g., OR (||), AND (&&), NOT (!)
Relational Operators: e.g., greater/less than (>/</=<) or Equality ( x == y) / Inequality (x != y)
What is the ternary operator?
’?’
- Can use to replace if statements e.g.,
if expr x = 5, if not then x = 6
int x;
if (expr)
{
x = 5;
}
else
{
x = 6;
}
Can be written as:
int x = (expr) ? 5 : 6;
What are the 3 main types of loops, and when would you use each of them?
While : use when you want a loop to repeat an unknown number of times, possibly not at all - e.g., a video game that you want to keep running
Do-while : use when you want a loop to repeat an known number of times, but at least once - e.g., for prompting for user inputs
- excecutes all lines of code between curly braces once, then if the bool-expr is true it will go back and repeat the process until bool-expr is false
For : use when you want a loop to repeat a discrete number of times - though you may not known the number
- for (int i = 0; i < 10; i++)
What are magic numbers in your code and what is wrong with them?
- Magic numbers are number floating around in your code - e.g., using 52 when tackling problem with a deck of cards
- It wont make sense to others reading your code - and if you change something you’ll have to go back and change that magic number at every point in the code
- Instead create a variable - e.g., for decksize : int deck_size = 52
What is a preprocesser directive or macro in C?
A way to define a constant in C so that no one can change it in your code:
- # define DECKSIZE 52
- Then can just use DECKSIZE throughout code