0
// it return "192.168.123.num"
#define GET_SLAVE(num) "192.168.123."#num

however, I want to get string with increased number.

e.g.) GET_SLAVE(2) --> "192.168.123.4"

how can i make it? or is it not possible in macro function?

Hong
  • 1
  • 3
  • 2
    Are you writing C or C++? Please don't tag both. The answer in C++ is likely to involve `std::string`, an overloaded `+` operator, and a recommendation to take a look at `constexpr`, none of which exist in C. – Silvio Mayolo Aug 23 '21 at 02:43
  • @SilvioMayolo Thank you for your answer, i edit my question. – Hong Aug 23 '21 at 03:02
  • 1
    By the way, could you elaborate on how the adding should work? wll it always be add by 2? – mediocrevegetable1 Aug 23 '21 at 03:33
  • in my case, always it is elevated by 2. of course, it can achieve this feature with normal function. however, i want to know if this is possible. and if possible, it will make my code to be simple. – Hong Aug 23 '21 at 03:46
  • Notable: [Can I add numbers with the C/C++ preprocessor?](https://stackoverflow.com/questions/3539549/can-i-add-numbers-with-the-c-c-preprocessor) and [Can the C preprocessor perform integer arithmetic?](https://stackoverflow.com/questions/1560357/can-the-c-preprocessor-perform-integer-arithmetic) – Oka Aug 23 '21 at 04:11
  • No, it will not make anything simpler - it will indeed make things more awful instead. Also, your executable will now contain all the hardcoded addresses and should you want to change anything, you need to recompile the entire program. – Antti Haapala -- Слава Україні Aug 23 '21 at 05:59
  • Does this answer your question? [Macro increase value and then concatenate](https://stackoverflow.com/questions/11717508/macro-increase-value-and-then-concatenate) – Krishna Kanth Yenumula Aug 23 '21 at 07:37

1 Answers1

2

There is some macro magic you can do to make this possible, but it doesn't look the best:

#define GET_SLAVE(num) "192.168.123."ADD2_##num##_

#define ADD2_0_ "2"
#define ADD2_1_ "3"
#define ADD2_2_ "4"
#define ADD2_3_ "5"
#define ADD2_4_ "6"
#define ADD2_5_ "7"
#define ADD2_6_ "8"
#define ADD2_7_ "9"
#define ADD2_8_ "10"
#define ADD2_9_ "11"
#define ADD2_10_ "12"
#define ADD2_11_ "13"
#define ADD2_12_ "14"
#define ADD2_13_ "15"
#define ADD2_14_ "16"
#define ADD2_15_ "17"
#define ADD2_16_ "18"
#define ADD2_17_ "19"
#define ADD2_18_ "20"
#define ADD2_19_ "21"
#define ADD2_20_ "22"
#define ADD2_21_ "23"
#define ADD2_22_ "24"
#define ADD2_23_ "25"
#define ADD2_24_ "26"
#define ADD2_25_ "27"
#define ADD2_26_ "28"
#define ADD2_27_ "29"
#define ADD2_28_ "30"
#define ADD2_29_ "31"
#define ADD2_30_ "32"
#define ADD2_31_ "33"
#define ADD2_32_ "34"
#define ADD2_33_ "35"
#define ADD2_34_ "36"
#define ADD2_35_ "37"
#define ADD2_36_ "38"
#define ADD2_37_ "39"
#define ADD2_38_ "40"
#define ADD2_39_ "41"
#define ADD2_40_ "42"
#define ADD2_41_ "43"
#define ADD2_42_ "44"
#define ADD2_43_ "45"
#define ADD2_44_ "46"
#define ADD2_45_ "47"
#define ADD2_46_ "48"
#define ADD2_47_ "49"
#define ADD2_48_ "50"
#define ADD2_49_ "51"
#define ADD2_50_ "52"
#define ADD2_51_ "53"
#define ADD2_52_ "54"
#define ADD2_53_ "55"
#define ADD2_54_ "56"
#define ADD2_55_ "57"
#define ADD2_56_ "58"
#define ADD2_57_ "59"
#define ADD2_58_ "60"
#define ADD2_59_ "61"
#define ADD2_60_ "62"
#define ADD2_61_ "63"
#define ADD2_62_ "64"
#define ADD2_63_ "65"
#define ADD2_64_ "66"
#define ADD2_65_ "67"
#define ADD2_66_ "68"
#define ADD2_67_ "69"
#define ADD2_68_ "70"
#define ADD2_69_ "71"
#define ADD2_70_ "72"
#define ADD2_71_ "73"
#define ADD2_72_ "74"
#define ADD2_73_ "75"
#define ADD2_74_ "76"
#define ADD2_75_ "77"
#define ADD2_76_ "78"
#define ADD2_77_ "79"
#define ADD2_78_ "80"
#define ADD2_79_ "81"
#define ADD2_80_ "82"
#define ADD2_81_ "83"
#define ADD2_82_ "84"
#define ADD2_83_ "85"
#define ADD2_84_ "86"
#define ADD2_85_ "87"
#define ADD2_86_ "88"
#define ADD2_87_ "89"
#define ADD2_88_ "90"
#define ADD2_89_ "91"
#define ADD2_90_ "92"
#define ADD2_91_ "93"
#define ADD2_92_ "94"
#define ADD2_93_ "95"
#define ADD2_94_ "96"
#define ADD2_95_ "97"
#define ADD2_96_ "98"
#define ADD2_97_ "99"
#define ADD2_98_ "100"
#define ADD2_99_ "101"

We create 100 macros that work for any number 0-99 (you can make more if you need to) which expand to the added-by-2 number as a string. In our GET_SLAVE macro we use num to choose which ADD2_X_ macro to use through ADD2_##num##_. Now you can do

puts(GET_SLAVE(2));
// Output: 192.168.123.4
mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33