How do I use extern to share variables between source files?

Using extern is only of relevance when the program you’re building consists of multiple source files linked together, where some of the variables defined, for example, in source file file1.c need to be referenced in other source files, such as file2.c. It is important to understand the difference between defining a variable and declaring a variable: A variable is declared when the compiler is informed that a … Read more

Printing long int value in C

You must use %ld to print a long int, and %lld to print a long long int. Note that only long long int is guaranteed to be large enough to store the result of that calculation (or, indeed, the input values you’re using). You will also need to ensure that you use your compiler in a C99-compatible mode (for example, using the -std=gnu99 option to … Read more

Categories C Tags

How do I calculate MB/s & MiB/s?

If I transfer 1381530 bytes in 17797601 nanoseconds, what is the data rate in those two measures? 0.0776 bytes/ns. First, careful: I’ve recently discovered that MB/s is technically equivalent to 8000 million bits/s I’ve never heard of this definition. “MB/s” usually means “megabytes per second”. This can be one of two definitions, depending on who you ask: 1 … Read more

Categories C Tags

How do you do exponentiation in C?

use the pow function (it takes floats/doubles though). man pow: EDIT: For the special case of positive integer powers of 2, you can use bit shifting: (1 << x) will equal 2 to the power x. There are some potential gotchas with this, but generally, it would be correct.

Categories C Tags

what is the meaning of == sign?

The == operator tests for equality. For example: And, in your example: x is true (1) if y is equal to z. If y is not equal to z, x is false (0). A common mistake made by novice C programmers (and a typo made by some very experienced ones as well) is: In this case, b is assigned … Read more

how to use wait in C

The wait system-call puts the process to sleep and waits for a child-process to end. It then fills in the argument with the exit code of the child-process (if the argument is not NULL). So if in the parent process you have And in the child process you do e.g. exit(1), then the above code will print Child process … Read more

How do we check if a pointer is NULL pointer?

I always think simply if(p != NULL){..} will do the job. But after reading this Stack Overflow question, it seems not. So what’s the canonical way to check for NULL pointers after absorbing all discussion in that question which says NULL pointers can have non-zero value?