A repo for programming exercise.
- 2024/03/09
- Start
c
programming exercise based on Learn C The Hard Way (Chiniese Version) . - c/ex1: Compile an example for printing "hello world".
- c/ex2: Simple example of
make
and Makefile - c/ex3: Print with format, and try to make it error/crash, through methods like: not initializing varialbes, not specifying variables in the print command.
- Start
- 2024/03/10
- c/ex4: Install
valgrind
and use it for printing errors of exeutable.- Use
curl -O <link>
download the compressed.tar.bz2
file. Failed to uncompress withtar xvzf <file>
at first with messagebzip2: (stdin) is not a bzip2 file...
. Root cause is found to be file is broken during downloading, which could be checked bymd5sum
comparison with that in the download page. Re-downloaded and then fixed. - How to change shell of
make
: By default,make
use the POSIX shell (/bin/sh
), need to addSHELL := /bin/bash
on the top of Makefile to let it use bash. (Ref.) - How to pass args to
make
: (Ref.)- Makefile:
run:
run: @echo $(ARGS)
make run ARGS="Hello"
, output:Hello
- Makefile:
- Use
- c/ex5: Read a simple c script line by line to know the basic code structure.
- c/ex6: A script containning multiple variables of different data types. Print them with
printf
. - c/ex7: A simple code for knowing different types of data.
- 'long' type integer:
- printf with %ld
- limit: -2^63 ~ +2^63 - 1
- printf special char
'\0'
(null_byte) with:- %c -> (empty)
- %s -> (null)
- %d -> 0
- printf char of number zero
'0'
with %s -> Segmentation fault - 'unsigned long' type integer:
- printf with %lu
- limit: 0 ~ +2^64 - 1
- 'long' type integer:
- c/ex4: Install
- 2024/03/23
- c/ex8: Play with array of numbers, array of chars and string by
printf
/sizeof
- string is array of chars ending with
null
character, i.e.'\0'
. sizeof()
returns the allocated memory for the operand, so, for a string, it will take the endingnull
into count, then usually looks 1 more than the number of characters of the string. (Ref.)
- string is array of chars ending with
- c/ex9: Involve something like array initialization, assigning string with pointer ... (Not really know what the author wants readers to know....just go through ๐ :)
- c/ex10: Array of strings and loop
argv[0]
is the command/executable itself.- Format of a
for
loop:Executing order: INITIALIZER, TEST, CODE, INCREMENTER, TEST, CODE, INCREMENTER, ... until TEST being false.for(INITIALIZER; TEST; INCREMENTER) { CODE; }
- c/ex8: Play with array of numbers, array of chars and string by
- 2024/03/24
- c/ex11: While loop
- Format of a
while
loop:while (TEST) { CODE; }
- Usually
for
loop is more robust, since it has INITIALIZER and INCREMENTER that help avoiding errors. - Way to print the memory address of a variable:
output example:
int var = 0; printf("Mem addr of var: %p \n" &var);
Mem addr of var: 0x7ffe2333ce7c
- 1 byte = 8 bit
- int -> 4 byte : 2^32 counts , -2^31 ~ +2^31 -1
- char *[] -> 8 byte : 63 chars + 1 null
- Format of a
- c/ex12: if - else conditional statement.
- Better do not omit the curly braces
{}
for conditional statements.
- Better do not omit the curly braces
- c/ex13: switch statement
- Compiler will convert it into a jumping table.
- Don't forget
break
anddefault
- c/ex11: While loop