'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup Reference : http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf ed2k://|file|n1256.pdf|3788603|a728a3c5e98e681a8c389ffc46640625| ===== DEVEL / C ===== * [[Book]] http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ * isbn:2100487345 [[French]] [[Livre]] * http://www.strath.ac.uk/IT/Docs/Ccourse/ccourse.html # C course * http://directory.google.com/Top/Computers/Programming/Languages/C/ * http://www.faqs.org/faqs/C-faq/ * https://en.wikipedia.org/wiki/MISRA_C# ConventioN ==== [[Langage]] Keywords : ==== * if else * for while do * return * struct typedef enum * [[sizeof]] * main ==== TYPES ==== * special type "void" which is not really a type * lang defined basic types : char, short, int, void, long, double On X86 architechure (running [[Linux]] or [[Windows]] ) : # sizeof(char)=1 # sizeof(void)=1 @ gcc@[[Linux]] & 0 @ [[MSVC]]@[[Windows]] # sizeof(short)=2 # sizeof(int)=4 # sizeof(void*)=4 # sizeof(char*)=4 # sizeof(long int)=4 # sizeof(long)=4 # sizeof(double)=8 # sizeof(long long)=8 // long long is not ansi and not supported by msvc * arrays and pointers , char ptr[] ~=~ char* ptr * structures : struct buffer { unsigned int size_; char* pbytes_; } * enum enum colors { RED=1, YELLOW, GREEN=6, BLUE }; Now RED=1, YELLOW=2, GREEN=6 and BLUE=7. The main advantage of enum is that if you don't initialize your constants, each one would have a unique value. The first would be zero and the rest would then count upwards. * [[typedef]] T limitmax = ((tmp=~0)==(tmp>>1)?~(tmp<<((sizeof(tmp)<<3)-1)):tmp); ==== ARRAYS ==== [[C]] & [[C++]] only support: int w[[]] = {1,2,3}; [[Java]] supports both : int[[]] v = {1,2,3}; int w[[]] = {1,2,3}; //static allocation (on compile time) static char img[[h]][[w]][[IMAGE_DEPTH]]; img[[h]][[w]][[k]] = 'a'; unsigned char* ptr = &img[[0]][[0]][[0]]; // dynamic allocation (on run pimg = new char[[h*w*IMAGE_DEPTH]]; time) pimg[[(ih*w*IMAGE_DEPTH)+(iw*IMAGE_DEPTH)+k]] = 'a' ; // both are stored the same in RAM memcmp( ( char*) pimg, ( char*) ptr, h*w*IMAGE_DEPTH) ); // =0 //same See how array can be usefull int f(int c[[]][[2]] ) { c[[0]][[0]] = 1; } int main(int argc, char* argv[[]]) { int a[[1]][[2]]; a[[0]][[0]] = 0; f(a); debugi( a[[0]][[0]] ); //1 } ==== OPERATORS ==== [[Bit]] MASK : int reverseColor_ARGB_to_BGRA(int c) { int b= c &0xFF; int g = (c>>8) &0xFF; int r = (c>>16) &0xFF; int a = (c>>24) &0xFF; // return ( a | (r<<8) | (g<<16) | (b<<24) ); // may be unsafe return ( a | ( (r<<8) &0xFF00 ) | ( (g<<16) &0xFF0000 ) | ( (b<<24) &0xFF000000 ) ); } * bitfield : http://tastalian.free.fr/index.php?index=10 * http://linuxfr.org/2006/09/07/21299.html # [[Bit]] ==== POINTERS ==== can you tell why in std libs they use : "const char *" while "char const * const" ...could be used RzR: because the other const just makes the pointer itself const, and since it's passed by value, that wouldn't make a difference to the caller an implementation could add or remove the extra const without making any difference to the caller your answser is smart http://www.goingware.com/tips/member-pointers.html ==== API ==== * http://altdevblogaday.org/2011/02/12/alternatives-to-malloc-and-new/# [[libc]] * http://www.greenend.org.uk/rjk/2001/06/poll.html * http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html# [[jmp]] @tag: [[libc]] [[api]] [[socket]] ==== [[OOP]] IN C ==== return-type function-name(paramater-type-list); int (*aaron(char *(*)(void)) (long, int); ''Defines the function aaron which returns a pointer to a function. The function aaron takes one argument: a pointer to a function which returns a character pointer and takes no arguments. The returned function returns a type int and has two parameters of type long and int.'' * http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html * http://www.function-pointer.org/fpt.html#defi * http://xinehq.de/index.php/hackersguide#AEN384 typedef struct my_stack_s my_class_t; struct my_stack_s { /* method "push" with one parameter and no return value */ void (*push)(my_stack_t *this, int i); /* method "add" with no parameters and no return value */ void (*add)(my_stack_t *this); /* method "pop" with no parameters (except "this") and a return value */ int (*pop) (my_stack_t *this); }; /* constructor */ my_class_t *new_my_stack(void); ==== [[printf]] ==== cat > main.c << EOF && make main && ./main p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);} EOF ==== operators ==== i++ vs ++i ? ++i does not create a temp object and addition, so it is better and faster ===== MISC ===== [[CPU]] : simd : http://ds9a.nl/gcc-simd/ http://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/X86-Built-in-Functions.html * [[optimize]] : http://www.eventhelix.com/realtimemantra/basics/optimizingcandcppcode.htm#Don%27t%20Define%20a%20Return%20Value%20if%20not%20Used http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_0zn6.asp have fun of bmiColors[[1]]; le mieux etant de lire le header xmmintrin.h * http://www.etalabs.net/compare_libcs.html * http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/test/selftest.html * http://www.cquestions.com/2012/02/tricky-c-questions-and-answers.html ===== MorE ===== @TaG: LangC [[CPlusPlus]] [[C++]] [[GCC]] [[MSVC]] [[Programming]] [[Maths]] [[Preprocessor]] [[static]] [[typedef]] [[Preprocessor]] [[Unix]] [[lang]] [[std]] [[ptr]] [[pointer]] [[oop]] UnioN {{http://24.media.tumblr.com/tumblr_m9rno6xn0b1rexgvto1_400.gif}}