c Flashcards

1
Q

How does C run?

A

C is a compiled language, so in order for C program’s to run on a machine, we have to translate a text file containing a C program to machine code using another program called a compiler

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

How is the way C runs different to MATLAB?

A

MATLAB interprets scripts one line at a time, while a C program includes of editing and saving the program and then compiling the program, saving the program as an executable file and then running the program. MATLAB does this process automatically.

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

Why does the main function have to return an integer?

A

For the system to determine if the program has finished correctly – therefore using return 0; at the end of main function

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

What is the difference between MATLAB types and C types?

A

MATLAB stores information about the type of every item of data with that data. This allows MATLAB functions and operations to check the data they are working with and decide what operation to do and how to store results.

C only stores the data, so the C compiler needs to know what the type is so it can layout the data in the storage of the executable program it produces & decide which versions of operations to use

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

What are the types in C?

A

int – stores integer types
double – stores floating point double types
float – stores float types
char – stores single characters
C does not have a bool type, therefore
zero == false
non-zero integer == true

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

How to print the different types?

A

%d – int
%.0f – double
%lf – float
%c – char

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

Why is it important to initialise variables?

A

Causes unpredictable behaviour if variables haven’t been initialised, the program will run with unexpected results. If variables are not initialised, they will contain garbage data.

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

Why doesn’t C complain about displaying a char type with %d?

A

C considers char to be an integer type and displays it’s character code

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

What is the <stdio.h> library</stdio.h>

A

stdio.h is the library for standard input and output functions

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

What is the stdlib.h library?

A

stdlib.h is the library for dynamic array functions (such as malloc)

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

Why do we use an ampersand when scanning integers?

A

Variables that are not arrays, e.g. single integers, require ampersand whereas character types are character arrays

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

How are arrays indexed in C?

A

Array indexing in C starts from 0
e.g. vector = [1 2 3]
vector[0] = 1, as the first element’s index position is 0

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

What are static arrays?

A

Static arrays are arrays with a fixed number of elements – the number of elements can’t be increased or decreased once created.

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

What is bounds checking?

A

The process of ensuring a program is not accessing elements outside an array.

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

What is the correct way to access elements inside an array?

A

Use index numbers from 1 to the length of the array minus 1

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

What are some useful functions in C?

A

strlen() – finding the length of a string
strcpy() – copying one string to another
strcat() – concatenate two strings together
strcmp() – comparing two strings

17
Q

What are pointers?

A

Pointers are variables with a data type that stores an address to somewhere in computer memory – a pointer points to something in memory. Pointers are declared with the data type and * before the variable name

18
Q

What is addresses in memory?

A

Addresses in memory are the numeric addresses of bytes in memory – every byte in memory has a numeric address. The computer memory starts at 0 and the highest address varies depending on the amount of RAM installed.

19
Q

What are hexadecimals?

A

Hexadecimals use 0-9 and A-F numbering addresses. Numbers prefixed with 0x means hexadecimal number.

20
Q

Where is the pointer stored for a static array?

A

The pointer for a static array is stored in the variable, and the data for the array is created next to the variable. When accessing the array variable, the pointer is being used to tell the program where the data in the array starts.

21
Q

What are allocation risks in C and defensive programming measures to prevent it from occurring?

A

Allocation risks occur when we run out of available memory or we ask for too much memory at once. Malloc will return NULL if it fails. Defensive programming to look out for this is writing code that checks if the variable is equal to NULL.