c99

bool to int conversion

柔情痞子 提交于 2019-11-26 07:31:11
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. Nawaz 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) So bool to int conversion is relevant in C++ only. In C, 4<5 evaluates to int value, in this case the

Does the C preprocessor strip comments or expand macros first? [duplicate]

感情迁移 提交于 2019-11-26 06:44:16
问题 This question already has answers here : In which step of compilation are comments removed? (2 answers) Closed 2 years ago . Consider this (horrible, terrible, no good, very bad) code structure: #define foo(x) // commented out debugging code // Misformatted to not obscure the point if (a) foo(a); bar(a); I\'ve seen two compilers\' preprocessors generate different results on this code: if (a) bar(a); and if (a) ; bar(a); Obviously, this is a bad thing for a portable code base. My question:

func() vs func(void) in c99

[亡魂溺海] 提交于 2019-11-26 06:37:02
问题 void func() In practice, an empty parameter means any argument is accepted. void func(void) accepts no argument. But in Standard C99, I find such lines: 6.7.5.3 Function declarators (including prototypes) 14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition

How to implement memmove in standard C without an intermediate copy?

夙愿已清 提交于 2019-11-26 06:34:10
问题 From the man page on my system: void *memmove(void *dst, const void *src, size_t len); DESCRIPTION The memmove() function copies len bytes from string src to string dst. The two strings may overlap ; the copy is always done in a non-destructive manner. From the C99 standard: 6.5.8.5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both

What is the difference between C, C99, ANSI C and GNU C?

余生颓废 提交于 2019-11-26 06:04:04
问题 I have started programming practice on codechef and have been confused by the difference between C and C99. What does C mean here? Is it C89? Check the languages at the bottom of this submit. It contains both C and C99. I found on the internet something called GNU C. Is there a different C for linux/unix systems? Are these compliant to the C standards by ANSI? I have also read in some places \"C99 strict\". What is this? Are there any other different standards of C in use? Is there something

GCC with -std=c99 complains about not knowing struct timespec

有些话、适合烂在心里 提交于 2019-11-26 05:34:36
问题 When I try to compile this on Linux with gcc -std=c99 , the compiler complains about not knowing struct timespec . However if I compile this without -std=c99 everything works fine. #include <time.h> int main(void) { struct timespec asdf; return 0; } Why is this and is there a way to still get it to work with -std=c99 ? 回答1: Explicitly enabling POSIX features The timespec comes from POSIX, so you have to 'enable' POSIX definitions: #if __STDC_VERSION__ >= 199901L #define _XOPEN_SOURCE 600

Does either ANSI C or ISO C specify what -5 % 10 should be?

微笑、不失礼 提交于 2019-11-26 04:52:55
问题 I seem to remember that ANSI C didn\'t specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly? 回答1: C89, not totally (§3.3.5/6). It can be either -5 or 5, because -5 / 10 can return 0 or -1 ( % is defined in terms of a linear equation involving / , * and + ): When integers are divided and the division is inexact, if both operands are

Standard alternative to GCC&#39;s ##__VA_ARGS__ trick?

喜夏-厌秋 提交于 2019-11-26 03:36:42
问题 There is a well-known problem with empty args for variadic macros in C99. example: #define FOO(...) printf(__VA_ARGS__) #define BAR(fmt, ...) printf(fmt, __VA_ARGS__) FOO(\"this works fine\"); BAR(\"this breaks!\"); The use of BAR() above is indeed incorrect according to the C99 standard, since it will expand to: printf(\"this breaks!\",); Note the trailing comma - not workable. Some compilers (eg: Visual Studio 2010) will quietly get rid of that trailing comma for you. Other compilers (eg:

Return value range of the main function

≯℡__Kan透↙ 提交于 2019-11-26 02:11:45
What does standard say about main return values range? Say only up to 255? Because int main(void){ return 256; } echo $? ; # out 0 Jerry Coffin 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 it, since even 64-bit Windows normally still uses a 32-bit int. As others have stated, the C &

Enabling VLAs (variable length arrays) in MS Visual C++?

笑着哭i 提交于 2019-11-26 02:02:29
问题 How can I enable the use of VLAs, variable length arrays as defined in C99, in MS Visual C++ or that is not possible at all? Yes I know that the C++ standard is based on C89 and that VLAs are not available in C89 standard and thus aren\'t available in C++, but MSVC++ is supposed to be a C compiler also, a behavior that can be switched on using the /TC compiler parameter ( Compile as C Code (/TC) ). But doing so does not seem to enable VLAs and the compiling process fails with the same errors