c11

Can unverified scanf call cause an undefined behavior?

江枫思渺然 提交于 2020-01-30 06:07:20
问题 Does below snippet invoke undefined behavior in case of an error? #include <stdio.h> int main() { int i; /* Indeterminate */ if (scanf("%d", &i) == 1) /* Initialize */ printf("%d\n", i); /* Success! Print read value */ else printf("%d\n", i); /* Input failed! Is printing `i` UB or not? */ return 0; } What if scanf fails, is an uninitialized variable accessed? EDIT Moreover what if I replace scanf("%d", &i) with my_initializer(&i) : int my_initializer(int *pi) { double room_temp_degc = get

Why do C11 global and local static asserts behave differently?

三世轮回 提交于 2020-01-24 00:47:06
问题 Consider the following code: const int g_foo = 1; // (1): _Static_assert(g_foo > 0, "g_foo > 0"); // error: expression in static assertion is not constant. _Static_assert(g_foo > 2, "g_foo > 2"); // error: expression in static assertion is not constant. void Foo(void) { const int foo = 1; // (2): _Static_assert(foo > 0, "foo > 0"); // No issue. _Static_assert(foo > 2, "foo > 2"); // error: static assertion failed: "foo > 2" // (3): _Static_assert(g_foo > 0, "g_foo > 0"); // No issue. _Static

Case variadic macro in C

拈花ヽ惹草 提交于 2020-01-05 05:18:50
问题 I have 2 wrapper macros for asserting function input parameters: /** * @brief An assert wrapper with no value return in case assert fails. * @param x_: value to test for being non zero. */ #define UTIL_ASSERT_VOID(x_) \ assert_param(x_); \ if (!x_) \ return; \ /** * @brief An assert wrapper with a value return in case assert fails. * @param x_: value to test for being non zero. */ #define UTIL_ASSERT_VAL(x_, ret_) \ assert_param(x_); \ if (!x_) \ return ret_; \ The former is used in functions

Declaring pointers to function returning arrays is actually legal?

泄露秘密 提交于 2020-01-04 06:24:07
问题 At least by the C11 standard and from what I've read. The only place where return type is not allowed to be an array type is in the section of function definitions (at $6.9.1.3): The return type of a function shall be void or a complete object type other than array type. At function calls ($6.5.2.2.1) it states this: The expression that denotes the called function shall have type pointer to function returning void or returning a complete object type other than an array type . Which means that

Can an implementation specify undefined behavior

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 19:14:46
问题 3.4.1 1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made Can an implementation specify that, implementation-defined behavior is undefined behavior in cases where undefined behavior is a possible outcome? For example: 6.3.1.3 Signed and unsigned integers 3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. So as long

Combining _Generic macros

≡放荡痞女 提交于 2020-01-03 19:07:07
问题 I am delighted by C11's _Generic mechanism - switching on type is something I miss from C++. It is however proving difficult to compose. For an example, given functions: bool write_int(int); bool write_foo(foo); bool write_bar(bar); // bool write_unknown is not implemented I can then write #define write(X) _Generic((X), \ int : write_int, \ foo: write_foo, \ bar: write_bar, \ default: write_unknown)(X) and, provided I don't try to use &write or pass it to a function, I can call write(obj) and

Casting pointers to _Atomic pointers and _Atomic sizes

为君一笑 提交于 2020-01-03 15:39:10
问题 By my reading of the standard, *(_Atomic TYPE*)&(TYPE){0} (in words, casting a pointer to a non-atomic to a pointer to a corresponding atomic and dereferencing) isn't supported. Do gcc and/or clang recognize it as an extension if TYPE is/isn't lock-free? (Question 1) Second and related question: I was under the impression that if TYPE couldn't be implemented as a lock free atomic, a lock would need to be embedded in the corresponding _Atomic TYPE . But if I make TYPE a largish struct, then on

Is cast of pointer to anonymous union valid in C11?

浪子不回头ぞ 提交于 2020-01-03 08:54:05
问题 const Boo *constBoo; Boo *nonConstBoo; nonConstBoo = ((union {const Boo *_q; Boo *_nq;})constBoo)._nq; Is the above construct valid in C11, or is it only GCC/clang extension that you can cast a pointer to an anonymous union in this fashion? If it is not valid, is there any other way to write an equivalent expression in valid C11 code? The intention was to emulate C++ const_cast that would be C11 compatible and provide some rudimentary type safety. An explicit cast from const to non-const

What ABI, if any, restricts the size of [u]intmax_t?

烈酒焚心 提交于 2020-01-03 07:22:32
问题 Starting with the 1999 edition, the ISO C standard defines a standard header <stdint.h> which defines, among other things, the typedefs intmax_t and uintmax_t . These designate, respectively, "a (signed|unsigned) integer type capable of representing any value of any (signed|unsigned) integer type". For example, if, as is typical, the widest signed and unsigned integer types are long long int and unsigned long long int , both of which are typically 64 bits, then intmax_t and uintmax_t might be

Why doesn't C11 support lambda functions

ぃ、小莉子 提交于 2020-01-02 02:43:09
问题 The new C++11 standard supports lambda functions, which I think is a useful feature. I understand that the C and C++ standards differ from each other but I don't understand why C11 doesn't support lambda functions. I think it could have a lot of use. Is there a reason why the developers of the C11 standard choose not to include this feature? 回答1: This is really just my opinion, since I don't know what the committee thinks. On the one hand, Lisp has been supporting lambda expression since its