c11

Why does auto a=1; compile in C?

佐手、 提交于 2019-11-27 17:25:10
The code: int main(void) { auto a=1; return 0; } gets compiled without errors by the MS Visual Studio 2012 compiler, when the file has the .c extension. I have always thought that when you use the .c extension, compilation should be according to the C syntax, and not C++. Moreover, as far as I know auto without a type is allowed only in C++ since C++11, where it means that the type is deduced from the initializer. Does that mean that my compiler isn't sticking to C, or is the code actually correct in C-language? Fred Foo auto is an old C keyword that means "local scope". auto a is the same as

What is gets() equivalent in C11?

冷暖自知 提交于 2019-11-27 14:47:01
问题 From cplusplus.com The most recent revision of the C standard (2011) has definitively removed this function from its specification The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3). I just wanted to know what is the alternative to gets() in C11 standard? 回答1: In C11 gets has been substituted by gets_s that has the following declaration: char *gets_s(char *str, rsize_t n); This function will read at most n-1 chars from stdin into *str . This is to avoid the buffer

Assignment operator sequencing in C11 expressions

一世执手 提交于 2019-11-27 14:45:28
Introduction The C11 standard (ISO/IEC 9899:2011) has introduced a new definition of side effect sequencing within an expression ( see related question ). The sequence point concept has been complemented with sequenced before and sequenced after relations which are now the basis for all definitions. Section 6.5 "Expressions", point 2 says: If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of

C11 _Generic: how to deal with string literals?

本秂侑毒 提交于 2019-11-27 14:18:26
Using the _Generic feature in C11, how do you deal with string literals? For instance: #include <stdio.h> #define foo(x) _Generic((x), char *: puts(x)) int main() { foo("Hello, world!"); return 0; } gives this error on clang: controlling expression type 'char [14]' not compatible with any generic association type Replacing char * with char[] gives me error: type 'char []' in generic association incomplete The only ways (to my knowledge) of getting this to compile are: Cast the string literal to an appropriate type. This is ugly and (in my view) defeats the point of _Generic in the first place.

C11 <thread.h> in GCC?

别等时光非礼了梦想. 提交于 2019-11-27 13:55:24
I’m trying to compile some C11 code using thread.h , but I can’t. I've recompiled GCC (running 4.6.2 now), and I’m trying to compile with gcc -std=c1x file.c -o file . I can do this in g++ (using the thread library, that is) but I can’t in C. Is thread.h not included in the GCC distribution yet? The standard C11 header for threading is <threads.h> , not <thread.h> . See section 7.26 of the N1570 draft . Most of the C standard library, including stdio for example, is not included in the gcc distribution. Instead, gcc depends on whatever runtime library is provided by the operating system. That

How to extract the source filename without path and suffix at compile time?

限于喜欢 提交于 2019-11-27 12:25:18
Using both gcc with -std=c11 and g++ with -std=c++14. E.g. for a file named src/dir/Hello.cxx it should expand to something like e.g.: const char basename[] = "Hello"; or const char basename[] = getStaticBasename(__FILE__); as where getStaticBasename() is a macro (for C sources) or constexpr function (for C++ sources) which results to "Hello". I have to avoid splitting the string from __FILE__ at runtime, because the path and suffix must not be compiled into the executable in any way. The solution must be without dependencies to huge libraries such as boost. As I have no makefiles, solutions

Compilers that support C11

丶灬走出姿态 提交于 2019-11-27 11:40:52
问题 I was wondering if there are any compilers that support a considerable amount of the new C11 standard. Looking for features like Generic Selection etc. Any suggestions? 回答1: Pelles C version 7.00 (Release Candidate is available now) http://www.smorgasbordet.com/pellesc/ 回答2: Your best bet is probably Clang. See the release notes for the current release and the upcoming one. 回答3: GCC 4.9 supports generic selection . It is in general bugfixing stage before release. http://gcc.gnu.org/gcc-4.9

Generics for multiparameter C functions in C11

百般思念 提交于 2019-11-27 11:18:21
问题 I understand C11 generics for one-parameter functions, like this: (from here) #define acos(X) _Generic((X), \ long double complex: cacosl, \ double complex: cacos, \ float complex: cacosf, \ long double: acosl, \ float: acosf, \ default: acos \ )(X) But, it seems to be a pain for functions with two arguments, you need to nest calls to _Generic , which is really ugly; Excerpt from the same blog: #define pow(x, y) _Generic((x), \ long double complex: cpowl, \ double complex: _Generic((y), \

std::isfinite on MSVC

只愿长相守 提交于 2019-11-27 07:39:24
问题 The C++11 and C11 standard define the std::isfinite function. Visual Studio 2012 doesn't seem to provide it as part of the cmath or math.h , but has amp_math.h which seems to provide this function. Is the isfinite interchangeable with std::isfinite ? The documentation doesn't talk about the behavior when called with NAN and I don't have a VS compiler to test this. 回答1: As Marius has already pointed out, the isfinite from amp_math.h is to be used in C++ AMP, which is an MS extension for

String input using C scanf_s

爱⌒轻易说出口 提交于 2019-11-27 07:10:38
问题 I've been trying to look for answer myself, but I can't find one. I want to insert a part of the programming that reads in a string like "Hello" and stores and can display it when I want, so that printf("%s", blah); produces Hello . Here's the code part that's giving me trouble char name[64]; scanf_s("%s", name); printf("Your name is %s", name); I know that printf isn't the problem; the program crashes after something is input after a prompt. Please help? 回答1: From the specification of fscanf