Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles.
// Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles. #include <assert.h> #include <stdio.h> #include <stdlib.h> static unsigned long long *fibonacci(const size_t length) { assert(length > 1); if (unsigned long long *const fib = static_cast<unsigned long long *>(calloc(length, sizeof *fib))) { fib[0] = 0; fib[1] = 1; for (size_t i = 2; i != length; ++i) fib[i] = fib[i - 2] + fib[i - 1]; return fib; } return NULL; } int main(int argc, char **argv) { (void)argc; static const size_t length = 94; if (unsigned long long *const fib = fibonacci(length)) { printf("[ "); for (size_t i = 0; i != length; ++i) printf("%llu ", fib[i]); printf("]\n"); free(fib); exit(EXIT_SUCCESS); } fprintf(stderr, "%s: error: out of memory\n", argv[0]); return EXIT_FAILURE; }
run
|
edit
|
history
|
help
0
DESim Example
Balanced Insert Heap Example
khcmknhc
Access to temporary object
Sum of Natural Numbers using loop
"nearest enclosing namespace"
non-template template parameters for container stream insertion: SFINAE
constructor-is-not-called-in-this-aggregation-class
Dynamic call
Recursive Sort Example