c99

C preprocessor using the closing bracket of a parent macro

孤者浪人 提交于 2019-12-22 05:22:05
问题 I have this code which works: #include <stdio.h> #define A(x) x B #define B(x) C(x, #define C(x,y) y x) int main( void ) { printf( A("1") ("2") "3" ); } It prints 132 (the point of the A macro is to swap the thing which follows its parameters in brackets with everything after that until another closing bracket) But if I use that within another macro: #define Z(x) x printf( Z( A("1") ("2") "3" ) ); I get the compile error "Unterminated function-like macro invocation". I realise that this

Token pasting in C

爱⌒轻易说出口 提交于 2019-12-22 05:15:21
问题 After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) hello ## PP_NARG(__VA_ARGS__) int main(void) { hello("hi"); // call hello1("hi"); hello("foo","bar"); // call hello2("foo","bar"); return 0; } I've read this from C-faq. But still

Forward declaration of inline functions

眉间皱痕 提交于 2019-12-22 01:37:58
问题 I have a header file that is going to contain a large amount (30+) of inline functions. Rather than having the reader scroll or search for the definition (implementation) of the inline function, I would like to have a forward declaration section that states the function declaration with comments describing the function. This section would allow the reader to find out how to use a function or to look for a function without having to scroll down to the implementation. Also, I would like the

Is it safe to cast a heap allocated pointer to a pointer to a VLA?

不羁岁月 提交于 2019-12-21 12:23:25
问题 If I've got a pointer to some heap allocated space that represents a typical row-major two dimensional array, is it safe to cast this pointer to an equivalent pointer to a VLA for convenient sub-scripting? Example: // // Assuming 'm' was allocated and initialized something like: // // int *matrix = malloc(sizeof(*matrix) * rows * cols); // // for (int r = 0; r < rows; r++) { // for (int c = 0; c < cols; c++) { // matrix[r * cols + c] = some_value; // } // } // // Is it safe for this function

Can I define variadic C preprocessor macros with __VA_ARGS in the middle instead of the end?

我怕爱的太早我们不能终老 提交于 2019-12-21 12:08:59
问题 GCC complains if I do this: #define M(obj,met, ..., contents) obj##_##met(const void * self, __VA_ARGS__) { \ contents \ } Giving me these 2 reasons: error: missing ')' in macro parameter list warning: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro Apparently, C99 - style variadic macros expect the closing parenthesis immediately after the ellipsis, effectively demanding that the variadic list be the last arguments of the macro. I need it to be in the middle to produce

Compatibility of C89/C90, C99 and C11

对着背影说爱祢 提交于 2019-12-21 09:18:23
问题 I just read: C Wikipedia entry. As far as I know there are 3 different versions of C that are widely used: C89, C99 and C11. My question concerns the compatibility of source code of different versions. Suppose I am going to write a program (in C11 since it is the latest version) and import a library written in C89. Are these two versions going to work together properly when compiling all files according to the C11 specification? Question 1 : Are the newer versions of C i.e. C99, C11 supersets

GCC options to enforce Ansi C standard check?

放肆的年华 提交于 2019-12-21 07:59:07
问题 What gcc options shall I use to enforce ANSI C (C99) warnings/errors? gcc (GCC) 3.4.2 (mingw-special) I'm using: gcc -pedantic -ansi -std=c99 is this correct? 回答1: The -ansi flag is synonymous with the -std=c89 flag. Just using -std=c99 with -pedantic should be sufficient. When in doubt, you can always refer to the GCC documentation. As of GCC 3.4.2, the chapter to read is 2 - Language Standards Supported by GCC. 回答2: This is an old question but I just wanted to add some extra points. Firstly

Why aren't fixed-point types included in C99?

佐手、 提交于 2019-12-21 07:56:20
问题 Thankfully, the complex type modifier was introduced into C99 standard. What I don't understand is why it was decided to omit support for fixed point arithmetic (specifically, support fractional types like 1.15 {signed} or 0.32 {unsigned}) where these types are so fundamental to DSP programming? Does GCC support these through an extension? 回答1: It's been discussed/proposed (e.g., in N938, N953) but those papers have only proposed it as extensions, not additions to the main standard. Those

How to return an anonymous struct in C?

三世轮回 提交于 2019-12-21 06:46:17
问题 Trying some code I realized that the following code compiles: struct { int x, y; } foo(void) { } It seems as if we are defining a function named foo which returns an anonymous struct . Now, my question is: Does it only happen to compile with my compiler or is this legal C(99)? If so, what is the correct syntax for a return statement and how can I correctly assign the returned value to a variable? 回答1: The struct you're returning is not an anonymous struct. C standard defines an anonymous

Are zero-length variable length arrays allowed/well defined?

99封情书 提交于 2019-12-21 05:11:54
问题 I'm programming in C99 and use variable length arrays in one portion of my code. I know in C89 zero-length arrays are not allowed, but I'm unsure of C99 and variable length arrays. In short, is the following well defined behavior? int main() { int i = 0; char array[i]; return 0; } 回答1: No, zero-length arrays are explicitly prohibited by C language, even if they are created as VLA through a run-time size value (as in your code sample). 6.7.5.2 Array declarators ... 5 If the size is an