-
2024/08/27
-
i’m bored, so instead of using social media, will try and read a page or two and write down some notes.
-
why C
-
i want to eventually build an emulator with C and sokol
-
prepare myself to eventually write something playing for playb.it
-
learn about manual memory management, os api, low level stuff etc
-
read cool people code
-
-
Hello, World!
-
there’s 2 stages to compile a C program: preprocess and compile
-
preprocess took everything inside
#define,#includeand put in your source code -
then compile came in and spit out machine code for CPU to understand
-
-
main()then exit -
compile with
-
gcc -o hello hello.c gcc -o multi a.c b.c c.c -
mac usually alias
gccwithclangalready (xcode dev thingy for git, etc)
-
-
-
C Versions
-
big ones: C89, C99, C11, C2x
-
C89, ANSI C, C90: ANSI C standard
-
C99: add
//comment, most popular (most likely most portable) -
C11: support unicode and multithread, modern C
-
C2x: most updated
-
-
what to choose: just compile with
gccorclang-
the book use C2x tho
-
-
-
notes:beej’s guide to C
-
2024/08/28
-
Variables and Statements
-
variable is human readable name for data in a memory
-
memory is a big array of bytes
-
if something bigger than a bytes, then store with multiple bytes
-
a bytes = 8bits = 8 zeroes or ones
-
since memory is an array, then each bytes of memory can be refer with index. this index is called address, location or pointer
-
pointer is just an variable that hold address to a variable that lie on memory
-
-
variable types
-
string is
char* -
there’s actually no bool, only 0 and not 0
-
-
uninitialized variable have indeterminate value
-
must be initialized with value or else it must be nonsense number
-
-
pre(++i), post(i++) increment decrement
-
5 + i++:5 + itheni + 1 -
5 + ++i:i + 1then5 + i
-
-
comma
,operator-
evaluate left to right, return right most
-
x = (1,2,3)=>x = 3
-
-
sizeofoperator-
tell the size (in bytes) of variable or a type
-
sizeof a -
sizeof(int)
-
-
depend on the system, except for
charwhich always 1 bytes -
sizeofreturn asize_ttype-
think
size_tas value represents a count
-
-
is a compile time operation
-
-
-
-
2024/09/12
-
Functions
-
anatomy
return_type fn_name(args) {body}-
fn_name(void)indicate function accept no arguments
-
-
all arguments is a copy, not a reference unless specify
-
But no fancy-schmancy name will distract you from the fact that EVERYTHING you pass to a function WITHOUT EXCEPTION is copied into its corresponding parameter, and the function operates on that local copy, NO MATTER WHAT. Remember that, even when we’re talking about this so-called passing by reference.
-
-
we can call function before defining it by using
function prototype-
omit the body and put it somewhere before calling the function
-
return _type fn(args)is a prototype
-
-
should always use
fn(void)to indicate function take no argument-
unless you really know what you doing, which i’m not.
-
-
-