midterm superdeck Flashcards
Range of a typical short int in C.
-2^15 to 2^15-1
This is 4 bytes.
-32768 to 32767
3 Components of a makefile.
target, dependency, recipe
Makefile, what determines if the recipe is performed?
The timestamp difference between the target and dependency. (I.e. If the dependency has changed, i.e. an extra file that hasn’t changed and doesn’t need to be recompiled).
Bash loop structure.
for condition
do
done
Bash if statement structure.
if
then
else
fi
How might one iterate through every file that starts with the command-line argument in bash?
for i in $1*
General command format
command -[options] [arg1] [arg2] … [argn]
What are the sources of input and output for commands?
stdin 0
stdout 1
stderr 2
These are 3 special files
Where does everything in Unix start?
The root (/).
What are the two main ways to get information about a command?
Using:
man command
or
command –help
cat command.
cat file
Takes input from file and puts it to stdout
Redirection in bash.
Output redirection
> overwrites a file
» appends a file
Input redirection
<
for example
command [arguments] < filename
How to type EOF in bash?
ctrl-d
echo command
Takes input (arguments) and puts it in stdout.
Bash comparison.
==, !=, <, etc for strings.
-eq, -ne, -lt, etc for math.
Bash else if.
if
then
elif
then
else
fi
What are the steps to the full process of building a file?
Preprocessor. Take definition, insert header files, make a program.
Compiler. Syntactically correct program translated into lower form (assembly).
Assembler. Translate assembly language statements into actual instructions. object code or .o file.
Linker. All object files linked together with system libraries to produce executable file.
all in a makefile.
all must be the first target.
Conventionally builds the entire project.
Has one dependency.
No recipe.
clean in a makefile.
Has no dependencies.
Conventionally removes everything made by the build process (executables and .o files)
What is .PHONY: used for?
This says that its dependencies, such as all and clean, are not real targets and do not produce files named after themselves.
Minimum size of int.
2 bytes. 16 bits.
Float and double format specifier.
%f, %e, and %g.
Booleans in C.
_Bool type (since C99 standard).
0 is false and 1 is true (or rather, anything other than 0 is true). false is 0, true is 1.
Making a long int literal.
A number followed by ‘L’. For example, making a long int of 2024 is
2024L
Such as
long int x = 2024L;
Long format specifiers.
Use a preceding lowercase L.
Such as %li
Integer format specifer.
ints
%i, %d, %x, %o
unsinged int
%u, %x, %o
Difference between i++ and ++i
i++ increments i once the expression is complete. ++i increments i before the expression is complete.
These can be combined.
C switch case?
switch (expression)
{
case val1:
statement;
…
break;
....
{
Note, this only works when expression is some form of int.
for statement
for (initial-statement; loop-condition; loop-statement)
C, conjunctions and disjunction.
&& AND, conjunction
|| OR, disjunction
Initializing arrays with value
type name[] = {t1, t2, t3};
or
type name[] = { [0]=t1, [37]=t2, [101]=t3};
The compiler will infer a size when initialized with values.
Initializing multidimensional arrays in C.
type name[][];
Odd notes:
Inner braces are optional when assigning a value, but are strongly preferred for the ease of reading.
You can still do
type name[][] = { [0][0] = t1, [37][42] = t2 };
How does C treat arguments to function?
Always, call by value. C always receives a copy of the argument.
Note that if this argument happens to be a pointer, then it can still alter the original by accessing it with the pointer.
How to ensure a value inside a function remain once the function ends?
Use the static keyword.
How to make a variable accessible everywhere?
Define it in the higher (I’m not certain of the correct term) scope.
How to make an object file with GCC.
Use -c option.
List of useful options for this course in makefiles.
-Wall
-Werror
-std=c99 (double check this)
-pedantic
-g
Variables to use in makefiles.
CC (C compiler)
CFLAGS (Things like -Werror, -pedantic, etc).
CPPFLAGS (C preprocessor flags? I don’t know if we’ve done these yet).
LDFLAGS (libraries like -lm)
Note, -c and -o are kept outside of these.
What must be done before using gdb?
Must have included the -g flag.
gdb commands
step (single step)
next (next line)
info break (list breakpoints)
clear line-or-function-name (clears breakpoint at some line or function)
delete breakpoints (figure it out)
info locals (list local variables)
set var x=4 (sets x to 4 in this example)
bt (prints a stacktrace)
Initializing an instance of a struct
struct struct_name var_name = { t1, t2, t3 };
or
struct struct_name var_name = {.a1=t1, .a3=t3 };
Struct literal.
I think this is correct. Not 100% certain though.
var_name = (struct struct_name) {t1, t2, t3};
Making a single struct.
You can optionally omit the name.
i.e.
struct
{
int p1;
int p2;
} name[10];
Makes 10 of these structs in an array named name (I think that’s what this does).