Lecture 5 - Introduction to C programming Flashcards

1
Q

What does it mean that C is a procedural language?

A

Programmers think in terms of operations to be done and supply data for the operation

add(a, b)

In object-oriented languages programmers think of the objects to be operated on and then what operation to be performed

rect.area()

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

What are the built-in data types in C

A

char (8 bit)
short (16)
int (16, 32, 64)
long (32, 64)
float (32)
double (64)

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

What is the syntax when declaring a pointer?

A

int *p;

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

How do you put an address in a pointer variable?

A

p = &a;

&a: gives address of a
a must be the same type as the pointer-type

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

How to change a value pointed to by a pointer?

A

p = &a
*p = 5

The last line changes the value of a

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

What is two’s complement?

A

Example using negative short:
short a = -65;

65 = 00000000 01000001

Two’s complement:
- flip all the bits
- 11111111 10111110
- Add 1 to the flipped number
- 11111111 10111111
- This is the two complement of a

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

What is two’s complement used for in C memory mapping

A

When having negative values (for example a negative short), it is represented as the two’s complement of the positive value.
This way the signed bit is 1, indicating a negative value. We get the magnitude of the number by taking the two’s complement again.

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

What happens if a short is assigned to an int?

A

As the short is only 8 bits, to keep the sign-bit the same, the value is sign extended.
Copy the upper bits to the rest of the higher order bits in the int

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

What happens if an integer (32 bits) is assigned to a short (16 bits)?

A

If the integer is large enough to take more than 16 bits, only the 16 LSB are copied to the short, and the higher order bits are discarded.

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

How many bits are used to hold pointer values?

A

Enough bits to represent the addresses. These are the same across all pointer-types, as the addresses of ints are just as large as addresses of pointers

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

What does the types of the pointers do?

A

When writing to a value pointed to by a pointer, the pointer type tells the program how many bits to represent this value in.

short *p;
*p = 65;

Here we know 65 needs to be represented in 16 bits

int *p
*p = 65

Here 65 would be represented by i.e 32 bits

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

How can you cast pointers to point to different data types?

A

int a;

short p = (short) &a

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

What is the difference between these to statements:

int a = 65
short b = a

int a = 65
short b = (short) &a

A

In the first, the lower bytes of a is copied to b

In the second, the 2 higher order bytes are copied to b

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

How is structs declared?

A

struct structName {
int x, y;
} variableName;

struct structName varName;

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

How do you access struct values?

A

Struct variable:
s.x

Struct pointer:
s->

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

How is a struct pointer declared?

A

struct structName *varName

17
Q

How are arrays declared in C?

A

int a[] = {x1, x2, x3}
The amount of elements decides the array size

int a[10];
Here the size is declared directly

18
Q

What does a represent in this statement

int a[] = {x1, x2, x3}

A

a is the starting address of the array

a = &a[0]
*arr = a[0]

19
Q

What does 3 represent in the statement below:

a[3] = 4;

A

Add 3 bytes*sizeof(element) to the base address a.
Go 3 elements ahead in the array

20
Q

What does -3 represent in this statement:

a[-3] = 10

A

Start at base address a and go 3 elements backwards, and write the value 10 there

21
Q

What is pointer arithmetic?

A

When you add something to a pointer, you add n times the size of the element that the pointer points to

22
Q

Explain this expression:
*p++

A

Take value pointed to by p, then increment pointer to next element

23
Q

Explain this expression:
*++p

A

Increment pointer, and take the value of the incremented pointer

24
Q

Explain this expression:
(*p)++

A

Increment the value pointed to by p, p is unchanged

25
Q

What is static memory allocation?

A

The compiler knows exactly how much memory needs to be allocated

26
Q

How can we dynamically allocate memory

A

use malloc, it allocates a chunk of memory at run time and returns the address

27
Q

What are memory leaks?

A

When dynamically allocated memory is not freed. This memory will become unavailable to the program. This can result in a program running out of memory and crash

28
Q

What type of memory is stored on the heap, stack and static

A

Heap: Dynamically allocated data
Stack: Function/method local variables
Static: For data living during the whole program lifetime

29
Q

What is the memory layout

A

Stack: Managed by compiler, grows down
Heap: Managed by programmer, grows up
Static: Initialized on program start
Instruction: Initialized on program start

When stack and heap meet, there are no more available memory