c99

Compatible definitions of inline functions for C99 and C++

寵の児 提交于 2019-12-31 02:19:15
问题 I have a utility library of C99 code used by C++11 application code. A few inline functions are declared in the C99 style with code explicitly generated in the translation unit like: // buffer.h inline bool has_remaining(void* obj) { ... } // buffer.c extern inline bool has_remaining(void * obj); However, when I try to use has_remaining in the C++ application, I get errors about multiple definitions at link time. It seems that g++ is instantiating the inline code that already exists in the

How to eliminate the “discard qualifier” warning?

独自空忆成欢 提交于 2019-12-30 10:12:29
问题 Using GCC and C99 mode, I have a function declared as: void func(float *X); When I call the function, I use a volatile array Y: volatile float Y[2]; int main() { func(Y); return 0; } When compiling (with -Wall ), I get the following warning: warning: passing argument 1 of ‘func’ discards qualifiers from pointer target type blah.c:4: note: expected ‘float *’ but argument is of type ‘volatile float *’ I can eliminate it with an explicit (float *) type cast, but this repeats in many places in

Implementing single-precision division as double-precision multiplication

坚强是说给别人听的谎言 提交于 2019-12-29 08:04:26
问题 Question For a C99 compiler implementing exact IEEE 754 arithmetic, do values of f , divisor of type float exist such that f / divisor != (float)(f * (1.0 / divisor)) ? EDIT: By “implementing exact IEEE 754 arithmetic” I mean a compiler that rightfully defines FLT_EVAL_METHOD as 0. Context A C compiler that provides IEEE 754-compliant floating-point can only replace a single-precision division by a constant by a single-precision multiplication by the inverse if said inverse is itself

A tested implementation of Peterson Lock algorithm?

痴心易碎 提交于 2019-12-29 07:00:06
问题 Does anyone know of a good/correct implementation of Peterson's Lock algorithm in C? I can't seem to find this. Thanks. 回答1: I won't make any assertions about how good or correct the implementation is, but it was tested (briefly). This is a straight translation of the algorithm described on wikipedia. struct petersonslock_t { volatile unsigned flag[2]; volatile unsigned turn; }; typedef struct petersonslock_t petersonslock_t; petersonslock_t petersonslock () { petersonslock_t l = { { 0U, 0U }

C type casts and addition precedence

我怕爱的太早我们不能终老 提交于 2019-12-29 06:36:07
问题 What's the precedence in the next expression? item = (char*)heap + offset; Is it (char*)(heap + offset) or ((char*)heap) + offset ? 回答1: Cast trumps binary addition according to the precedence table. 回答2: It's ((char *)heap) + offset . Casts have much higher precedence than addition. 回答3: ((char*)heap) + offset 回答4: The cast is done first, since it has a much higher precedence. You can look that up in the C precedence table! 来源: https://stackoverflow.com/questions/3354446/c-type-casts-and

Why weren't new (bit width specific) printf() format option strings adoped as part of C99?

别来无恙 提交于 2019-12-29 04:40:26
问题 While researching how to do cross-platform printf() format strings in C (that is, taking into account the number of bits I expect each integer argument to printf() should be) I ran across this section of the Wikipedia article on printf(). The article discusses non-standard options that can be passed to printf() format strings, such as (what seems to be a Microsoft-specific extension): printf("%I32d\n", my32bitInt); It goes on to state that: ISO C99 includes the inttypes.h header file that

How to parse a string into a datetime struct in C?

烂漫一生 提交于 2019-12-29 01:51:06
问题 I would like to have a string (char*) parsed into a tm struct in C. Is there any built-in function to do that? I am referring to ANSI C in C99 Standard. 回答1: While POSIX has strptime(), I don't believe there is a way to do this in standard C. 回答2: There is a function called strptime() available in time.h in UNIX derived systems. It is used similar to scanf() . You could just use a scanf() call if you know what format the date is going to be in. I.E. char *dateString = "2008-12-10"; struct tm

printf/fprintf maximum size according to c99

一个人想着一个人 提交于 2019-12-29 01:41:05
问题 The C99 standard says: The number of characters that can be produced by any single conversion shall be at least 4095 Does it mean that the maximum size is 4095 if yes why its says "at least"? 回答1: You've found one of the more annoying aspects of the C language specifications. They don't usually say what a maximum is. Instead, they'll usually say what the smallest allowed value for a maximum is. They recognized that different hardware / compiler / linker environments have different

C99 - why are false and true defined as 0 and 1 and not as ((bool)0) and ((bool)1)?

牧云@^-^@ 提交于 2019-12-29 01:38:13
问题 Just stumbled across an assert, that failed, as it compared false to the returntype of a function, as the function itself returned a bool and the assert checked not only the value, but also the type of the returnvalue to match the one of false, to guarantee, that a bool is returned. Now the problem is, that C99 defines bool as _Bool and _Bool is even not necessarily same size as int (in fact, in my experience, on most platforms in nowadays it is often same size as unsigned char), not to talk

Is a compiler allowed to add functions to standard headers?

偶尔善良 提交于 2019-12-28 06:58:26
问题 Is a C compiler allowed to add functions to standard headers and still conform to the C standard? I read this somewhere, but I can't find any reference in the standard, except in annex J.5: The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers , or predefined macros with names that do not begin with an underscore.