
C Wizzard
@c_preprocessor
Snippets of interesting C code
Array size macro: #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) // Used like this: int a[] = {0, 5, 6}; int n = ARRAY_SIZE(a); // n = 3 // Warning: does not work with array arguments to // functions: int func(int a[]) { int nb = ARRAY_SIZE(a); // Doesn't work! }
malloc(0) can return either a null pointer or a pointer to 0 bytes. The ANSI/ISO Standard says that it may do either and that the behaviour is implementation-defined.
Difference between ++i and i++: ++i adds one to the stored value of i and ``returns'' the new, incremented value to the surrounding expression; i++ adds one to i but returns the prior, unincremented value.
Best Books for Learning C Programming #C #Programming #ProgrammingBooks toptalkedbooks.com/articles/PZFCV…
Count the number of bits which are set in an integer: static int bitcounts[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; int bitcount(unsigned int u) { int n = 0; for(; u != 0; u >>= 4) n += bitcounts[u & 0x0f]; return n; }
difference between memcpy and memmove: memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable.
The postfix ++ and -- operators have higher precedence than the prefix unary operators. Therefore, *p++ is equivalent to *(p++); it increments p, and returns the value which p pointed to before p was incremented. To increment the value pointed to by p, use (*p)++
Array subscripting is commutative in C. a[e] is identical to *((a)+(e)), for any two expressions a and e, as long as one of them is a pointer expression and one is integral. For example: char *tmpptr = "abcdef"; now tmpptr[5] is 'f' and 5[tmpptr] is also 'f'.
United States Xu hướng
- 1. Aaron Gordon 10.2K posts
- 2. Wentz 23.8K posts
- 3. Vikings 50.3K posts
- 4. Chargers 55.1K posts
- 5. Pacers 21.7K posts
- 6. #criticalrolespoilers 7,369 posts
- 7. Brosmer 3,749 posts
- 8. Shai 21.5K posts
- 9. Jokic 14.4K posts
- 10. #LOVERGIRL 12.3K posts
- 11. Herbert 16.1K posts
- 12. #EAT_IT_UP_SPAGHETTI 145K posts
- 13. $UNI 3,172 posts
- 14. #Skol 5,205 posts
- 15. hobi 29.3K posts
- 16. Quentin Johnston 2,156 posts
- 17. Will Richard 4,068 posts
- 18. Al Michaels 1,309 posts
- 19. #SPAGHETTI_FT_JHOPE 30.1K posts
- 20. Cam Johnson 1,070 posts
Something went wrong.
Something went wrong.