c-preprocessor

C macro to get the smallest power of two greater than a given number

心已入冬 提交于 2019-12-24 11:01:13
问题 I need a C macro to get the smallest of power two greater than a given number. For example, FIRSTFREEBIT(0x16) (binary 1_0110 ) must be equal to 0x20 . I am going to use it as: #include <someheader.h> // defines SOME_X and SOME_Y enum { x = SOME_X, y = SOME_Y, z = FIRSTFREEBIT(x|y), t = z << 1, }; A similar, but slightly different SO question: Algorithm for finding the smallest power of two that's greater or equal to a given value 回答1: Here's my code, you are welcome to invent something

#Define's scope throughout library?

风流意气都作罢 提交于 2019-12-24 10:44:29
问题 Say I have a constant: #define PI 3.14 Say I have a static library with multiple header and source files. If I declare this in the header file, will its scope apply to all of the source files? Or do the source files need to include the header with the declaration of PI ? 回答1: They will need to include the file which contains #define PI 3.14, otherwise the preprocessor will not read the #define line, and subsequently the compile will fail. In C++, a good way to think of the compile process is

Operator 'overloading' equivalent with #define in C/Objective-C [duplicate]

南楼画角 提交于 2019-12-24 10:19:53
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Operator overloading in C If I have a struct: typedef struct myStruct { ... } myStruct; myStruct myStructAdd(myStruct a, myStruct b); I need something like this: #define myStruct a + myStruct b myStructAdd(a, b) // NOTE this code does NOT WORK. This is what the question is asking. To make this syntax valid: myStruct a; myStruct b; myStruct c = a + b; Is there any way to use a #define to do this? EDIT: I'm not

How to call a C function from random places inside another function?

懵懂的女人 提交于 2019-12-24 09:49:34
问题 Can anyone tell me how to insert a function call (say Yield() ) at random places inside a C function, so that each time the code is run, Yield() gets called from different parts of the code ? I am faced with such a requirement as I'm using 2 threads in a cooperative threading environment , where unless the running thread yields the processor explicitly , the other (waiting) thread cannot start running. I don't want to place the Yield() call at a single point , since that makes the thread

How to check Lua version in SWIG interface file?

假如想象 提交于 2019-12-24 08:48:29
问题 I have the below function which I would like to use only when the Lua version is equal to, or smaller than 5.1 . So I wrote it like the following in myBindings.i file: /* used to backport lua_len to Lua 5.1 */ #if LUA_VERSION_NUM <= 501 %{ static void lua_len(lua_State *L, int i) { switch (lua_type(L, i)) { case LUA_TSTRING: lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); break; case LUA_TTABLE: if (!luaL_callmeta(L, i, "__len")) lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); break; case

How to tag some types to build a type list from these tagged types at compile time?

不羁的心 提交于 2019-12-24 07:07:48
问题 I generated some types with an external tool. I include some of them depending on the application. I tried to play with both boost preprocessor and boost mpl to tag my generated types and build an mpl vector of these tagged types. Unfortunately, my trick only works when I am including the headers I need in a single file with my TypeRegistry. // TypeRegistry.h typedef ::boost::mpl::vector<> TypeRegistryRegisteredTypes0; # define TYPE_REGISTRY_REGISTER_TYPE(T) \ typedef ::boost::mpl::push_back<

Is c++ a space free language?

浪子不回头ぞ 提交于 2019-12-24 03:51:16
问题 #define PR ( A, B ) cout << ( A ) << ( B ) << endl ; - error -> A was not declared in scope - error -> B was not declared in scope - error -> expected "," before "cout" I thought C++ was space free language but when I write above code, then I see some errors. I am still thinking "Is my console is not working properly or library?". If I am not wrong, how can someone say "C++ is a space free language" ? 回答1: There are numerous exceptions where whitespace matters; this is one of them. With the

Macro directives in C, my code example doesn't work

前提是你 提交于 2019-12-24 02:49:13
问题 I want to get the following code piece work: #define READIN(a, b) if(scanf('"#%d"', '"&a"') != 1) { printf("ERROR"); return EXIT_FAILURE; } int main(void) { unsigned int stack_size; printf("Type in size: "); READIN(d, stack_size); } I don't get it, how to use directives with the # operator. I want to use the scanf with print ERROR etc. several times, but the "'"#%d"' & '"&a"'" is I think completely wrong. Is there any way to get that running? I think a macro is the best solution or not? 回答1:

C Programming: Preprocessor, macros as tokens

橙三吉。 提交于 2019-12-24 02:06:09
问题 I'm trying to do something that is conceptually similar to this, but can't seem to get it to work (error shown at end) any ideas? #include <stdio.h> int main( int argc , char const *argv[] ) { int abc_def_ghi = 42; #define SUFFIX ghi #define VAR(prefix) prefix##_def_##SUFFIX printf( "%d\n" , VAR(abc) ); return 0; } // untitled:8: error: ‘abc_def_SUFFIX’ undeclared (first use in this function) 回答1: You just need additional indirection: #include <stdio.h> int main( int argc , char const *argv[]

Is there a way to create a preprocessor macro for a function?

亡梦爱人 提交于 2019-12-24 02:03:57
问题 Is it possible to create a C++ preprocessor macro based on a function result? For example, I'd like to save the screen height dynamically in a preprocessor macro definition: #define SCREEN_HEIGHT GetSystemMetrics(SM_CYVIRTUALSCREEN) Then I want to use the result to set values based on the screen height: #if SCREEN_HEIGHT < 1200 #define TOP_COORD 200 #define BOTTOM_COORD 500 #define LEFT_COORD 0 #define RIGHT_COORD 1280 #else #define TOP_COORD 1100 #define BOTTOM_COORD 1400 #define LEFT_COORD