Week 1 C Flashcards

(23 cards)

1
Q

What is the difference between machine code and source code?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How would you create and enter a new directory named ‘hello’ and create a new C file named ‘hi’ using the Command-Line-Interface?

A
  • mkdir hello
  • cd hello
  • code hi.c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

For a C file named hello, there is one file named hello.c and one named hello. What is the difference?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a header file, and how would you include one?

A

include <stdio.h></stdio.h>

A header file is a file you need to include to access libraries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are libraries?

A

A collection of code that somebody else wrote for you

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you create a variable in C, what different types are there and how would you print one out?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List some terminal / command-line commands and what they do

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you say OR and AND when using conditionals in C?

A

|| means OR

&& means AND

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What different ways can you continuously increment a counter by one in C?

A

counter += 1
counter++ (only for +/- 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the declaration domain, what does it look like and what do the different parts mean?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you format functions within a C file?

A
  • In alphabetical order at the bottom of the file
  • Prototype (first line of function) at top of file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What 3 things do you need to be thinking about when writing any code?

A
  • Correctness
  • Design
  • Style
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is one potential limitation of using an integer type?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Truncation and why is a problem?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can floating points be inprecise?

A

Similar to integer overflow, floating points can run out out available bits - so it becomes imprecise after the 32’nd bit is used up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Name a few of the different data types and variables - what they are used for

A
  • 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
17
Q

What is void (in C)?

A

Is a type (but not a data type)
- Cannot assign a value to it but functions have a void return type

18
Q

What are some arithmetic operators?

A

+ - / % *
++ / –
+= / -= / *=

19
Q

What are the two types of boolean expressions?

A

Logical operators: e.g., OR (||), AND (&&), NOT (!)

Relational Operators: e.g., greater/less than (>/</=<) or Equality ( x == y) / Inequality (x != y)

20
Q

What is the ternary operator?

A

’?’
- 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;

21
Q

What are the 3 main types of loops, and when would you use each of them?

A

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++)

22
Q

What are magic numbers in your code and what is wrong with them?

A
  • 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
23
Q

What is a preprocesser directive or macro in C?

A

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