c-preprocessor

C preprocessor stringification weirdness

半腔热情 提交于 2019-12-10 13:59:19
问题 I am defining a macro that evaluates to a constant string, holding the filename and the line number, for logging purposes. It works fine, but I just can't figure out why 2 additional macros are needed - STRINGIFY and TOSTRING , when intuition suggests simply __FILE__ ":" #__LINE__ . #include <stdio.h> #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) #define THIS_ORIGIN (__FILE__ ":" TOSTRING(__LINE__)) int main (void) { /* correctly prints "test.c:9" */ printf("%s", THIS_ORIGIN);

Is there any C preprocessor as an independent program?

折月煮酒 提交于 2019-12-10 13:56:55
问题 I know that C preprocessor exists as part of compiler. But I'm looking for an independent program. Is there any such tool? 回答1: It's often called cpp . For example, on my Linux box: CPP(1) GNU CPP(1) NAME cpp - The C Preprocessor SYNOPSIS cpp [-Dmacro[=defn]...] [-Umacro] [-Idir...] [-iquotedir...] [-Wwarn...] [-M|-MM] [-MG] [-MF filename] [-MP] [-MQ target...] [-MT target...] [-P] [-fno-working-directory] [-x language] [-std=standard] infile outfile This particular one is part of gcc and is

How to concatenate strings with C preprocessor with dots in them?

烂漫一生 提交于 2019-12-10 13:51:19
问题 I've read the following question and the answer seems clear enough: How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"? But what if VARIABLE has a dot at the end? I'm trying to do a simple macro that increments counters in a struct for debugging purposes. I can easily do this even without the help from the above question simply with #ifdef DEBUG #define DEBUG_INC_COUNTER(x) x++ #endif and call it DEBUG_INC_COUNT(debugObj.var1); But adding "debugObj."

Count lines between two code locations in C preprocessor

為{幸葍}努か 提交于 2019-12-10 13:47:38
问题 I want to use the C preprocessor to count the amount of lines between two code locations. The basic idea is something like this: #define START __LINE__ static char* string_list[] = { "some string", "another string", ... "last string" }; #define END __LINE__ #if END - START > 42 #error Too many entries #endif Of course this doesn't work because in this case START and END are merely redefinitions of the __LINE__ macro. I was playing around a bit with the # and ## operators, but I could not get

Evaluate macro parameter once only

我是研究僧i 提交于 2019-12-10 13:44:24
问题 In the following code, whatever is passed as retval is evaluated as given for every use of that token. #define _CPFS_RETURN(commit, retval) do { \ util_cpfs_exit(commit); \ return retval; \ } while (false) #define CPFS_RETURN_BOOL(retval) do { \ _CPFS_RETURN(retval, retval); \ } while (false) For example given the use CPFS_RETURN_BOOL(inode && file_truncate(inode, len)); , this is generated: do { do { util_cpfs_exit(inode && file_truncate(inode, len)); return inode && file_truncate(inode, len

Iterate through struct by variable name

做~自己de王妃 提交于 2019-12-10 13:39:09
问题 UPDATE: 6 months later, and I just came across this answer: Is it legal to index into a struct?: Answer by Slava . I think this is a MUCH better solution than any of the ones provided here, as there is absolutely no undefined behavior. Hopefully this helps the next person, since it is already too late for me to implement. Before you comment telling me to use an array or vector, or any form of container instead, it is a hard truth that I cannot. I know, this would be solved with an array, and

Is #ifdef MACRO equivalent to a comment

感情迁移 提交于 2019-12-10 13:38:55
问题 Assuming that MACRO is not defined, are these equivalent #ifdef MACRO Not valid C or C++ code #endif /* Not valid C or C++ code */ In GCC 4.7.1, it seems to be equivalent but are there preprocessors that do more? 回答1: It depends on what you mean by "not valid C or C++ code". Text inside a comment does not have to conform to most of the rules of the language. It isn’t even tokenized. This is perfectly valid: /* This comment doesn't contain a valid sequence of preprocessing tokens (because of

Preprocessor definitions for Universal Windows Platform?

ぐ巨炮叔叔 提交于 2019-12-10 13:37:19
问题 We have a C++ library, and we received a few requests to support UWP. I'm investigating the port now. I'm looking through Microsoft's C/C++ Preprocessor Reference | Predefined Macros for Visual Studio 2015, but I don't see anything related to UWP. I found How to: Use Existing C++ Code in a Universal Windows Platform App, but they look like the old defines for those Metro UI apps: WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PC_APP) WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP) WINAPI_FAMILY

#if inside #define?

混江龙づ霸主 提交于 2019-12-10 13:37:08
问题 I'm sitting on some legacy code that generates a lot of code through #defines. Now I know it's not possible to have an #ifdef inside a #define , but is an #if possible? I would like to do add some specialization for a specific type. (without making major changes like using templates instead). The following sample gives me cryptic errors so this is not the way: #define MK_GET(type) \ type get_ ## type (int index) \ { \ #if type == double \ <-- what i want to add specialized code... \ #endif ..

Passing the value of a variable to macro in C

不打扰是莪最后的温柔 提交于 2019-12-10 13:27:15
问题 I'm trying to pass the value of a variable to a macro in C, but I don't know if this is possible. Example: #include <stdio.h> #define CONCVAR(_n) x ## _n int main () { int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9; int i; for (i = 0; i <= 9; i++) CONCVAR(i) = i*5; return 0; } Here, I'm trying to use a macro to assign a value to all x_ variables, using ## tokens. I know I can easily achieve this with arrays, but this is for learning purposes only. CONCVAR(i) is substituted to xi , not x1 (if i ==