code_junkie

Convert a preprocessor token to a string

Nov 11th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #define MAX_LEN 16
  2.  
  3. char val[MAX_LEN+1]; // room for
  4. sscanf(buf, "%"MAX_LEN"s", val);
  5.  
  6. #define STRINGIFY(x) #x
  7. #define TOSTRING(x) STRINGIFY(x)
  8. #define AT __FILE__ ":" TOSTRING(__LINE__)
  9.  
  10. #define VERSION_MAJOR 4
  11. #define VERSION_MINOR 47
  12.  
  13. #define VERSION_STRING "v" #VERSION_MAJOR "." #VERSION_MINOR
  14.  
  15. #define VERSION_MAJOR 4
  16. #define VERSION_MINOR 47
  17.  
  18. #define STRINGIZE2(s) #s
  19. #define STRINGIZE(s) STRINGIZE2(s)
  20. #define VERSION_STRING "v" STRINGIZE(VERSION_MAJOR)
  21. "." STRINGIZE(VERSION_MINOR)
  22.  
  23. #include <stdio.h>
  24. int main() {
  25. printf ("%sn", VERSION_STRING);
  26. return 0;
  27. }
  28.  
  29. sscanf(buf, "%" #MAX_LEN "s", val);
  30.  
  31. #define STR1(x) #x
  32. #define STR(x) STR1(x)
  33. sscanf(buf, "%" STR(MAX_LEN) "s", val);
  34.  
  35. #define MAX_LEN 16
  36. #define MAX_LEN_S "16"
  37.  
  38. char val[MAX_LEN+1];
  39. sscanf(buf, "%"MAX_LEN_S"s", val);
  40.  
  41. strncpy(val, buf, MAX_LEN);
  42. val[MAX_LEN] = '';
  43.  
  44. sprintf(buf, "%*s", MAX_LEN, val);
Add Comment
Please, Sign In to add comment