C_quicknotes

Takeaways from the book “Advanced C Programming” by Berry, John Thomas.

While it’s titled “advanced”, it is rather basic. Here is some quick notes on stuff that I’m not familar with/ don’t use often.

define variables as global to a small region of program

Global variables always involves the possibility of side effects. By keeping the coupling well controlled, we minimize some of this danger.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// stack and top are global to push() and pop(), but hidden from main().
void push();
void pop();
int main(){
// calling push() and pop()
}

int top;
void* stack[100];

void push(){
// .. pushing to the stack
}

void* pop(){
// .. popping from the stack
}

Using stack to hide global variables from the rest of the program

main.c

1
2
3
4
5
6
7
8
9
// stack and top can still be accessed here
// by declaring them with extern.
// or worse, main.c can declare them as global again
// causing problems...

extern void* stack[];
extern int top;
int main(){
}

lib.c

1
2
3
4
int top;
void* stack[100];
void push(){...};
void pop(){...};

to avoid this, declare the global variables as static so that their scopes are restricted only to the home file.

1
2
static double stack[100];
static int top=0;

Declaring functions in main to limit the scope: only use there!

1
2
3
4
5
6
7
8
9
int main(
int foo();
foo();

)

int foo(){
// ...
}

Bitfield

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
typedef struct _bf{
    int f1:1,
        f2:1,
        f3:1;
}flags;

int main(){
    flags bf;
    bf.f1 = 0;
    bf.f2 = 1;
    bf.f3 = 1;

    // check flag
    if(bf.f1) // do ..
    
}
Aug 8, 2022


[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<




Fun with Image Maps and SVGs via benji February 20, 2024

Over the past few weeks I've been playing around with making some images on my website interactive. My "informatics" class in high school taught us some basic HTML. By basic I mean in notepad and writing everything by hand, saving to a...

Goblin Week 2024 via Helvetica Blanc January 26, 2024

It snuck up on me, but I managed to draw my little goblins to celebrate the week! I love my children - they're like awful Pikmin. I've uploaded the whole parade as a print, as well as individual prints for each goblin. There's something very fu…

How to trust gpg keys via Travis Shears Personal Site October 27, 2023

After moving some GPG keys to a new computer I kept getting these trust warnings. It is NOT certain that the key belongs to the person named in the user ID.If you * really * know what you are doing, you may answer the next question with yes. Use this key a…

Generated by openring from webring