111 Flashcards
(143 cards)
What is a pointer
variable that contains the address of another - signified by *
used for passing variables to functions by ref
size is always system’s memory address size
How do you get the memory address of var?
&var
A string in C is…
an array of characters terminating with \0
indicated by %s
what do you use to indicate a a) string, b) decimal?
a) %s b) %d
How do you compare 2 strings in C?
using strcmp() - returns -1/0/1 (less than/equal to/greater than) based on ASCII codes
How do we dynamically allocate space in memory (C)?
using malloc(int) to allocate and free(var) to deallocate
What’s an API?
software interface between your program + something else - set of operations defined for interacting w/ a system in a controlled way
to use need to #include <headerfile.h></headerfile.h>
What is a file?
ideally persistent storage of a collection of data, either text or binary + can be accessed serially or by random access
EOF means??
end of file
How to read/write/open to a text file?
getchar() to return next input char + putchar(int) to put char on standard output
a
FILE fopen(char name, char mode) - modes being r/a/w
How do you find where you are in the file in C?
long ftell(FILE *stream) - find out how many bytes from the start we are
Pros of single file projects?
- only 1 file to parse for compiler
- programmer only needs to scroll through one place
- copying/distributing project is just 1 source file + no dependencies - easyyy
Cons of single-file projects?
- not scalable
- scrolling becomes unmanageable
- difficult for teams to co-edit 1 file
- harder to package up functions for reuse - danger of hidden side effects
What is a #include statement + the difference between “” and <> in them?
include basically pastes the code from the file it references in w/ your code
”” for in same file and <> for in system files
What is forward declaration/referencing?
defining the function type name and types of variables to help compiler and IDE when parsing functions from header files
also needed if function used before declared in the file
What does gcc -c do?
create objects/part compiled files (saves from file.c to file.o)
How do you turn an object into a library?
gcc -c file.c - make file.c an object
ar rcs libx.a file.o - make file.o a library
where libraries should always be called lib”name”.a
Why do we need dynamic data structures? (cons of static structs.)
costly (time + money) to shift data around in static structures - inefficient
need to know size to declare
may run out of space or take up unnecessary space
How to add to dynamic data structure?
allocate space for a node from the heap (use malloc())
find where to add the item to the structure
adjust pointers accordingly
What is version control?
principled approach to tracking your code base
records differences between versions w/ no.s + timestamps too when you commit
What is the pre-processor useful for?
defining constants (#define)
defining macros (#define)
including files (#include)
conditional compilation (#if/#ifndef)
How does the macro #define MIN(X,Y) (X) < (Y) ? (X) : (Y) work?
means where you see MIN(int,int) you substitute it with the comparison which will return whichever integer is smaller
how is conditional compilation helpful?
used to compile for different types of processor
can use #ifndef to protect header files from multiple inclusion errors
How do you do a binary shift in C?
«_space;= left shift,»_space; = right shift