c11

Why doesn't the compiler detect and produce errors when attempting to modify char * string literals?

纵饮孤独 提交于 2019-11-28 04:55:05
问题 Assume the following two pieces of code: char *c = "hello world"; c[1] = 'y'; The one above doesn't work. char c[] = "hello world"; c[1] = 'y'; This one does. With regards to the first one, I understand that the string "hello world" might be stored in the read only memory section and hence can't be changed. The second one however creates a character array on the stack and hence can be modified. My question is this - why don't compilers detect the first type of error? Why isn't that part of

What are those strange array sizes [*] and [static] in C99?

泄露秘密 提交于 2019-11-28 04:42:22
Apparently the following function prototypes are valid in C99 and C11: void foo(int a[const *]); void bar(int a[static volatile 10]); What is the purpose of those strange subscript notations * , static , and CV qualifiers? Do they help distinguish statically typed arrays from variable-length arrays? Or are they just syntactic sugar? static in parameter array declarator void f(int a[static 10]); static here is an indication that parameter a is a pointer to int but that the array objet (where a is a pointer to its first element) has at least 10 elements. A compiler has then the right to assume f

C11 grammar ambiguity between _Atomic type specifier and qualifier

故事扮演 提交于 2019-11-28 03:02:27
问题 I'm trying to write a lex/yacc grammar for C11 based off of N1570. Most of my grammar is copied verbatim from the informative syntax summary, but some yacc conflicts arose. I've managed to resolve all of them except for one: there seems to be some ambiguity between when '_Atomic' is used as a type specifier and when it's used as a type qualifier. In the specifier form, _Atomic is followed immediately by parentheses, so I'm assuming it has something to do with C's little-used syntax which

What does “representable” mean in C11?

天涯浪子 提交于 2019-11-28 01:11:59
According to C11 WG14 draft version N1570 : The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases the argument is an int , the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF . If the argument has any other value, the behavior is undefined. Is it undefined behaviour?: #include <ctype.h> #include <limits.h> #include <stdlib.h> int main(void) { char c = CHAR_MIN; /* let assume that char is signed and CHAR_MIN < 0 */ return isspace(c) ? EXIT_FAILURE : EXIT_SUCCESS; } Does the standard allow

Why didn't gcc implement _s functions?

北城以北 提交于 2019-11-28 00:04:14
_s functions, such as scanf_s , printf_s seems to be optional standard. MSVC has implemented these functions, but gcc hasn't. Is there specific reason for not implementing secure functions? Is scanf of gcc secure enough? The _s functions are optional ( Annex K of the C11 standard ). They're widely regarded as 'not very beneficial'. In the answers to my question Do you use the TR-24731 "safe" functions? , you can find information about where there are problems with the standard specification — such as crucial differences between the standard and Microsoft's implementation. TR 24731-1 was a

Dynamic array allocation on stack in C

痴心易碎 提交于 2019-11-28 00:04:01
I just did a experiment yesterday, and find something confusing: #include <stdio.h> int main() { int j; scanf("%d",&j); const int i = j; int arr[i]; return 0; } The number j is read from keyboard and it’s used to allocate the array arr on the stack. The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible? Variable length arrays were added to C99. It's described in the C99 rationale: 6.7.5.2 Array declarators C99 adds a new array type called a variable length array type. The inability to declare arrays

Which object declarations in C cause storage to be reserved (i.e. are definitions)?

末鹿安然 提交于 2019-11-27 23:15:11
C11 specifies in section 6.7 which declarations are also definitions: A definition of an identifier is a declaration for that identifier that: — for an object, causes storage to be reserved for that object; [...] I didn't find a comprehensive list of which object declarations cause storage to be reserved. Intuitively it is clear to me, but I wasn't able to get this information out of the C11 standard. There's no definitive list because the standard just describes what are definitions and it's not in a single place. I'll try to sum it up here. I'll only use the type int here without any

Why does C++11 not support anonymous structs, while C11 does?

老子叫甜甜 提交于 2019-11-27 22:39:15
C11 supports anonymous structures, like so: struct Foo { struct { size_t x, y; }; }; struct Foo f; f.x = 17; f.y = 42; Basically, the members of such a struct are treated as if they were members of the enclosing struct or union (recursively, if the enclosing structure was itself anonymous). What was the rationale for C++11 not also including anonymous structures? They're only uncommonly useful (mostly inside unions, to eliminate the typing of an identifier for the struct ), certainly. But they seem an obvious enough addition to the specification (and one already implemented by many compilers)

initialization of anonymous structures or unions in C1X

那年仲夏 提交于 2019-11-27 21:13:22
问题 I have the following question: How are anonymous structures (or unions) properly initialized according to the current C1X draft? Is this legal: struct foo { int a; struct { int i; int j; }; int b; }; struct foo f = { 1, 2, 3, 4 }; struct foo g = { 1, { 2 }, 3 }; In GCC, g.j == 0 and g.b == 3 , while in tcc g.j == 3 and g.b == 0 . The current draft says: "[...] unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have

Is there any option to switch between C99 and C11 C standards in Visual Studio?

帅比萌擦擦* 提交于 2019-11-27 17:43:34
问题 I am new to Visual Studio Environment and I am using VS2017 Pro. I wanted to write simple program in C and compiled with both c99 and c11 standards. In Visual Studio, I could only find compiler switches for C++ standards only. How can we tell visual studio environment that we want to compile current code with c99 and c11 C standards. 回答1: The only 'modes' supported by Visual C++ are: /std:c++14 mode for C++14 conformance (the default), /std:c++17 mode for C++17 support which is not quite