mcinglis / c-style Goto Github PK
View Code? Open in Web Editor NEWMy favorite C programming practices.
License: Other
My favorite C programming practices.
License: Other
Reason: it's too difficult to use correctly (among the many, many mistakes you will make, think of the meaning of the N parameter: it's how many characters you can append).
Edit: see http://the-flat-trantor-society.blogspot.it/2012/03/no-strncpy-is-not-safer-strcpy.html for a more detailed explanation of what it was designed to do and how it's different from typical usage.
Better alternatives are OpenBSD's strlcat, or better(!!!) something like Microsoft' strcat_s/strncat_s -- it's better in that it returns wether an overflow occurred, so you can't make typos comparing to the wrong size; the downsize is that by default it aborts on overflow...
An even simpler alternative imho is http://yarchive.net/comp/linux/strncpy.html
int const xs[] = { 1, 2, 3 }; return sum( xs, sizeof xs );
Should the last line be return sum( xs, sizeof xs / sizeof xs[0] );
?
your example with run_server( "3490" );
doesn't work if the struct has default arguments
#include <stdio.h>
struct run_server_options {
char * port;
int backlog;
};
#define run_server( ... ) \
run_server_( ( struct run_server_options ){ \
/* default values */ \
.port = "45680", \
.backlog = 5, \
__VA_ARGS__ \
} )
int run_server_( struct run_server_options opts )
{
printf("port: %s backlog: %d\n", opts.port, opts.backlog);
}
int main( void )
{
return run_server( "3490" );
}
this code adds too many elements to the struct
"3490"
is totally ignored and the output is port: 45680 backlog: 5
I think the 80 character line limit clashes with the previously mentioned push toward using the newest tooling that you can. (C89 v C99 v C11) Additionally, most systems are using widescreen monitors, even the raspberry pi is in wide screen aspect. Even using an outdated IDE like KEIL, which when crowded with frame and panels, still leaves ample room to edit code.
I think, if you're going to hold onto the 80 character line limit, then you should use Linux's 8-space tabbing, as Torvalds recommends, since that enforces code simplicity.
The -pedantic
option will prevent many such weak casts.
The example link here is broken, could you provide the source from alphabet.h
in your trie.c
repository?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.