Stringizing operator in C not working on linux -
i have following code works expected in windows on ubuntu error "tostring1 not declared in scope".
#include <stdio.h> #define 2 #define b 3 #define c 4 #define d 5 #define tostring1(s) #s #define tostring(s) tostring1(s) #define numbers a,b,c,d #define numberss tostring(numbers) int main() { int a1 = 0, a2 = 0, a3 = 0, a4 = 0; if (sscanf_s(numberss, "%d,%d,%d,%d", &a1, &a2, &a3, &a4) == 4) { printf("the numbers assigned correctly"); } printf("%d %d %d %d ", a1, a2, a3, a4); }
what reason that? if remove tostring1 , make
#define tostring(s) #s
the result 0 every variable on both windows , ubuntu.
can have explanation this?
the compiler says:
error: macro "tostring1" passed 4 arguments, takes 1 sscanf(numberss, "%d,%d,%d,%d",a1,a2,a3,a4);
indeed, numberss
tostring(numbers)
, numbers is a,b,c,d
so need variable macro magic here work correctly:
#define 2 #define b 3 #define c 4 #define d 5 #define tostring1(s...) #s #define tostring(s...) tostring1(s, __va_args__) #define numbers a,b,c,d #define numberss tostring(numbers) int main() { int a1,a2,a3,a4; sscanf(numberss, "%d,%d,%d,%d",&a1,&a2,&a3,&a4); printf("%d %d %d %d", a1,a2,a3,a4); }
online: https://ideone.com/pupd6x
(also, please note have fixed sscanf take addresses)
Comments
Post a Comment