Warning/error “function declaration isn’t a prototype”

In C int foo() and int foo(void) are different functions. int foo() accepts an arbitrary number of arguments, while int foo(void) accepts 0 arguments. In C++ they mean the same thing. I suggest that you use void consistently when you mean no arguments. If you have a variable a, extern int a; is a way to tell the compiler that a is a symbol that might be present in a … Read more

Categories c Tags ,

Bind failed: Address already in use

The error usually means that the port you are trying to open is being already used by another application. Try using netstat to see which ports are open and then use an available port. Also check if you are binding to the right ip address (I am assuming it would be localhost)

Convert char array to a int number in C

I personally don’t like atoi function. I would suggest sscanf: It’s very standard, it’s in the stdio.h library šŸ™‚ And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end. EDIT I just found this wonderful question here on the site that explains and compares 3 different ways … Read more

C Vector/ArrayList/LinkedList

For resizable arrays you can use malloc() and realloc(). These allow you to reserve (with malloc()) and resize (with realloc()) a certain amount of space on the heap. They’re used this way: Just replace ‘int’ with your struct type. šŸ˜‰

Categories c Tags

Setting std=c99 flag in GCC

Instead of callingĀ /usr/bin/gcc, useĀ /usr/bin/c99. This is the Single-Unix-approved way of invoking a C99 compiler. On an Ubuntu system, this points to a script which invokesĀ gccĀ after having added theĀ -std=c99Ā flag, which is precisely what you want.

Writing binary number system in C code

Standard C doesn’t define binary constants. There’s a GNU C extension though (among popular compilers, clang adapts it as well): the 0b or 0B prefixes: If you want to stick with standard C, then there’s an option: you can combine a macro and a function to create an almost readable “binary constant” feature: And then … Read more

Categories C Tags

expected expression before ‘{‘ token

The error is because you can’t assign an array that way, that only works to initialize it. So applying this to your specific example: or you could use memset the same way, but instead of sizeof(int) you need size of your structure. That doesn’t always work… but given your structure, it will.

difference between and

The <malloc.h> header is deprecated (and quite Linux specific, on which it defines non-standard functions like mallinfo(3)). Use <stdlib.h> instead if you simply need malloc(3) and related standard functions (e.g. free, calloc, realloc ….). Notice that <stdlib.h> is defined by C89 (and later) standards, but not <malloc.h> Look into /usr/include/malloc.h you’ll find there some non-standard functions (e.g. malloc_stats(3), etc…) – in addition of malloc…. And gcc don’t link header files, but libraries. Read Levine’s book about linkers & loaders for more. … Read more

execv vs execvp, why just one of them require the exact file’s path?

If the program name argument contains no slashes, the execvp() function looks for the program to execute in the directories listed on your PATH environment variable. If you don’t have . (the current directory) on your PATH and you aren’t in one of the directories listed on your path, a plain name like b will not be executed, even if b is in … Read more