c99

What are the most useful new features in C99? [closed]

℡╲_俬逩灬. 提交于 2019-11-28 13:23:27
问题 C99 has been around for over 10 years, but support for it has been slow coming, so most developers have stuck with C89. Even today, I'm sometimes mildly surprised when I come across C99 features in C code. Now that most major compilers support C99 (MSVC being a notable exception, and some embedded compilers also lagging behind), I feel that developers who work with C probably ought to know about what C99 features are available to them. Some of the features are just common features that were

Where is <inttypes.h> in Visual Studio 2005?

落爺英雄遲暮 提交于 2019-11-28 13:21:59
I'd like to use the C99 header file inttypes.h in a Visual Studio project (I'd like to printf 64 bit numbers). However, this file does not seem to exist in my install. Is this just not part of VS2005? Are there any alternatives? It's at google . VS doesn't come with <inttypes.h> No, it is not included in VS 2005. An alternative is Boost's implementation in the Boost::Integer library, specifically boost/cstdint.hpp For Visual Studio 2005 see the bug "C99 header <inttypes.h> missing" at http://connect.microsoft.com/VisualStudio/feedback/details/99133/c99-header-inttypes-h-missing#details where

Why is SCHAR_MIN defined as -127 in C99?

这一生的挚爱 提交于 2019-11-28 12:19:48
§5.2.4.2.1 of C99 defines SCHAR_MIN as -127 and SCHAR_MAX as 127. Should not the range for an 8 bit signed integer be -128 to +127? The limits.h for my compiler defines SCHAR_MIN as (-1 << ((CHAR_BIT)-1)) , which is -128 given CHAR_BIT is 8. Is there any reason why SCHAR_MIN was defined -127 and not -128 ? It doesn't actually define SCHAR_MIN as -127, it defines the minimum range of signed characters to -127..127. It does this because it has to be able to handle the other two encoding schemes for signed numbers, those being ones' complement and sign/magnitude. Both of these have a positive and

Generating function declaration using a macro iteration

笑着哭i 提交于 2019-11-28 11:35:18
I'm trying to generate a function declaration using a macro /* goal: generate int f(int a, float b) */ template<typename P> struct ptype; template<typename P> struct ptype<void(P)> { typedef P type; }; #define NAMEe #define COMMAe #define COMMA , #define NAME(N) N PARAMS #define PARAMS(P, ...) COMMA ## __VA_ARGS__ P NAME ## __VA_ARGS__ #define PARAM_ITER(P) P NAME #define PROTO(R, N, P) \ ptype<void R>::type N (PARAM_ITER P (,e)) PROTO((int), f, (int)(a)(float)(b)); It will iteratively process the next (name) or (type) by NAME or PARAMS respectively, with the ... having an empty macro argument

Allocating struct with flexible array member

半世苍凉 提交于 2019-11-28 11:22:25
This is C99 code: typedef struct expr_t { int n_children; foo data; // Maybe whatever type with unknown alignment struct expr_t *children[]; } expr_t; Now, how do I allocate memory ? expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); or expr_t *e = malloc (offsetof (expr_t, children) + n * sizeof (expr_t *)); ? Is sizeof even guaranteed to work on an type with flexible array member (GCC accepts it) ? CrazyCasta expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); is well defined in C99. From the C99 specification 6.7.2.1.16: As a special case, the last element of a

What is the definition of “arithmetic operation” in C99?

天涯浪子 提交于 2019-11-28 10:56:27
In C99, the term arithmetic operation appears 16 times, but I don't see a definition for it. The term arithmetic operator only appears twice in the text (again without definition) but it does appear in the Index: arithmetic operators additive, 6.5.6, G.5.2 bitwise, 6.5.10, 6.5.11, 6.5.12 increment and decrement, 6.5.2.4, 6.5.3.1 multiplicative 6.5.5, G.5.1 shift, 6.5.7 unary, 6.5.3.3 Then we have + - | & (binary) ++ -- * (binary) / % << >> ~ as arithmetic operators, if the Index is considered normative! Perhaps we should identify arithmetic operation as being the use of an arithmetic operator.

Is it always safe to convert an integer value to void* and back again in POSIX?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 09:40:55
This question is almost a duplicate of some others I've found, but this specifically concerns POSIX, and a very common example in pthreads that I've encountered several times. I'm mostly concerned with the current state of affairs (i.e., C99 and POSIX.1-2008 or later), but any interesting historical information is of course interesting as well. The question basically boils down to whether b will always take the same value as a in the following code: long int a = /* some valid value */ void *ptr = (void *)a; long int b = (long int)ptr; I am aware that this usually works, but the question is

Why are typedef identifiers allowed to be declared multiple times?

佐手、 提交于 2019-11-28 09:12:58
From the C99 standard, 6.7(5): A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: for an object, causes storage to be reserved for that object; for a function, includes the function body; for an enumeration constant or typedef name, is the (only) declaration of the identifier. If identifiers with typedef are in fact definitions, then why are they allowed to be declared more than once? Example: int main() { typedef int x; typedef int x; } Above program compiles with no errors. How is this

Is int main() { } (without “void”) valid and portable in ISO C?

走远了吗. 提交于 2019-11-28 08:12:39
The C standard specifies two forms of definition for main for a hosted implementation: int main(void) { /* ... */ } and int main(int argc, char *argv[]) { /* ... */ } It may be defined in ways that are "equivalent" to the above (for example, you can change the parameter names, replace int by a typedef name defined as int , or write char *argv[] as char **argv ). It may also be defined "in some other implementation-defined manner" -- which means that things like int main(int argc, char *argv[], char *envp) are valid if the implementation documents them. The "in some other implementation-defined

How many memory pages do C compilers on desktop OSes use to detect stack overflows?

帅比萌擦擦* 提交于 2019-11-28 07:33:25
问题 This question is related to but different from this one about variable length arrays in C99. The answers point out that one danger with allocating variable length arrays (or just large arrays of a fixed size) in the stack is that the allocation may fail silently, as opposed to, say, calling malloc , which explicitly tells the caller whether allocation succeeded. Modern non-embedded compilation platforms use an invalid memory zone to detect some stack overflows at no additional cost (the