Post History
C (compliant), 197 198 bytes. Golfed version of the function in the question, using X macros: #define L Y(1000,M)Y(900,CM)Y(500,D)Y(400,CD)Y(100,C)Y(90,XC)Y(50,L)Y(40,XL)Y(10,X)Y(9,IX)Y(5,V)Y(4,I...
Answer
#2: Post edited
# C (compliant), 198 bytes.- Golfed version of the function in the question, using X macros:
- ```c
#define L Y(1000,M)Y(900,CM)Y(500,D)Y(400,CD)Y(100,C)Y(90,XC)Y(50,L)Y(40,XL)Y(10,X)Y(9,IX)Y(5,V)Y(4,IV)Y(1,I)- #define Y(n,R)for(;v>=n;v-=n)strcat(r,#R);
char r[99];char*f(int v){L return r;}- ```
- This function abuses static initialization so it can only be called once. By adding `*r=0;`, we can call it multiple times during testing, which is why the code in the TIO link below looks slightly longer.
<a href="https://tio.run/##XZKxbsMgEIZ3PwVSFohwg6U0skWdhSxIzpLBIqo6WNhOkFIcYcdLlFevC04raFnu1/cd3A3I@CTltFBaXm51A976oVbdy3kbhcgofbJsWtRNq3QDCnCECSEE79ERZrYyF15t2Nm6dsCFxIW5Aws2N@Bi9lgUs8bCWcxdecWlc5i7kmCOwM@JfqceocYH1HYG0nGbazrGuUZ2OVkN0ODFAdFInisDzHuWfVAXly1UegAjui9NTmgBTDPcjAaGPiYnPiulARw7VaPoHgFwvQ09bGGCEHWTVyvAPU09LTkPRBa0i@AV8uQWB3S99rTgpRebzWY2VuxYIcpwMiFeMRaINA1FIYT4t5q/yfYBzjKPBfu79fOKVfvoMX3J9lKd@im2/yKXSTrF16au9KBk3BjTmf4b" title="C (gcc) – Try It Online">Try it online!</a>- ---
- It can be minimized a bit further with non-compliant gcc tricks.
- # C (compliant), 197 <strike>198</strike> bytes.
- Golfed version of the function in the question, using X macros:
- ```c
- #define L Y(1000,M)Y(900,CM)Y(500,D)Y(400,CD)Y(100,C)Y(90,XC)Y(50,L)Y(40,XL)Y(10,X)Y(9,IX)Y(5,V)Y(4,IV)Y(1,I)return r;
- #define Y(n,R)for(;v>=n;v-=n)strcat(r,#R);
- char r[99];char*f(int v){L}
- ```
- This function abuses static initialization so it can only be called once. By adding `*r=0;`, we can call it multiple times during testing, which is why the code in the TIO link below looks slightly longer.
- <a href="https://tio.run/##XZJBb4MgFMfvfgqSXqDBFZOu0Th7oRcSeunB0Cw7GNSWpMMGrZemX30O7BbYuLy/v9/D9w7I@CTltFBaXm51A976oVbdy3kbhcgofbJsWtRNq3QDODjChBCC9@gIM1upC6827GxdO@BC4sLcgQWdGzCfPRZ81lg4i5krr7h0DjNXEsyQaYab0cDk4OdEv@OPUOMDajsD83Fb6HyMC43slrIaoMGLA8ojea4MMO9Z9pG7uGyh0gMY0X1pCpLzx@Q@PyulARw7VaPoHgFwvQ09bGGC0DxztQLM09TTkrFAZEG7CP5CntzigK7XnnJWerHZbGZjxY5yUYaTCfGK0kCkaSi4EOLfav4m3Qc4yzwW9O/WzytW7aPH9CXbS3Xqp9g@i0Im6RRfm7rSg5JxY0xn@m8" title="C (gcc) – Try It Online">Try it online!</a>
- ---
- It can be minimized a bit further with non-compliant gcc tricks.
#1: Initial revision
# C (compliant), 198 bytes. Golfed version of the function in the question, using X macros: ```c #define L Y(1000,M)Y(900,CM)Y(500,D)Y(400,CD)Y(100,C)Y(90,XC)Y(50,L)Y(40,XL)Y(10,X)Y(9,IX)Y(5,V)Y(4,IV)Y(1,I) #define Y(n,R)for(;v>=n;v-=n)strcat(r,#R); char r[99];char*f(int v){L return r;} ``` This function abuses static initialization so it can only be called once. By adding `*r=0;`, we can call it multiple times during testing, which is why the code in the TIO link below looks slightly longer. <a href="https://tio.run/##XZKxbsMgEIZ3PwVSFohwg6U0skWdhSxIzpLBIqo6WNhOkFIcYcdLlFevC04raFnu1/cd3A3I@CTltFBaXm51A976oVbdy3kbhcgofbJsWtRNq3QDCnCECSEE79ERZrYyF15t2Nm6dsCFxIW5Aws2N@Bi9lgUs8bCWcxdecWlc5i7kmCOwM@JfqceocYH1HYG0nGbazrGuUZ2OVkN0ODFAdFInisDzHuWfVAXly1UegAjui9NTmgBTDPcjAaGPiYnPiulARw7VaPoHgFwvQ09bGGCEHWTVyvAPU09LTkPRBa0i@AV8uQWB3S99rTgpRebzWY2VuxYIcpwMiFeMRaINA1FIYT4t5q/yfYBzjKPBfu79fOKVfvoMX3J9lKd@im2/yKXSTrF16au9KBk3BjTmf4b" title="C (gcc) – Try It Online">Try it online!</a> --- It can be minimized a bit further with non-compliant gcc tricks.