C Flashcards

1
Q

Recall the basic structure of a C program

A

int main function

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

How to make a comment in C

A

//

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

What’s the formal name for fixed data (values) that we can use directly in a C program?

A

literals

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

What are the data types in C (literals) - give examples

A

Basic data types (refer to image)

arrays
string (i.e. array of characters)

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

How do we declare variables in C?

A

Depends on the data type of the variable we want to declare

(1) Integer Literals
int age;

(2) Floating-point Literals
double salary;

(3) Character Literals
char letter;

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

How do we properly end a C statement?

A

Use semi-colon ;

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

How do we print

A

printf(“”);

prints anything present inside the quotation marks

“%d”, age
prints anything present inside the quotation mark

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

What is %d in this C statement?

printf(“%d”, age);

A

%d - format specifier for int data types
value of age replaces %d

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

What are the respective format specifiers for each of the literal data types?

A

(1) Integer Literals
%d

(2) Floating-point Literals
%lf
(‘L’ f not 1)

(3) Character Literals
%c

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

How do you print this:

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

How many characters can the character variable store?

A

a single character inside single quotes

char letter = ‘s’;
NOT char letter = “s”;

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

How do we change the value stored in a variable?
E.g. if we initially declare int age = 25 but want to change age to 35

A

age = 35;

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

When we change a value stored in a variable, what should we note?

A

The new value must match the data type we initially declared for that variable.

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

How do you name variables made up of 2 words?

A

camelCase

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

Give some examples of illegal variable names

A

❌ spaces
❌ use of symbols (except alphabets, numbers and underscore)
❌ variable name starts with a number
❌ names already part of C syntax e.g. if, else etc

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

How can we create multiple variables together in a single line?

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

What do pointers allow us to do?

A

Work with memory addresses

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

After creating and storing value in variable, how can we access the memory address of that value?

e.g.
int var = 13;

A

&var

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

What is a pointer?

Use variable ‘pt’ as an example

A

a special symbol that stores the address of a variable rather than a value

int* pt;

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

How can you assign an address to a Pointer?

Use int number = 36; as example

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

Label the different parts of this C function

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

What are some assignment operators?

A

=
+= (same as accumulator pattern in Python)
-=
*=
/=
%=

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

How to limit the number of 0s when formatting floating points?

A

printf(“%.2lf”)

2 is the number of zeros we want to pad out the floating point with

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

What result is returned when we use division operator on int and floating points?

e.g. 12 / 8
12.0 / 8.0

A

