If you consider the following PHP+C polyglot snippet, there is a problem regarding class property access in the printf call:

#include <stdio.h>
#include <stdlib.h>
#define class struct
#define function 

class Point {
#define public int
    public $x;
    public $y;

function main()
    struct Point*
    $p = alloca(sizeof(struct Point));
    printf("%d", $p->x);  // PHP expects $p->x, but C expects $p->$x
    return 0;

My question is, how to resolve this in a neat manner? Possible solutions:

  1. Remove all $ via sed before compiling with gcc
  2. Define public $x as int x using #if __PHP__
  3. Edit: Use getters, maybe? (works in C by passing self as first argument with function pointers)

Trying to define dollar sign as empty space does not work:

#define $ 

Any other ideas? I am also considering using m4 in the C Makefile. Thank you. :)

  • 2
    \$\begingroup\$ Welcome to Code Golf! Glad to see a non-code-golf tips question, there's very few of them. \$\endgroup\$ Aug 31, 2022 at 13:08
  • 1
    \$\begingroup\$ @RadvylfPrograms I'm not sure this one is allowed though, as it lacks an objective winning criterion to work towards (other than "make it work") \$\endgroup\$
    – pxeger
    Aug 31, 2022 at 13:11
  • 1
    \$\begingroup\$ Can you throw in a #define x $x, then use $p->x? \$\endgroup\$
    – user
    Aug 31, 2022 at 13:11
  • 1
    \$\begingroup\$ @user Heck yes, that works! Thanks! Feel free to post it as an answer. \$\endgroup\$ Aug 31, 2022 at 13:18
  • 1
    \$\begingroup\$ Currently, I think this is borderline off-topic. While [polyglot]s are a part of this site, the main thing every question here needs to have is a winning criteria: what part of this polyglot are you attempting to optimise? Without that, this is more on-topic on SO than here \$\endgroup\$ Aug 31, 2022 at 13:48

1 Answer 1


You can add #define x $x with the other #defines at the top. Now $p->x should also work in C.

  • 1
    \$\begingroup\$ Btw, this also works for properties with similar names, like $x and $xx, without causing collisions. \$\endgroup\$ Aug 31, 2022 at 13:28
  • \$\begingroup\$ OK, so there's one problem - if someone defines a function called x at the same time... :d Have to think about that one. \$\endgroup\$ Aug 31, 2022 at 13:38
  • \$\begingroup\$ Simple solution: Just namespace the variables with __object_prop_x or sim to avoid collision. \$\endgroup\$ Aug 31, 2022 at 17:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.