Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C tips

Name: Anonymous 2009-03-23 19:55


    1. Use fprintf ("fast printf") instead of printf.
    2. ++i is faster than both i++ and i = i + 1.
    3. void main(void) is faster than int main(void) or int main(int, char **) since no value needs to be returned to the OS.
    4. Swapping with exclusive-or (a^=b^=a^=b swaps a and b) is faster than using a temporary. This works for all types (including structures), but not on all compilers. Some compilers may also give you a harmless warning.
    5. Static storage duration objects are faster than automatic storage duration objects because the CPU doesn't have to set aside storage on the stack every time a function is called. Make your loop indexes global so that you can use them everywhere:
[code]    int i;
    void func(void) { for (i = 0; i < 10; i++) ; /* ... */ }
    void func2(void) { for (i = 0; i < 20; i++) ; /* ... */ }
    /* ... */[code]
    6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it):
[code]    int arr[256];
    size_t realsize;
    for (realsize = 0; realsize <= SIZE_MAX; ++realsize)
            if (!memset(arr, 0, realsize)) break;
    /* now you know that arr actually has realsize / sizeof (int) elements */[code]
    If you combine this with #5, your program will be faster in the long run (but this usually doesn't work for short programs).

Name: Anonymous 2009-03-23 20:01

This thread has been closed and replaced with the following thread:

Subject: Compiling C code with a C++ compiler
Name:
Email:

It works.

Name: Anonymous 2009-03-23 20:18

>>1
ANONIX QUALITY

Name: Anonymous 2009-03-23 20:20

Name: Anonymous 2009-03-23 20:32

>>4
At least I'm not begin trolled by someone who I don't think is as clever as he thinks he is.

Name: Anonymous 2009-03-23 22:19

This thread has been closed and replaced with the following thread:

Subject: Compiling C++ code with a C++ compiler
Name:
Email:

It doesn't work.

Name: Anonymous 2009-03-23 22:31

>>2
only in the most trivial cases.

Name: Anonymous 2009-03-23 23:15

This thread has been closed and replaced with the following thread:

Subject:
Compiling C code with a Objective-C compiler
Name:
Email:


It works.

Name: Anonymous 2011-01-31 20:49

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List