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