[C] Hidden features of C - Programming On Unix
Users browsing this thread: 5 Guest(s)
|
|||
Don't really like calling these things "hidden" features, because they aren't hidden at all. Just most people don't read the standard or what language extensions and builtins their compiler offers.
Anyway here's a few things that I don't see used much, that weren't mentioned in that link. Some of these are new features added to C in C11, and could be achieved using compiler extensions however they're particularly useful having them as apart of the standard. The first is alignment (added in C11). In this example, it uses alignment to make sure that the types are stored at addresses that have (at least) their lowest bit unused. So it can then use that space to store a flag. Code: #include <stdio.h> The next is generic selection (added in C11). The example uses it as a way of getting the format specifier for the current type. Code: #include <stdio.h> Now that isn't a particularly useful example of generics. One thing I found it really useful for was when I made a modifiable constant value system for my game engine (so constant values could be modified at runtime in an editor, really useful when you know you want a constant value but unsure what value specifically). It would then let me simply do RUNTIME_CONSTANT(value). e.g. int a = RUNTIME_CONSTANT(34);. The next thing is format specifiers for fscanf (or variants of); the same can also be said for the format specifiers of fprintf (or variants of) but it has differing specifiers and can't be bothered with an example. There's a few things to cover here. One thing you'll often hear when regarding fscanf is that it is not secure when dealing with strings (and as a result many avoid it), but it's perfectly safe to use for strings when done properly. The other thing is that it is more flexible than most assume/know about, so it can be used for more complex input though there is a limit (it's still rather restrictive compared to other string processing solutions that other languages support, and it can also be less efficient to use fscanf over handling the converting yourself as it needs to parse the format string). A fscanf conversion format specifier (something that begins with %) can set whether it should be ignored (won't be assigned to one of the arguments), a maximum field width (can limit the number of characters to be included in the conversion), a length modifier, and the conversion specifier. Some of the less common conversion specifiers are scansets (%[], or %[^], the latter producing a scanlist of anything but those between the brackets; these can be useful when you want only particular input or you want to include isspace characters), as well as some specifiers specific to some types you may use (size_t, u/intmax_t, ptrdiff_t, stdint types while they don't have their own specifier they do define macros with the appropriate specifier for their type). The first part of the example uses a scanset to accept only numbers, either up to 15 characters (as determined by the maximum field width) or a non-number. While the next part of the example uses the maximum field width to limit the sizes of the numbers we want, as they're packed tightly together. Code: #include <stdio.h> A final example (can't be bothered doing anymore) is of floating point environment access. With it you're able to set different options for how floating point values and operations should be handled (this can be stuff from changing what should cause exceptions, to changing how the values should behave, e.g. how it should be rounded). It's mostly implementation defined, and this example will not be portable. The example assumes that you're compiling on x86 architecture that supports SSE and platform that provides FE_DFL_DISABLE_SSE_DENORMS_ENV functionality, and assumes the it will use SSE over x87 for the floating point operations. In the example it uses the environment access to disable denormals for SSE (which will cause all denormal values to be treated as 0.0). The alternatives for doing this would normally be either using asm and setting the bits in the MXCSR register or using intrinsics or builtins. Code: #include <stdio.h> |
|||
Messages In This Thread |
[C] Hidden features of C - by benwaffle - 12-06-2013, 10:33 PM
RE: Hidden features of C - by yrmt - 13-06-2013, 08:29 AM
RE: Hidden features of C - by bottomy - 13-06-2013, 02:32 PM
RE: Hidden features of C - by bottomy - 20-06-2013, 02:41 AM
RE: Hidden features of C - by venam - 20-06-2013, 03:01 AM
RE: Hidden features of C - by Hans Hackett - 24-06-2013, 06:57 AM
RE: Hidden features of C - by benwaffle - 08-01-2014, 09:56 PM
RE: Hidden features of C - by bottomy - 09-01-2014, 03:54 AM
RE: [C] Hidden features of C - by dami0 - 24-07-2014, 07:23 PM
RE: [C] Hidden features of C - by jmbi - 25-07-2014, 12:43 AM
|