C Flashcards
Which of these does C have?
- Explicit memory management
- Runtime error checking
- Exception handling
- ANSI/ISO standards
Has:
- Explicit memory management
- ANSI/ISO standards
Does not have:
- Runtime error checking
- Exception handling
What are the 6 things that define C?
- Imperative (describes computation in terms of statements that change a program state)
- Procedural/functional
- Compiled
- Statically (types are checked before runtime), weakly (supports implicit type conversions) typed
- Available on just about every platform (portable)
- Very fast
How are C programs compiled and linked?
They are compiled into objects (.o) and then linked into an executable (.exe)
What are the base datatypes and their associated memory allocation, range and precision?
- Unsigned char (1 byte, 0 to 255)
- Char (1 byte, -128 to 127)
- Unsigned short int (2 bytes, 0 to 65, 535)
- Short int (2 bytes, -32,768 to 32, 767)
- Int (4 bytes, -2,147,483,648 to 2,147,483,647)
- Long int (8 bytes, +/- 9 x 10^18)
- Float (4 bytes, +/- 3.4 x 10^-38, +/- 3.4 x 10^38, 6 digits)
- Double (8 bytes, +/- 1.07 x 10^-308, +/- 1.07 x 10^308, 15 digits)
- Long double (16 bytes, +/- 3.4 x 10^-4932, +/- 3.4 x 10^4932, 18 digits)
How do you show variables in printf statements?
Char - %c
Int - %d
Float/double - %f
Long double - %Lf
Why does “char ch = ‘A’” print 65 if trying to display ch as %d?
It follows the ASCII table, and the follows for all other characters
What issue arises with if statements in C?
There is no explicit boolean datatype in ANSI C. A condition is evaluated to an int value: 0 = false, anything else is true.
What is the difference between “a = 0” and “a == 0” in an if statement?
“a = 0” means that the if statement equates to 0, or “if(false)” and therefore cannot be true. “a == 0” means that does a hold the value that is 0. If it does then the statement is true.
What do | and & represent?
Bitwise OR and Bitwise AND respectively
What do || and && represent?
OR and AND respectively
What do BREAK and CONTINUE mean in a loop?
BREAK: Jump to the next command after the end of the loop
CONTINUE: Jump to the start of the next increment
How does C store a string?
A string is an array of characters
How are strings and characters represented?
Characters use single quotes and strings use double quotes.
How are strings terminated in memory?
Using the null character. All strings in C are “null terminated”
What does an array contain if it is uninitialised?
It will contain random stuff
What does weakly typed mean?
Supports implicit type conversions
What three things must a function have in C?
- Have a uniquely named group of program statements (so no overloading)
- Accept zero or more arguments or parameters
- Return zero or one value to the calling code
How is a function called that returns an integer?
int doubleIt(int a Number) { … }
int b = doubleInt(3)
Why do functions need to be declared like variables?
- The (one-pass) compiler needs to know a function’s definition before it is called
- Functions may appear in any order in the program module (main function is first by strong convention)
- The functions may call each other (so there is no ordering of the function definitions that defines them before they are called)
- The code for the function might not be in this program module
Are global variables used in C?
It is bad practice to use global variables. Variables only exist when they are in scope.
What does the keyword “static” do?
It initialises the variable when the function is first called and retain their value when they go out of scope
What is a shell in Linux and Unix?
A command-line interpreter for the OS and the outer layer of the OS surrounding the kernel
How does the section of code work line by line?
char ch = ‘A’;
char *p;
p = &ch;
- Variable ‘ch’ is a character
- Variable ‘p’ is a pointer to a character
- ‘p’ is assigned to be the pointer to ch
If x is any datatype, then what is &x?
&x is the location in memory (its ‘address’) where x is currently stored
If x is a pointer, then what is <datatype> *x</datatype>
<datatype> *x declares x to contain the memory address of a variable of type <datatype>.
*x says: go to the memory address stored in x and bring back the value (of type <datatype>) stored there
</datatype></datatype></datatype>
How much memory is used to store a pointer in Windows 32-bit and Windows 64-bit respectively?
4 bytes and 8 bytes
What two runtime errors can occur from using pointers?
- Memory fault
- Segmentation fault
What causes runtime errors using pointers?
- Trying to access memory to which it is not allowed access
- Trying to access memory in a way that is not allowed (e.g. trying to overwrite the OS)
What possible solutions may be used to solve the issue of at compile time not knowing how much memory is needed at runtime?
- Declare an array whose size is decided at runtime (But there is no way of knowing how much free contiguous memory is available. And you might not know how much is required before declaring the array)
- Process the data in smaller chunks (Possible, but this requires a lot of extra programming).
- Dynamically allocate memory at runtime using pointers (Here we can check whether memory allocation was successful).
How can memory be allocated to a variable at runtime?
(Example is for an int)
int *p = NULL;
p = (int *)malloc(23); //23 bytes
p = (int )malloc(23sizeof(int)); //23 integers
What should happen if there is not enough memory?
You must always trap the fact that memory was not allocated
What happens to a ‘normal’ variable when it goes out of scope?
Its allocated memory is released
What happens to dynamically allocated variables when they go out of scope?
A memory leak occurs. Allocated memory is not released, even though the variable goes out of scope.
What must be done to prevent a memory leak?
The memory must be explicitly freed before the variable goes out of scope.
What happens if you attempt to free memory that has been freed, or memory not allocated by malloc?
‘behaviour is undefined’
int main ()
{
char c = ‘A’;
printf(“Before: c = %c\n”,c);
myFunctionA(c);
printf(“ After: c = %c\n”,c);
}
void myFunctionA (char ch)
{ ch = ‘B’; }
Why is the value of c not changed?
It is not updated because ch is a copy of c.
int main ()
{
char c = ‘A’;
printf(“Before: c = %c\n”,c);
myFunctionA(c);
printf(“ After: c = %c\n”,c);
}
void myFunctionA (char ch)
{ ch = ‘B’; }
How do you update the value of c?
In order to update the value of a parameter within a function it must be passed as a pointer. The value of the thing passed cannot be changed. The memory address where c is stored should be copied, (and is not updated), not c’s value.
int main ()
{
char c = ‘A’;
printf(“Before: c = %c\n”,c);
myFunctionA(&c);
printf(“ After: c = %c\n”,c);
}
void myFunctionA (char *ch)
{ *ch = ‘B’; }
int main ()
{
char *p = NULL;
myFunctionB(p);
printf(“p = %s\n”,p);
}
void myFunctionB (char *str)
{
strcpy(str,”Hello”);
}
Why does this code produce a ‘Memory fault (coredump)’?
p is uninitialised, so contains random stuff. The random stuff is then copied from p into str when the function is called. When strcpy is called, the text is stored in whatever random address is in p and hence str. The result is likely to be a memory fault, depending on what that random address is.
int main ()
{
char *p = NULL;
myFunctionB(p);
printf(“p = %p: %s\n”,p,p);
}
void myFunctionB (char *str)
{
if ( !(str = (char *)malloc(6)) )
{
printf(“Insufficient memory\n”);
exit(-1);
}
strcpy(str,”Hello”);
printf(“str = %p: %s\n”,str,str);
}
Why does malloc change the value of str, but value of p remains unchanged?
The random stuff is copied from p into str when the function is called. malloc reserves 6 bytes of memory and the memory address of the first byte is stored in str. So str is correctly updated but p is not changed.
Can you have a pointer to a pointer and why?
Yes, as a pointer is just another datatype
int main ()
{
char *p = NULL;
setString(&p);
printf(“p = %p: %s\n”,p,p);
}
void setString (char str)
{
if ( !(str = (char )malloc(6)) )
{
printf(“Insufficient memory\n”);
exit(-1);
}
strcpy(str,”Hello”);
printf(“str = %p: %s\n”,str,str);
}
Why does this code work correctly?
The memory address of p is stored at str, making *str the same as p. malloc reserves 6 bytes of memory, and the memory address of the first byte is stored in *str. Therefore, *p is the memory address where the text is held.
How do you pass arguments to the main function?
This will be done in the command line. In order to define main,
‘int main( int argc, char **argv )’
must be written. argc is the number of arguments passed, and **argv is the array of string values. These names are standard.
What is ‘parameter 0’ when passing arguments into main?
It is always the name of the executable.
int Plus (int a, int b) { return a+b; }
int Minus (int a, int b) { return a-b; }
int Multiply (int a, int b) { return ab; }
int Divide (int a, int b) { return a/b; }
int doFunction (int a, int b, int (myFunc)(int,int) )
{ return myFunc(a, b); }
What do these set of functions do?
The third parameter of ‘doFunction’ is a pointer to a function that take two int values as parameters and returns an int value. This can be any of the first 4 functions.