C Flashcards

You may prefer our related Brainscape-certified 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
Q

What are the increment and decrement operators?

A

increment: ++
decrement: - -

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

How would you use increment/decrement operators on say variable ‘var’?

A

It is recommended in COMP9024 to put the operator after var

var++
var–

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

What data type can be used with increment and decrement operators?

A

only integers

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

What is precedence and associativity?

A

Operators with higher precedence are executed first and operators with lower precedence are executed last

(), * and / are calculated first before + and -

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

How can we find out how much storage each data is occupying in the memory?

A

Use sizeof(variable) operator

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

How do we print sizeof() calculations?

A

Use format specifier “%zu”

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

What is the measurement does sizeof() return?

A

Bytes

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

What kinds of type conversions are there?

A

(1) Implicit Type Conversion
(2) Explicit Type Conversion - manually convert one type to another

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

What happens if we try to assign a double value to an int variable?

E.g.

int number = 8;
number = 35.48

A

We’ll get only 35

Double values are larger (8 bytes) than integer values (4 bytes) so we’ll lose data

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

How do we manually convert one data type to another?

A
34
Q

What function allows us to take input from a user? How does it do that?

A

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
Q

How can use booleans in our code in C?

A

import true and false using #include <stdbool.h></stdbool.h>

create boolean using bool

36
Q

In C, boolean values are represented in terms of which integer values?

A

true = any values except 0
false = 0

37
Q

What format specifier do we use to represent boolean value?

A

%d

38
Q

How do we write a boolean statement in C comparing

int age = 25;

Does age equal 25?
Does age not equal 25?

A
39
Q

Name the 3 logical operators in C programming

A

&& - AND
(age > 17) && (salary > 4000)

|| - OR
(age > 17) || (salary > 4000)

! - NOT
!(age > 17)

40
Q

How are if..else statements written in C?

A
41
Q

How are if..else if .. else statements executed in C?

A
42
Q

Create an array numbers containing integers 1, 2, 3, 4, 5

A
43
Q

How to access first element of this array?

int numbers[5] = {1, 2, 3, 4, 5};

A

numbers[0];

or more generally, numbers[index]

44
Q

How do you write a switch statement?

A
45
Q

What does the while loop do?

A

repeat a block of code until the boolean expression of the loop is false

46
Q

What does the for loop do? Identify parts of a for loop

A

run a block of code for a certain number of times

47
Q

while vs for

A

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
Q

Can we omit an array size?

A

Yes, C will automatically keep track of it

int array[] = {1, 2, 3, 4, 5};

49
Q

What happens if you assign Less Elements Than Size of the Array?

A

The remaining spaces will be filled with a random number

50
Q

What happens if we do this?

declare array first then initialize all elements together after declaration

A

We’re not allowed to do this

51
Q

How do we print something in C?

A
52
Q

What is the output of this print code?

A

The cube of 3 is 27.

The first %d is for the arg1
the second %d is arg2

53
Q

How to code this output in C?

Your “login ID” will be in the form of z1234567.

A
54
Q

recall how to write a do … while loop

A
55
Q

do … while vs while loop?

A

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
Q

what metaphor can we relate the while and do ... while loop to?

A

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
Q

return-type functionName(void) {
// code block
}

what does void in the argument mean?

A

function has no arguments

58
Q

float vs double data types

A

float is a lower precision decimal number (less number of digits to the right of the decimal place), double is precise

59
Q

what is this called (at the beginning of a C program)?

A

symbolic constant

60
Q

what is a symbolic constant

A

typically defined at the top of a C program where the variable (MAX) is followed by associated value (20)

61
Q

what happens when the symbolic constant is read by the compiler’s pre-processor?

A

all occurrences of variable is replaced by associated value

62
Q

when will the symbolic constant NOT be replaced?

A

(1) occurs inside string
(2) as part of another name

63
Q

when would it be useful to use symbolic constants

A
  • avoid burying “magic numbers” in the code (slide 58)
  • make the code easier to understand and maintain (slide 57)
64
Q

what styles would be useful in COMP9024

A

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
Q

how many spaces would the string “hello” take up in memory? why?

A

6 because the last character is the end of string delimiter \0

66
Q

what are 2 different ways we can initialise a string array?

A
67
Q

how do we use the construct atoi()?

A

include <stdlib.h></stdlib.h>

say it as “a to i”

n = atoi(str)

68
Q

what does atoi() do?

A

converts string to integer to use in computation

69
Q

what does typedef do?

A

allows us to give meaningful names to data types

70
Q

what are 2 ways to initialise multi-dimensional arrays?

A
71
Q

What is a structure in C?

A

collection of variables (can be of different types) under a single name

72
Q

how do we define a structure?

A

use struct

73
Q

How do we access components of the structure?

A

Using dot operator

Person.name

74
Q

When we defining a structured data type, is any memory allocated?

A

No

75
Q

After we’ve defined a structured data type, how do we allocate memory?

A

Declare a variable

76
Q

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

A
77
Q

When we define a struct, we are basically creating a ___

A

data type like double and int

78
Q

What are two ways we can create struct variables?

A
79
Q

Is this valid code to create struct variables?
Why/why not?

A

With typedef keyword, person1 and person2 become aliases instead of struct variables

80
Q

How can you create struct variables using typedef

A
81
Q

What does typedef do?

A

Creates an alias name for struct

82
Q

Why do we use structs?

A

if we have to store multiple attributes of related data, structs make our code easier to write and understand, improves readability