Integers - returns only the quotient (1)
Floating - returns exact result (1.50)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
What are the increment and decrement operators?
increment: ++ decrement: - -
25
How would you use increment/decrement operators on say variable 'var'?
It is recommended in COMP9024 to put the operator after var var++ var--
26
What data type can be used with increment and decrement operators?
only integers
27
What is precedence and associativity?
Operators with higher precedence are executed first and operators with lower precedence are executed last (), * and / are calculated first before + and -
28
How can we find out how much storage each data is occupying in the memory?
Use sizeof(variable) operator
29
How do we print sizeof() calculations?
Use format specifier "%zu"
30
What is the measurement does sizeof() return?
Bytes
31
What kinds of type conversions are there?
(1) Implicit Type Conversion (2) Explicit Type Conversion - manually convert one type to another
32
What happens if we try to assign a double value to an int variable? E.g. int number = 8; number = 35.48
We'll get only 35 Double values are larger (8 bytes) than integer values (4 bytes) so we'll lose data
33
How do we manually convert one data type to another?
34
What function allows us to take input from a user? How does it do that?
scanf() e.g. scanf("%d", &age); %d - format specifier that represents the type of input data &age - represents the memory address of the age variable The scanf() function takes int input (specified by %d) from the user and stores it in the memory location of the age variable.
35
How can use booleans in our code in C?
import true and false using #include create boolean using bool
36
In C, boolean values are represented in terms of which integer values?
true = any values except 0 false = 0
37
What format specifier do we use to represent boolean value?
%d
38
How do we write a boolean statement in C comparing int age = 25; Does age equal 25? Does age not equal 25?
39
Name the 3 logical operators in C programming
&& - AND (age > 17) && (salary > 4000) || - OR (age > 17) || (salary > 4000) ! - NOT !(age > 17)
40
How are if..else statements written in C?
41
How are if..else if .. else statements executed in C?
42
Create an array numbers containing integers 1, 2, 3, 4, 5
43
How to access first element of this array? int numbers[5] = {1, 2, 3, 4, 5};
numbers[0]; or more generally, numbers[index]
44
How do you write a switch statement?
45
What does the while loop do?
repeat a block of code until the boolean expression of the loop is false
46
What does the for loop do? Identify parts of a for loop
run a block of code for a certain number of times
47
while vs for
Based on my experience with Python problems, it seems `while` loops are usually used to make an infinite loop and is best for a changing data source. We can specify when to break the loop and restart it from the top A `for` loop hardcodes conditions which is maybe more suited for an unchanging data source
48
Can we omit an array size?
Yes, C will automatically keep track of it int array[] = {1, 2, 3, 4, 5};
49
What happens if you assign Less Elements Than Size of the Array?
The remaining spaces will be filled with a random number
50
What happens if we do this? declare array first then initialize all elements together after declaration
We're not allowed to do this
51
How do we print something in C?
52
What is the output of this print code?
The cube of 3 is 27. The first %d is for the arg1 the second %d is arg2
53
How to code this output in C? Your "login ID" will be in the form of z1234567.
54
recall how to write a do ... while loop
55
do ... while vs while loop?
do ... while ensures the code body will be run before checking the condition while loop code body will only run if the condition is true
56
what metaphor can we relate the `while` and `do ... while` loop to?
Fixing equipment I fix equipment, then check whether it's actually fixed. If it's not fixed, I continue fixing While Is this device safe to use? If a tool is safe to use I continue using it
57
return-type functionName(void) { // code block } what does void in the argument mean?
function has no arguments
58
float vs double data types
float is a lower precision decimal number (less number of digits to the right of the decimal place), double is precise
59
what is this called (at the beginning of a C program)?
symbolic constant
60
what is a symbolic constant
typically defined at the top of a C program where the variable (`MAX`) is followed by associated value (`20`)
61
what happens when the symbolic constant is read by the compiler's pre-processor?
all occurrences of variable is replaced by associated value
62
when will the symbolic constant NOT be replaced?
(1) occurs inside string (2) as part of another name
63
when would it be useful to use symbolic constants
- avoid burying "magic numbers" in the code (slide 58) - make the code easier to understand and maintain (slide 57)
64
what styles would be useful in COMP9024
Style considerations that do matter for your COMP9024 assignments: * consistent indentation (3 spaces throughout, or 4 spaces throughout, do not use TABs) * keep functions short and break into sub-functions as required * use meaningful names (for variables, functions etc) * use symbolic constants to avoid burying "magic numbers" in the code * comment your code "as much as possible"
65
how many spaces would the string "hello" take up in memory? why?
6 because the last character is the end of string delimiter \0
66
what are 2 different ways we can initialise a string array?
67
how do we use the construct atoi()?
say it as "a to i" #include n = atoi(str)
68
what does atoi() do?
converts string to integer to use in computation
69
what does typedef do?
allows us to give meaningful names to data types
70
what are 2 ways to initialise multi-dimensional arrays?
71
What is a structure in C?
collection of variables (can be of different types) under a single name
72
how do we define a structure?
use struct
73
How do we access components of the structure?
Using dot operator Person.name
74
When we defining a structured data type, is any memory allocated?
No
75
After we've defined a structured data type, how do we allocate memory?
Declare a variable
76
How can we use a structure that's passed as a parameter to a function? E.g. a function that prints the date using DateT structure
77
When we define a struct, we are basically creating a ___
data type like double and int
78
What are two ways we can create struct variables?
79
Is this valid code to create struct variables? Why/why not?
With `typedef` keyword, `person1` and `person2` become aliases instead of struct variables
80
How can you create struct variables using typedef
81
What does typedef do?
Creates an alias name for struct
82
Why do we use structs?
if we have to store multiple attributes of related data, structs make our code easier to write and understand, improves readability