c99

GNU89, mixed declarations and loop initial declarations

孤人 提交于 2019-12-18 07:04:33
问题 The default C dialect for GCC and ICC is GNU89. GNU89 allows mixed declarations e.g. int i; i = 0; int j; I inferred (incorrectly) from a number of other posts on SO e.g C: for loop int initial declaration, that this meant I could do for(int i=0; i<n; i++) with GNU89 but when I do this I get error: 'for' loop initial declarations are only allowed in C99 mode Apparently, mixed declarations and loop initial declarations are not the same thing (i.e. one does not imply the other). If I could only

Variable-length arrays in C89?

冷暖自知 提交于 2019-12-18 05:47:11
问题 I've read that C89 does not support variable-length arrays, but the following experiment seems to disprove that: #include <stdio.h> int main() { int x; printf("Enter a number: "); scanf("%d", &x); int a[x]; a[0] = 1; // ... return 0; } When I compile as such (assuming filename is va_test.c ): gcc va_test.c -std=c89 -o va_test It works... What am I missing? :-) 回答1: GCC always supported variable length arrays AFAIK. Setting -std to C89 doesn't turn off GCC extensions ... Edit: In fact if you

static_if in C99's preprocessor

和自甴很熟 提交于 2019-12-18 05:16:28
问题 Is it possible to implement static_if in C99? #define STATIC_IF(COND, ...) \ if (COND) MACRO1(__VA_ARGS__); \ else MACRO2(__VA_ARGS__); How can I properly implement STATIC_IF(…) in here? Depending on COND the arguments either should be passed to MACRO1 or MACRO2 , but the arguments for both macros look differently. COND is statically testable, something like sizeof (…) > 42 . #if COND then #define STATIC_IF MACRO1 … wouldn't work for my use case. I cannot use compiler specific solutions. 回答1:

Variable Length Arrays in C++14?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 04:54:22
问题 n3639 proposed the adoption of c99's variable-length-arrays into C++14 (at least for the first dimension.) But the latest I've been able to find lists n3639 as: Features in the first CD of C++14, subsequently removed to a Technical Specification Did this ever make it into a Technical Specification, or was it lost in the hand off? The reason for my question is, I've noticed this code: void f(size_t n) { int a[n]; for (size_t i = 0; i < n; ++i) a[i] = 2 * i; sort(a, a + n); } This fails to

Do C99 signed integer types defined in stdint.h exhibit well-defined behaviour in case of an overflow?

佐手、 提交于 2019-12-18 04:44:11
问题 All operations on "standard" signed integer types in C (short, int, long, etc) exhibit undefined behaviour if they yield a result outside of the [TYPE_MIN, TYPE_MAX] interval (where TYPE_MIN, TYPE_MAX are the minimum and the maximum integer value respectively. that can be stored by the specific integer type. According to the C99 standard, however, all intN_t types are required to have a two's complement representation: 7.8.11.1 Exact-width integer types 1. The typedef name intN_t designates a

When __builtin_memcpy is replaced with libc's memcpy

China☆狼群 提交于 2019-12-18 03:56:00
问题 There is a version of C99/posix memcpy function in GCC: __builtin_memcpy . Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here: Finally, on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call. What is the logic in this selection? Is it logic the same in other gcc-compatible compilers, like clang/llvm, intel c++ compiler, PCC, suncc (oracle studio)? When I should prefer

How much is it possible to create fake-functions with macros in C?

你。 提交于 2019-12-18 03:34:09
问题 People always say that macros are unsafe, and also that they are not (directly) type-checking on their arguments, and so on. Worse: when errors occur, the compiler gives intrincate and incomprehensible diagnostics, because the macro is just a mess. Is it possible to use macros in almost the same way as a function, by having safe type-checking, avoiding typical pitfalls and in a way that the compiler gives the right diagnostic. I am going to answer this question (auto-answering) in an

How to compile for a freestanding environment with GCC?

旧街凉风 提交于 2019-12-18 02:24:07
问题 The code I'm working on is supposed to be possible to build for both hosted and freestanding environments, providing private implementations for some stdlib functions for the latter case. Can I reliably test this with just GCC on a normal workstation/build server? Compile for freestanding environment with GCC The "-ffreestanding" option looked promising, but it seems that it "only" disables built-ins and sets the STDC_HOSTED macro properly, it still provides all system headers. The option "

How universally is C99 supported?

微笑、不失礼 提交于 2019-12-17 22:33:42
问题 How universally is the C99 standard supported in today's compilers? I understand that not even GCC fully supports it. Is this right? Which features of C99 are supported more than others, i.e. which can I use to be quite sure that most compilers will understand me? 回答1: If you want to write portable C code, then I'd suggest you to write in C89 (old ANSI C standard). This standard is supported by most compilers. The Intel C Compiler has very good C99 support and it produces fast binaries.

Which C99-compiler (Clang vs. GCC) is closer to standard on const structure fields?

非 Y 不嫁゛ 提交于 2019-12-17 20:48:56
问题 I have code like this: $ cat test.c #include <stdio.h> typedef struct { const int x; } SX; static SX mksx(void) { return (SX) { .x = 10 }; } void fn(void) { SX sx; while((sx = mksx()).x != 20) { printf("stupid code!"); } } And 2 opinions about its correctness: $ for i in gcc clang; do echo "$i SAYS:"; $i -c -std=c99 -pedantic -Werror test.c; done gcc SAYS: test.c: In function ‘fn’: test.c:15:2: error: assignment of read-only variable ‘sx’ while((sx = mksx()).x != 20) ^ clang SAYS: Which