c99

Does this pointer casting break strict aliasing rule?

放肆的年华 提交于 2019-12-04 02:52:54
This is the fast inverse square root implementation from Quake III Arena: float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; } I noticed that long int i takes the dereferenced value at the address (cast to a long * ) of float y . The code then performs operations

GCC options to enforce Ansi C standard check?

半腔热情 提交于 2019-12-04 02:14:17
What gcc options shall I use to enforce ANSI C (C99) warnings/errors? gcc (GCC) 3.4.2 (mingw-special) I'm using: gcc -pedantic -ansi -std=c99 is this correct? James McNellis The -ansi flag is synonymous with the -std=c89 flag. Just using -std=c99 with -pedantic should be sufficient. When in doubt, you can always refer to the GCC documentation . As of GCC 3.4.2, the chapter to read is 2 - Language Standards Supported by GCC . This is an old question but I just wanted to add some extra points. Firstly, regardless of the set of generic command-line switches you supply to GCC, currently it doesn't

Is it guaranteed that Complex Float variables will be 8-byte aligned in memory?

末鹿安然 提交于 2019-12-04 02:04:13
问题 In C99 the new complex types were defined. I am trying to understand whether a compiler can take advantage of this knowledge in optimizing memory accesses. Are these objects ( A - F ) of type complex float guaranteed to be 8-byte aligned in memory? #include "complex.h" typedef complex float cfloat; cfloat A; cfloat B[10]; void func(cfloat C, cfloat *D) { cfloat E; cfloat F[10]; } Note that for D , the question relates to the object pointed to by D , not to the pointer storage itself. And, if

How to define extern variable along with declaration?

橙三吉。 提交于 2019-12-03 23:57:05
Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable . That means, an extern declaration that initializes the variable serves as a definition for that variable . So, /* Just for testing purpose only */ #include <stdio.h> extern int y = 0; int main(){ printf("%d\n", y); return 0; } should be valid ( compiled in C++11 ). But when

printf conversion specifier for _Bool?

和自甴很熟 提交于 2019-12-03 23:42:05
With printf() , I can use %hhu for unsigned char , %hi for a short int , %zu for a size_t , %tx for a ptrdiff_t , etc. What conversion format specifier do I use for a _Bool ? Does one exist in the standard? Or do I have to cast it like this: _Bool foo = 1; printf("foo: %i\n", (int)foo); There is no specific conversion length modifier for _Bool type. _Bool is an unsigned integer type large enough to store the values 0 and 1 . You can print a _Bool this way: _Bool b = 1; printf("%d\n", b); Because of the integer promotions rules, _Bool is guaranteed to promote to int . Until C99, bool was not

What is the point of the C99 standard?

匆匆过客 提交于 2019-12-03 23:36:08
C99 adds several useful features to the language, yet I find it difficult to recommend any practice which depends upon C99. The reason for this is because there are few (any?) actual implementations of the C99 language. Sure, there is limited support in a few compilers, but nobody wants spend the time to write C code only to have it be unportable. This is frustrating given that the standard was written and finalized over 10 years ago now. Plus I hear discussions of a C1x from time to time, and I wonder why someone would be taking steps to revise the language given that the current version of

What is the meaning of “static” in parameters array types in C?

跟風遠走 提交于 2019-12-03 22:27:22
问题 I saw following little complicated function definition. void foo(double A[static 10]) { double B[10]; } Is it valid C & C++ code? Is it new syntax introduced by C99 or C++ standard? What is the purpose of it? When should I use it? What is the need for this? 回答1: This C99 notation, void foo(double A[static 10]) , means that the function can assume that A points to 10 valid arguments (from *A to A[9] ). The notation makes programs more informative, helping compilers to both optimize the code

Are there any implementations that support a negative zero, or reserve it as a trap representation?

天涯浪子 提交于 2019-12-03 20:48:51
On most implementations of this day and age, a signed integer value that has a bit pattern of 1 for the sign bit and all 0 for the value bits tends to represent the lowest possible value for that signed integer type. However, as 6.2.6.2p2 states, that's not a requirement: Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones' complement), is a trap representation or a normal value. My first question is simple: Are there any implementations that use this bit pattern

How to compile a Linux kernel module using -std=gnu99?

让人想犯罪 __ 提交于 2019-12-03 17:31:27
问题 I've recently learned how to program simple character drivers and while playing around with the code I noticed that I get a lot of the following GCC warnings thrown for my C99 code: warning: ISO C90 forbids mixed declarations and code I assume this is because the main Linux kernel Makefile is set to compile using a non-C99 standard. I searched around I found this answer here on stackoverflow: How to use make and compile as C99? So I naturally tried the following in my Makefile: ccflags-y :=

How to enforce C89-style variable declarations in gcc?

本小妞迷上赌 提交于 2019-12-03 16:59:03
问题 I work on a code base which is mostly C with a little C++, and is mostly built with gcc but occasionally it needs to be built with MSVC. Microsoft's C compiler is still pretty much C89 with a few minor extensions, and it still doesn't support mixed code and variable definitions à la C++/C99. So I need to find a way to prevent developers from writing out-of-order code/variable definitions while they are working with gcc, otherwise the build subsequently breaks with MSVC. If I use gcc -std=c89