Main Menu

Deep C

Started by panreyes, March 06, 2013, 02:33:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


I've just learnt something :|

It made me understand of all those bugs in my C practices.


He, that's a pretty interessting book. I knww C is a dangerous language, this book explains things clearly in a funny way.  No I understand why so many C programs are cryptic.... ;D


I can explain you lot of this behavoir, but not in english... if you want  I can do it in spanish...

for example, is stupid said, that C reuse a var between several functions, only is the space for non-static vars...

this give you 42, too.

#include <stdio.h>
void foo(void)
        int a;
        printf("%d\n", a);
void bar(void)
        int b = 42;
int main(void)

the first example, is curious, but is logic... depend on stack state machine based implementation or direct interpreter...

well, who did this document?
Download Lastest BennuGD Release:


I don't understand why the document said that examples, like this fail... in my linux gcc they works right

#include <stdio.h>
int main(void) {
int v[6] = {4,6,2,9};
int i = 2;
int j = i * 3 + v[i++];
printf("%d\n", j);

this give 8... is ok

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main()
        // implementation-defined
         int i = ~0;
         i >>= 1;
         printf("%d\n", i);
         // unspecified
         printf("4") + printf("2");
         // undefined
         int k = INT_MAX;
         k += 1;
         printf("%d\n", k);

this give us...


values are ok.

-1 is because you use int (then rotation is signed rotation)... if you define "i" as unsigned int, the values are differents.
Download Lastest BennuGD Release:


If you read it thoroughly it says it may work with some compilers, but you should be aware of that possible problem for troubleshooting.

int j = i * 3 + v[i++];[/size]

Because of some compiler optimizations, it's not recommended to read and change the same variable in the same expression or sentence.


optimization bug of specific compiler... in gcc works fine...

and is recommended initialize vars on declaration or before his use.
Download Lastest BennuGD Release: