c99

Return value range of the main function

旧巷老猫 提交于 2019-11-26 01:47:51
问题 What does standard say about main return values range? Say only up to 255? Because int main(void){ return 256; } echo $? ; # out 0 回答1: The standard doesn't say. 0 , EXIT_SUCCESS and EXIT_FAILURE have (sort of) specified meanings. Anything else depends on the implementation. At the present time, most Unix-based systems only support 8-bit return values. Windows supports (at least) a 32-bit return value. I haven't checked whether 64-bit Windows supports a 64-bit return value, but I rather doubt

Tentative definitions in C and linking

亡梦爱人 提交于 2019-11-26 01:39:40
问题 Consider the C program composed of two files, f1.c: int x; f2.c: int x=2; My reading of paragraph 6.9.2 of the C99 standard is that this program should be rejected. In my interpretation of 6.9.2, variable x is tentatively defined in f1.c , but this tentative definition becomes an actual definition at the end of the translation unit, and (in my opinion), should therefore behave as if f1.c contained the definition int x=0; . With all compilers (and, importantly, linkers) I was able to try, this

Why does C++11 not support designated initializer lists as C99? [closed]

半世苍凉 提交于 2019-11-26 01:37:37
问题 Consider: struct Person { int height; int weight; int age; }; int main() { Person p { .age = 18 }; } The code above is legal in C99, but not legal in C++11. What was the c++11 standard committee\'s rationale for excluding support for such a handy feature? 回答1: C++ has constructors. If it makes sense to initialize just one member then that can be expressed in the program by implementing an appropriate constructor. This is the sort of abstraction C++ promotes. On the other hand the designated

Printf width specifier to maintain precision of floating-point value

…衆ロ難τιáo~ 提交于 2019-11-26 01:18:55
问题 Is there a printf width specifier which can be applied to a floating point specifier that would automatically format the output to the necessary number of significant digits such that when scanning the string back in, the original floating point value is acquired? For example, suppose I print a float to a precision of 2 decimal places: float foobar = 0.9375; printf(\"%.2f\", foobar); // prints out 0.94 When I scan the output 0.94 , I have no standards-compliant guarantee that I\'ll get the

bool to int conversion

时间秒杀一切 提交于 2019-11-26 00:58:53
问题 How portable is this conversion. Can I be sure that both assertions pass? int x = 4<5; assert(x==1); x = 4>5; assert(x==0); Don\'t ask why. I know that it is ugly. Thank you. 回答1: int x = 4<5; Completely portable. Standard conformant. bool to int conversion is implicit! §4.7/4 from the C++ Standard says (Integral Conversion ) If the source type is bool, the value false is converted to zero and the value true is converted to one . As for C, as far as I know there is no bool in C. (before 1999)

Is it possible to iterate over arguments in variadic macros?

我的梦境 提交于 2019-11-26 00:32:46
问题 I was wondering if it is possible to iterate over arguments passed to a variadic macro in C99 or using any GCC extensions ? For e.g. is it possible to write a generic macro that takes a structure and its fields passed as arguments and prints offset of each field within the structure ? Something like this: struct a { int a; int b; int c; }; /* PRN_STRUCT_OFFSETS will print offset of each of the fields within structure passed as the first argument. */ int main(int argc, char *argv[]) { PRN

Passing a multidimensional variable length array to a function

。_饼干妹妹 提交于 2019-11-26 00:28:58
问题 There are tons of similar questions, but still I could not find any answer relevant for the feature of variable length arrays in C99/C11. How to pass multidimensional variable length array to a function in C99/C11? For example: void foo(int n, int arr[][]) // <-- error here, how to fix? { } void bar(int n) { int arr[n][n]; foo(n, arr); } Compiler ( g++-4.7 -std=gnu++11 ) says: error: declaration of ‘arr’ as multidimensional array must have bounds for all dimensions except the first If I

Visual Studio support for new C / C++ standards?

百般思念 提交于 2019-11-25 23:54:24
问题 I keep reading about C99 and C++11 and all these totally sweet things that are getting added to the language standard that might be nice to use someday. However, we currently languish in the land of writing C++ in Visual Studio. Will any of the new stuff in the standard ever get added to visual studio, or is Microsoft more interested in adding new C# variants to do that? Edit: In addition to the accepted answer, I found the Visual C++ team blog: http://blogs.msdn.com/vcblog/ And specifically,

Is “inline” without “static” or “extern” ever useful in C99?

倖福魔咒の 提交于 2019-11-25 22:46:08
问题 When I try to build this code inline void f() {} int main() { f(); } using the command line gcc -std=c99 -o a a.c I get a linker error (undefined reference to f ). The error vanishes if I use static inline or extern inline instead of just inline , or if I compile with -O (so the function is actually inlined). This behaviour seems to be defined in paragraph 6.7.4 (6) of the C99 standard: If all of the file scope declarations for a function in a translation unit include the inline function

Is type-punning through a union unspecified in C99, and has it become specified in C11?

故事扮演 提交于 2019-11-25 22:35:43
问题 A number of answers for the Stack Overflow question Getting the IEEE Single-precision bits for a float suggest using a union structure for type punning (e.g.: turning the bits of a float into a uint32_t ): union { float f; uint32_t u; } un; un.f = your_float; uint32_t target = un.u; However, the value of the uint32_t member of the union appears to be unspecified according to the C99 standard (at least draft n1124), where section 6.2.6.1.7 states: When a value is stored in a member of an