Exam Flashcards
Is C strongly typed? Why?
No, C is weakly typed
You can convert between data types (eg casting pointers)
What is gcc?
GNU Compiler Collection is a compiler for C and C++
Is C statically or dynamically typed?
Statically - you must declare the datatype of a variable
How much memory does it require to store a character?
Unknown - variable memory space is machine specific
How do you store a boolean?
There is no boolean datatype in C
You have the equivalent by using 0 to represent False and any other number to represent True
What is the missing line, for ANSI standard C?
int main(){ for ( i=0; i<10; i++ ){ /* some code */ } }
int i;
In ANSI standard, the iterative variable must be declared before use in the for loop
True or False, it is guaranteed that arrays are stored in contiguous memory?
True
Strings are just _____________
an array of characters
Which string function returns the length of the string?
strlen(string)
Which string function copies a string ‘string1’ into ‘string2’?
strcpy(string2, string1)
Which string function concatenates string2 onto the end of string1?
strcat(string1, string2)
What string function would you use to determine if string1 and string2 were equal?
strcmp(string1, string2)
Note that this returns 0 (which means false) if the strings are equal
How can you use string functions in your C program?
#include <string.h>
What are four uses for random numbers?
Simulations
Machine Learning
Cryptography/Codes
Games
What are some characteristics of rand()?
The number returned is an integer
The number is very large
The number is the same each time the program is run
What does this code do?
int i; for (i=1; i<=10; i++){ printf("number %02d: %f\n",i,10.0*float)rand()/RAND_MAX); }
Prints 10 random numbers, between 0 and 10
How would you ensure that rand() gives different numbers every time a program is run?
You should seed the random number generator using:
srand(/*current time*/)
What is the best method for generating random numbers?
Using the GNU Scientific Library (GSL)
What is the definition of a function?
A function is:
- a uniquely named group of program statements
- which accepts zero or more arguments
- and returns zero or more values to the calling code
What is a function prototype? Why use them?
Functions must be declared before being defined
The compiler (which is one-pass) needs to know a function definition’s format before it is used
What is Scope?
Variables that are declared within a function’s scope are only accessible within the function
In general, it is very bad practice to use global variables/scope
What is the name for a function which does not return any values?
Procedure
What is a static variable? How are they used? Give an example use for it
Static variables are initialised once, when the function is first called. They retain their value when they go out of scope.
They are declared using the static
keyword
An example use would be counting page numbers within a function called ‘turnPage’
What is the definition of a pointer?
A variable which stores the address of another variable
What does &x
mean?
The location in memory (its “address”) where x is currently stored
What does *x
mean?
go to the memory address stored in x and bring back the value stored there
What is a void pointer?
A void pointer can point to any datatype, but must be cast when used
What are two runtime errors? Why are they caused?
Memory Fault and Segmentation Fault
Trying to access memory which is not allowed access. Trying to access memory in a way which is not allowed
When using malloc()
, what must you do?
Ensure that malloc()
was successfulfree()
the memory when you no longer have need for it
What does malloc() do?
Dynamically allocates memory at runtime, returning a void pointer to the memory that was allocated
What does pass by value mean? What is the opposite? How could this be achieved?
Pass by Value - creates a copy of the value held into the function, the original value remains unchanged
The opposite is pass by reference. In C, In order to update the value of a variable within a different function, it must be passed as a pointer.
How would you pass arguments to the main function?
int main(int argc, char **argv){
argc - the number of arguments passed
argv - the array of string values
What is scanf()
? What is it used for?
Returns the number of data items successfully assigned a value
It is like the reverse of printf()
, almost like input()
from python
What is a Stream?
- An abstraction of a file
- Is buffered
- May be text or binary
How do you open a stream? What else must you always do?
file = fopen("fileName.txt", "fileMode");
You should always ensure that it was successful, and close the stream when are finished
In C, what are the three standard streams?
stdin - input from the keyboard
stdout - output to the terminal (normal messages)
stderr - output to the terminal (error/warning messages)
What is a compiler directive? Why use them?
An instruction to the compiler for an action which occurs before compilation
Used for:
* Giving your programs access to useful library functions and values
* Controlling what code gets compiled
* Defining macros to make your code more readable
* Defining macros for values that might change in the future