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