c Flashcards
How does C run?
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 is the way C runs different to MATLAB?
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.
Why does the main function have to return an integer?
For the system to determine if the program has finished correctly – therefore using return 0; at the end of main function
What is the difference between MATLAB types and C types?
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
What are the types in C?
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 to print the different types?
%d – int
%.0f – double
%lf – float
%c – char
Why is it important to initialise variables?
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.
Why doesn’t C complain about displaying a char type with %d?
C considers char to be an integer type and displays it’s character code
What is the <stdio.h> library</stdio.h>
stdio.h is the library for standard input and output functions
What is the stdlib.h library?
stdlib.h is the library for dynamic array functions (such as malloc)
Why do we use an ampersand when scanning integers?
Variables that are not arrays, e.g. single integers, require ampersand whereas character types are character arrays
How are arrays indexed in C?
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
What are static arrays?
Static arrays are arrays with a fixed number of elements – the number of elements can’t be increased or decreased once created.
What is bounds checking?
The process of ensuring a program is not accessing elements outside an array.
What is the correct way to access elements inside an array?
Use index numbers from 1 to the length of the array minus 1
What are some useful functions in C?
strlen() – finding the length of a string
strcpy() – copying one string to another
strcat() – concatenate two strings together
strcmp() – comparing two strings
What are pointers?
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
What is addresses in memory?
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.
What are hexadecimals?
Hexadecimals use 0-9 and A-F numbering addresses. Numbers prefixed with 0x means hexadecimal number.
Where is the pointer stored for a static array?
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.
What are allocation risks in C and defensive programming measures to prevent it from occurring?
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.