c-preprocessor

C/C++ macro string concatenation

强颜欢笑 提交于 2019-12-16 22:08:16
问题 #define STR1 "s" #define STR2 "1" #define STR3 STR1 ## STR2 Is it possible to concatenate have STR3 == "s1"? You can do this by passing args to another Macro function. But is there a direct way? 回答1: If they're both strings you can just do: #define STR3 STR1 STR2 The preprocessor automatically concatenates adjacent strings. EDIT: As noted below, it's not the preprocessor but the compiler that does the concatenation. 回答2: You don't need that sort of solution for string literals, since they are

Stringification of a macro value

一笑奈何 提交于 2019-12-16 21:00:30
问题 I faced a problem - I need to use a macro value both as string and as integer. #define RECORDS_PER_PAGE 10 /*... */ #define REQUEST_RECORDS \ "SELECT Fields FROM Table WHERE Conditions" \ " OFFSET %d * " #RECORDS_PER_PAGE \ " LIMIT " #RECORDS_PER_PAGE ";" char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN]; /* ...and some more uses of RECORDS_PER_PAGE, elsewhere... */ This fails with a message about "stray #", and even if it worked, I guess I'd get the macro names stringified, not the values

Real-world use of X-Macros

ⅰ亾dé卋堺 提交于 2019-12-16 20:16:31
问题 I just learned of X-Macros. What real-world uses of X-Macros have you seen? When are they the right tool for the job? 回答1: I discovered X-macros a couple of years ago when I started making use of function pointers in my code. I am an embedded programmer and I use state machines frequently. Often I would write code like this: /* declare an enumeration of state codes */ enum{ STATE0, STATE1, STATE2, ... , STATEX, NUM_STATES}; /* declare a table of function pointers */ p_func_t jumptable[NUM

What are the applications of the ## preprocessor operator and gotchas to consider?

限于喜欢 提交于 2019-12-16 20:03:28
问题 As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ## , the parameter is replaced by the actual argument, the ## and

What are the applications of the ## preprocessor operator and gotchas to consider?

烂漫一生 提交于 2019-12-16 20:01:14
问题 As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ## , the parameter is replaced by the actual argument, the ## and

C++ preprocessor stringification that preserves newlines?

无人久伴 提交于 2019-12-14 04:18:25
问题 I need to record (for auditing/logging purposes) the code of lambda functions that get passed around in my code. Of course, the lambda object also needs to be saved. So I came up with a macro solution as follows: #define LAMBDA_AND_STRING(lambda) lambda, #lambda using namespace std; int main(int argc, const char * argv[]) { auto p = pair<function<void()>, string> ( LAMBDA_AND_STRING( [] { cout << "Hello world!" << endl; cout << "Hello again!"; } ) ); cout << "CODE:" << endl << p.second <<

While using #ifndef, .h file being added multiple times

柔情痞子 提交于 2019-12-14 04:05:36
问题 I am trying to use following pattern. #ifndef TRACER_H #include "Tracer.h" #endif This is statement is added to each file in the code such that tracer.h is added only once. Still I am getting an error saying multiple objects. Also Tracer.h contains #ifndef TRACER_H #define TRACER_H Here is the error; i tried pragma once as well: 1>Generating Code... 1>Linking... 1>LINK : \\stu05-fsrv.ad.syr.edu\akbhat$\Visual Studio 2008\Projects\Project3\Debug\Project3.exe not found or not built by the last

Multi-pass C preprocessor [closed]

走远了吗. 提交于 2019-12-14 01:25:12
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 months ago . Is it remotely sane to apply the C preprocessor to the same codebase multiple times (specifically, twice in sequence?) For instance, having declarations such as the following: ##define DECLARE(FILE) # define DECLARATIONS \ # include FILE \ # undef DECLARATIONS Have you ever

C preprocessor #if expression

给你一囗甜甜゛ 提交于 2019-12-14 00:15:34
问题 I am a bit confused on the type of expression we can use with the #IF preprocessor in the C language. I tried the following code, and it isn't working. Please explain and provide examples for expressions that can be used with the preprocessor. #include<stdio.h> #include<conio.h> #include<stdlib.h> int c=1; #if c==1 #define check(a) (a==1)?a:5 #define TABLE_SIZE 100 #endif int main() { int a = 0, b; printf("a = %d\n", a); b = check(a); printf("a = %d %d\n", a, TABLE_SIZE); system("PAUSE");

Declaring variables inside a struct in c [duplicate]

不羁岁月 提交于 2019-12-14 00:09:54
问题 This question already has answers here : Why can't we initialize members inside a structure? (6 answers) Closed 3 years ago . I wanted to make an object oriented preprocessor to my programming language which converts my language into C (like early C++). And I want to simulate the classes with structures. And the question is: how can I declare a variable inside a struct like this: typedef struct { //equivalent of class int a = 5; int (*sub)(int) = &int_sub; //function of a class, uses an