What is the argument for printf that formats a long?
Put an l (lowercased letter L) directly before the specifier. unsigned long n; long m; printf(“%lu %ld”, n, m);
Put an l (lowercased letter L) directly before the specifier. unsigned long n; long m; printf(“%lu %ld”, n, m);
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
Just about every modern operating system will recover all the allocated memory space after a program exits. The only exception I can think of might be something like Palm OS where the program’s static storage and runtime memory are pretty much the same thing, so not freeing might cause the program to take up more … Read more
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
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
It depends on what you need the value for. You (and everyone else so far) omitted the third alternative: static const int var = 5; #define var 5 enum { var = 5 }; Ignoring issues about the choice of name, then: If you need to pass a pointer around, you must use (1). Since … Read more
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
TL;DR while(!feof) is wrong because it tests for something that is irrelevant and fails to test for something that you need to know. The result is that you are erroneously executing code that assumes that it is accessing data that was read successfully, when in fact this never happened. I’d like to provide an abstract, … Read more
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.
It’s an abbreviation of Error NO ENTry (or Error NO ENTity), and can actually be used for more than files/directories. It’s abbreviated because C compilers at the dawn of time didn’t support more than 8 characters in symbols.