c-preprocessor

Can we write a macro in many lines without the backslash at the end?

血红的双手。 提交于 2019-12-25 04:57:26
问题 I saw some examples in CPP manual where we can write macros body in many lines without the backslash. #define strange(file) fprintf (file, "%s %d", ... strange(stderr) p, 35) output: fprintf (stderr, "%s %d", p, 35) Are they special cases like directives inside arguments macros or is it allowed only for #define ? For include directives It must be always declared on one line if I am not wrong. Edit: From https://gcc.gnu.org/onlinedocs/cpp/Directives-Within-Macro-Arguments.html 3.9 Directives

Undefine a function-like macro in C?

时光怂恿深爱的人放手 提交于 2019-12-25 02:50:14
问题 I am trying to do some hacks over the glibc, and I wanted to know whether it's possible to redefine function-like macros ? For example, <tgmath.h> has the following macro: #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1) How to redefine expm1 as : #define expm1(Val) __TGMATH_UNARY_REAL_IMAG (Val, expm1, cexpm1) I suppose that I have to cancel the previous definition but I do not know exactly how to do that. 回答1: Exactly. Just undefine it first. #ifdef expm1 #undef expm1 #endif #define

Stringification working with USERNAME:PASSWORD but not for SERIAL:TOKEN?

醉酒当歌 提交于 2019-12-25 02:31:06
问题 I have the following Makefile (If you are asking me why there's \" included you can refer to my previous question) BOARD_TAG = mega2560 CPPFLAGS = -DUSERNAME=\"$(USERNAME)\" -DPASSWORD=\"$(PASSWORD)\" include $(ARDMK_DIR)/Arduino.mk and code: void setup() { Serial.begin(9600); String auth_raw2(USERNAME ":" PASSWORD); Serial.println(auth_raw2); } void loop() {} when I compile this with make USERNAME=hello PASSWORD=world , everything works and I see 'hello:world' being printed out. However, if

How do most embedded C compilers define symbols for memory mapped I/O?

萝らか妹 提交于 2019-12-25 01:38:13
问题 I often times write to memory mapped I/O pins like this P3OUT |= BIT1; I assumed that P3OUT was being replaced with something like this by my preprocessor: *((unsigned short *) 0x0222u) But I dug into an H file today and saw something along these lines: volatile unsigned short P3OUT @ 0x0222u; There's some more expansion going on before that, but it is generally that. A symbol '@' is being used. Above that there are some #pragma's about using an extended set of the C language. I am assuming

Weird macro definition issue

与世无争的帅哥 提交于 2019-12-24 23:33:03
问题 I want to define a macro at compile time based on the value of another macro. However this code is not executing as expected: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIXTEEN 16 #define TWO (SIXTEEN % 8 == 0)? (SIXTEEN / 8) : ((SIXTEEN / 8) + 1) int main(); int main() { printf("max = %d\n", TWO); int i; for (i = 0; i < TWO; i++) { printf("%d\n", i); } return 0; } This prints: max = 2 0 1 2 ... and continues until terminated, When it should be printing simply: max = 2

Is the C preprocessor able to process strings char by char?

让人想犯罪 __ 提交于 2019-12-24 15:27:00
问题 I'd like to obscure strings at compile time. I know it can be done in other preprocessors but I haven't found a way to do this with the C preprocessor. 回答1: Well, you can do it, but it's ugly. #define ENCODE_STRING_14(str) {\ str[0] ^ 0x020,\ str[1] ^ 0x020,\ str[2] ^ 0x020,\ str[3] ^ 0x020,\ str[4] ^ 0x020,\ str[5] ^ 0x020,\ str[6] ^ 0x020,\ str[7] ^ 0x020,\ str[8] ^ 0x020,\ str[9] ^ 0x020,\ str[10] ^ 0x020,\ str[11] ^ 0x020,\ str[12] ^ 0x020,\ '\0'\ } void Decode( char *str, int length ) {

Preprocessor for_each witin SWIG interface

爷,独闯天下 提交于 2019-12-24 14:22:17
问题 I've been using the REFLECTABLE macro from this answer in my C++ header file, which looks like: #ifndef TIMER_H #define TIMER_H // From the linked question, but (deliberately) ignored by SWIG here, // only relevant is as much as it defines REFLECTABLE for other preprocessors #include "reflection.hh" struct Timer { REFLECTABLE ( (float) startTime, (float) endTime, ) }; #endif The SWIG preprocessor doesn't follow #include and doesn't handle the definition of REFLECTABLE from the linked question

Macro SWAP(t,x,y) exchanging two arguments of type t

不问归期 提交于 2019-12-24 13:43:19
问题 So I am basically trying to make a SWAP(t,x,y) macro that exchanges two arguments of type t. I am trying to think of going around the problem when these two arguments are of the form v[i++] and w[f(x)] , i.e. SWAP(int, v[i++], w[f(x)]). The code below is basically crashing ... #define SWAP(T,x,y) {T *p = x; T *q = y; T z = *p; *p = *q; *q = z;} int f (int x){ return (0-x); } int main(void) { int v[] = {1,2,3}; int i = 0; int w[] = {4,5,6}; int x = -1; int *p = v; int *q = w; SWAP(int*, v[i++]

C macro issue: redefinition of functions / structure

巧了我就是萌 提交于 2019-12-24 12:30:04
问题 Given the following code (it's a macro that generates code for a list data structure, based on the contained type). list.h #ifndef _LIST_H #define _LIST_H #ifdef __cplusplus extern "C" { #endif #define LIST_TEMPLATE_INIT(type) \ typedef struct __list_s_##type { \ struct __list_s_##type *next; \ type value; \ } __list_##type; \ \ __list_##type * __list_##type##_malloc(type value){ \ __list_##type * list = NULL; \ list = malloc(sizeof(*list)); \ list->value = value; \ return list; \ }\ \ void _

Standards for the C and/or C++ preprocessors? [closed]

梦想与她 提交于 2019-12-24 12:08:48
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . What standards document(s) specify the behavior of the C and/or C++ pre-processors? Wikipedia suggests http://www.open-std.org/JTC1/SC22/WG14/www/standards is valid for C99. Is it? What about C++ flavours? 回答1: The C language standard (ISO/IEC 9899) specifies how the preprocessor behaves in C. The