The difference between stdout and STDOUT_FILENO

stdout is a FILE* “constant” giving the standard outout stream. So obviously fprintf(stdout, “x=%d\n”, x); has the same behavior as printf(“x=%d\n”, x);; you use stdout for <stdio.h> functions like fprintf, fputs etc.. STDOUT_FILENO is an integer file descriptor (actually, the integer 1). You might use it for write syscall. The relation between the two is STDOUT_FILENO == fileno(stdout) (Except after you do weird things like fclose(stdout);, or perhaps some freopen after some fclose(stdin), which you should … Read more

Error: control may reach end of non-void function in C

You are getting this error because if your for loop breaks due to breaking condition i < n; then it don’t find any return statement after for loop (see the below, I mentioned in code as comment). If for loop break due to i >= n then control comes to the position where I commented and there is no return statement present. Hence you are getting an error “reach … Read more

Difference between char* and const char*?

char* is a mutable pointer to a mutable character/string. const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated. char* const is an immutable pointer (it cannot point to any other location) but the contents … Read more

Xcode – Warning: Implicit declaration of function is invalid in C99

The function has to be declared before it’s getting called. This could be done in various ways: Write down the prototype in a headerUse this if the function shall be callable from several source files. Just write your prototypeint Fibonacci(int number);down in a .h file (e.g. myfunctions.h) and then #include “myfunctions.h” in the C code. Move the function before it’s … Read more

Difference between a Structure and a Union

With a union, you’re only supposed to use one of the elements, because they’re all stored at the same spot. This makes it useful when you want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they … Read more

What does WEXITSTATUS(status) return?

WEXITSTATUS(stat_val) is a macro (so in fact it does not “return” something, but “evaluates” to something). For how it works you might like to look it up in the headers (which should be #included via <sys/wait.h>) that come with the C-compiler you use. The implementation of this macro might differ from one C-implementation to the other. Please note, … Read more

What is a bus error? Is it different from a segmentation fault?

Bus errors are rare nowadays on x86 and occur when your processor cannot even attempt the memory access requested, typically: using a processor instruction with an address that does not satisfy its alignment requirements. Segmentation faults occur when accessing memory which does not belong to your process. They are very common and are typically the … Read more