5 ms·
Very cool. But let me warn you: Your method of code organization won't scale to anything whose #include graph doesn't form a tree. You should learn how to pu
by SamReidHughes 11y ago
Very cool. But let me warn you: Your method of code organization won't scale to anything whose #include graph doesn't form a tree. You should learn how to put your declarations into include-guarded header files and your implementations into C files.
Also, abs has signature int abs(int x). I think you want to use fabs.
Edit: Also, it is quite peculiar to have the program print out null characters. The output for ./calc '2' is four characters: {'2', '\0', '\0', '\n'}. In places where you're using %c and printing '\0', you could use %s and print "" instead.
- maq123 11y agoRegarding code organization ... Any learning sources where some approaches are being explained ? I am sysadmin and sometimes I find it hard to understand how source files I just downloaded connected to each other. Thx
- SamReidHughes 11y agoAny book on C. The early chapters of an online C tutorial.
- dagw 11y agoI just checked the first 5 C tutorials the showed up in Google and non of them talked about code organization in anything but the most superficial sense.
- kevinnk 11y agoTry http://www.tutorialspoint.com/cprogramming/c_header_files.htm http://www.tutorialspoint.com/cprogramming/c_header_files.ht...
- enesunal 11y agosearch for 'C best practices'. I've even seen a guideline from Linus Torvalds a few months ago.
- vivekkannan 11y agofixed both. thanks for pointing it out. I have just been told about wacky header files. will changing calc.h to calc.c make things less wackier?
- jdiez17 11y agoThe header file should basically only contain: * The dependencies (includes) of the implentation * A list of function signatures, data structures it provides, etc * Any constant definition specific to the module Another "feature" of separating the header file from the implementation is that it guards against recursive includes, as the commenter above suggested. So your calc.h and calc.c should look something like this: https://gist.github.com/jdiez17/2f093b2c8cc2913bb890 https://gist.github.com/jdiez17/2f093b2c8cc2913bb890
- SamReidHughes 11y agoThe header file should not have the includes that the implementation uses. It should have the minimal necessary to achieve its purposes as a header file.
- jdiez17 11y agoWhy? Most of those headers are going to be guarded against multiple inclusions anyway, and it's a nice way to list the dependencies of the module.
- mbel 11y agoIt is leaking includes without any reason, there is no need for the user of given module to know what dependencies the implementation is using. Place to list dependencies is at the beginning of the source file of given module, and as far as I know this is the convention. Main problem with dependencies in the headers is that if any header of your library include headers from the different libraries the user will have have to (1) have those headers, (2) probably configure their build to use them, even if they bring zero useful information for them.