c99

Why are structs not allowed in equality expressions in C? [duplicate]

筅森魡賤 提交于 2019-12-06 01:18:10
问题 This question already has answers here : Why doesn't C provide struct comparison? (5 answers) Closed 2 years ago . The unavailability of structs as comparison operands is one of the more obvious things in C that don't make too much sense (to me). structs can be passed by value and copied via assignments but == is not specified for them. Below are the relevant parts of the C11 standard (draft) that define the constraints of the equality operators ( == and != ) and the simple assignment

CRTSCTS not define when compiling as C99

落爺英雄遲暮 提交于 2019-12-06 01:06:22
问题 I'm writing some serial code on a raspberry pi and switched to C99. When I did I started getting the error "error: ‘CRTSCTS’ undeclared (first use in this function)" $ c99 -M serial01.c | grep termios.h /usr/include/termios.h /usr/include/arm-linux-gnueabihf/bits/termios.h \ $ gcc -M serial01.c | grep termios.h /usr/include/termios.h /usr/include/arm-linux-gnueabihf/bits/termios.h \ using -M reveals 2 termios.h headers. the former does not contain a definition for CRTSCTS and the latter does.

Struct vs string literals? Read only vs read-write? [duplicate]

被刻印的时光 ゝ 提交于 2019-12-05 22:43:08
问题 This question already has answers here : Why are compound literals in C modifiable (2 answers) Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”? (17 answers) Closed last year . Does the C99 standard permit writing to compound literals (structs)? It seems it doesn't provide writing to literal strings. I ask about this because it says in C Programming: A Modern Approach, 2nd Edition on Page 406. Q. Allowing a pointer to a compound literal

Variable Length Array

拥有回忆 提交于 2019-12-05 20:58:12
问题 I would like to know how a variable length array is managed (what extra variables or data structures are kept on the stack in order to have variable length arrays). Thanks a lot. 回答1: It's just a dynamically sized array (implementation-dependent, but most commonly on the stack). It's pretty much like alloca in the old days, with the exception that sizeof will return the actual size of the array, which implies that the size of the array must also be stored somewhere (implementation-dependent

A bug in GCC implementation of bit-fields

柔情痞子 提交于 2019-12-05 20:25:52
问题 Working in C11, the following struct: struct S { unsigned a : 4; _Bool b : 1; }; Gets layed out by GCC as an unsigned (4 bytes) of which 4 bits are used, followed by a _Bool (4 bytes) of which 1 bit is used, for a total size of 8 bytes. Note that C99 and C11 specifically permit _Bool as a bit-field member. The C11 standard (and probably C99 too) also states under §6.7.2.1 'Structure and union specifiers' ¶11 that: An implementation may allocate any addressable storage unit large enough to

Does this pointer casting break strict aliasing rule?

早过忘川 提交于 2019-12-05 19:16:23
问题 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

Why are no strict-aliasing warnings generated for this code?

假装没事ソ 提交于 2019-12-05 18:48:48
I have the following code: struct A { short b; }; struct B { double a; }; void foo (struct B* src) { struct B* b = src; struct A* a = (struct A*)src; b->a = sin(rand()); if(a->b == rand()) { printf("Where are you strict aliasing warnings?\n"); } } I'm compiling the code with the following command line: gcc -c -std=c99 -Wstrict-aliasing=2 -Wall -fstrict-aliasing -O3 foo.c I'm using GCC 4.5.0. I expected the compiler to print out the warning: warning: dereferencing type-punned pointer will break strict-aliasing rules But it never is. I can get the warning to be printed out for other cases, but I

Is there a preprocessor macro to detect C99 across platforms?

爱⌒轻易说出口 提交于 2019-12-05 18:04:09
问题 C++ has a __cplusplus preprocessor define that lets you detect the version. Is there anything similar for C? Preferably I'd like it to be portable across XCode, GCC, and Visual Studio versions. 回答1: As per article on Wikipedia on C99 A standard macro __STDC_VERSION__ is defined with value 199901L to indicate that C99 support is available #if __STDC_VERSION__ >= 199901L /*C99*/ #else /*Not C99*/ #endif 回答2: You can test the value of the macro __STDC_VERSION__ (note there are two underscores in

Are C unions never padded at the beginning?

依然范特西╮ 提交于 2019-12-05 17:33:52
Are there any guarantees in the C99 standard that unions will only ever be padded at the end like structs? And relatedly, will the address of the union always be equal to the address of any of its possible members? Yes. As you note, structures never have leading padding. The address of a union always refers to the first element of any component of the union (with suitable casts), so there can't be any padding at the start of a union either. Yes. Suitably cast, the address of a union is also a pointer to any of the elements within the union. ISO/IEC 9899:2011 6.7.2.12 Structure and union

How to define extern variable along with declaration?

…衆ロ難τιáo~ 提交于 2019-12-05 12:48:31
问题 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