Which is faster: while(1) or while(2)?

Both loops are infinite, but we can see which one takes more instructions/resources per iteration. Using gcc, I compiled the two following programs to assembly at varying levels of optimization: int main(void) { while(1) {} return 0; } int main(void) { while(2) {} return 0; } Even with no optimizations (-O0), the generated assembly was … Read more

What is the printf format specifier for bool?

There is no format specifier for bool types. However, since any integral type shorter than int is promoted to int when passed down to printf()‘s variadic arguments, you can use %d: bool x = true; printf(“%d\n”, x); // prints 1 But why not: printf(x ? “true” : “false”); or, better: printf(“%s”, x ? “true” : … Read more

What is a “static” function in C?

static functions are functions that are only visible to other functions in the same file (more precisely the same translation unit). EDIT: For those who thought, that the author of the questions meant a ‘class method’: As the question is tagged C he means a plain old C function. For (C++/Java/…) class methods, static means … Read more

Why does printf not flush after the call unless a newline is in the format string?

The stdout stream is line buffered by default, so will only display what’s in the buffer after it reaches a newline (or when it’s told to). You have a few options to print immediately: Print to stderrinstead using fprintf (stderr is unbuffered by default): fprintf(stderr, “I will be printed immediately”); Flush stdout whenever you need … Read more

How to generate a random int in C?

Note: Don’t use rand() for security. If you need a cryptographically secure number, see this answer instead. #include <time.h> #include <stdlib.h> srand(time(NULL)); // Initialization, should only be called once. int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX. On Linux, you might prefer to use random and srandom.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)