c-preprocessor

How to make a variadic macro for std::cout?

筅森魡賤 提交于 2019-12-27 15:42:26
问题 How would I make a macro that took a variable amount of arguments, and prints its out using std::cout? Sorry if this is a noob question, couldn't find anything that clarified variadic macros after searching around for the answer. Conceptual Example: #include <iostream> #define LOG(...) std::cout << ... << ... << std::endl int main() { LOG("example","output","filler","text"); return 0; } would output: exampleoutputfillertext 回答1: You do not need preprocessor macros to do this. You can write it

How to make a variadic macro for std::cout?

巧了我就是萌 提交于 2019-12-27 15:42:07
问题 How would I make a macro that took a variable amount of arguments, and prints its out using std::cout? Sorry if this is a noob question, couldn't find anything that clarified variadic macros after searching around for the answer. Conceptual Example: #include <iostream> #define LOG(...) std::cout << ... << ... << std::endl int main() { LOG("example","output","filler","text"); return 0; } would output: exampleoutputfillertext 回答1: You do not need preprocessor macros to do this. You can write it

Should I use #define, enum or const?

柔情痞子 提交于 2019-12-27 11:37:13
问题 In a C++ project I'm working on, I have a flag kind of value which can have four values. Those four flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each record I wish to keep this attribute, so I could use an enum: enum { xNew, xDeleted, xModified, xExisting } However, in other places in code, I need to select which records are to be visible to the user, so I'd like to be able to pass that as a single

Should I use #define, enum or const?

左心房为你撑大大i 提交于 2019-12-27 11:35:22
问题 In a C++ project I'm working on, I have a flag kind of value which can have four values. Those four flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each record I wish to keep this attribute, so I could use an enum: enum { xNew, xDeleted, xModified, xExisting } However, in other places in code, I need to select which records are to be visible to the user, so I'd like to be able to pass that as a single

How are getchar() and putchar() Macros?

丶灬走出姿态 提交于 2019-12-26 09:47:33
问题 From what I understand about macros in C, they are predefined constants that will be used throughout the program with their constant value, so we go ahead and define them to avoid further complications and make the code more readable, so people reading it will understand what is supposed to stay constant and what isn't. I have read here and there (C programming A Modern Approach, K.N King) that we can define these two functions as macro. Since I'm somewhat new to C, I can't wrap my head

Include header files optionally in C++

对着背影说爱祢 提交于 2019-12-25 16:09:25
问题 I have a C++ code which needs to include a certain library in some servers and not in other servers. I build my code using bjam. Code example: if server in server_list: include <header-file.h> int function(); else: int function(); And during build using bjam: if server in server_list: -llibrary else: ... 回答1: Header file inclusion is a compile time activity not run time. So you can't use if conditions for the same use #ifdefs #define SERVER_IN_LIST #ifdef SERVER_IN_LIST #include<...> #endif

Include header files optionally in C++

有些话、适合烂在心里 提交于 2019-12-25 16:08:40
问题 I have a C++ code which needs to include a certain library in some servers and not in other servers. I build my code using bjam. Code example: if server in server_list: include <header-file.h> int function(); else: int function(); And during build using bjam: if server in server_list: -llibrary else: ... 回答1: Header file inclusion is a compile time activity not run time. So you can't use if conditions for the same use #ifdefs #define SERVER_IN_LIST #ifdef SERVER_IN_LIST #include<...> #endif

Using X-lists and preprocessor directives to generate configurable C Code At compile time

我只是一个虾纸丫 提交于 2019-12-25 08:47:41
问题 I have a codebase already containing repetitive code, with only minor differences, serializable ID-s, indexes, variable arrays. The codebase is huge, and some components are being activated/deactivated based on simple preprocessor directives and constants(e.g.: #define CFG_PROJECT cfgAutobot , #define CFG_PROJECT cfgUltron , ..etc). The functionality is effectively the same, but with varying components and conditionals. Example: int somedata; int somecounter; void main_loop(){ #if(CFG_PROJECT

Using two headers with the same function names

此生再无相见时 提交于 2019-12-25 07:26:19
问题 I want to use some of the functionality of <termios.h and <asm/termios.h> , but they contain functions with the same name, and only including them both I got error: redefinition of 'struct termio' . I managed to use some of <asm/termios.h structures ( struct termios2 ) by isolating the #include directive exactly where I need (in a function). void MyClass::MyMethod() { #include <asm/termios.h> struct termios2 tio; // do stuff with tio variable. } This works fine for only one function, but if I

When I want for define to set 1000000, program in process crashed [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-25 06:48:23
问题 This question already has answers here : Segmentation fault on large array sizes (5 answers) Closed 5 years ago . When I want to #define for SIZE 1.000.000 , my program crashed before it start main function, but when i #define for SIZE 100.000 it work. I have two arrays initialization in my program. #define SIZE 1000000 char *baza_vocka[SIZE]; char b_vocka[SIZE]; EDIT : They are local variables. 回答1: In case of 1M you're trying to allocate an array on the stack, which is bigger than the stack