quine program in C using only macros

11

Asked on Stack Overflow, but they suggested to move it here

A while ago I was reading about quines. It made me wonder if it possible to write a macro in C which will print itself, without writing any function such as main. every piece of code should be only in macro. After a few attempts, I couldn't find the solution.

While searching the web for answer, I found the following code:

#define Q(x) #x;x
char *q=Q(main(){printf("#define Q(x) #x;x\nchar *q=Q(");printf(q);printf(")\n");})

Although part of the code isn't in the #define (isn't macro). Is it possible to implement a macro which will print the program ( quine type program )?

EDIT: if it isn't possible (because the macros are 'made' in the preprocessor step), is it possible to implement a quine which is mostly in macros?

kickstart

Posted 2018-05-17T13:38:56.160

Reputation: 211

Question was closed 2018-05-18T06:29:18.057

5

@Picard This is on topic, and it's not a duplicate. It is similar in style to this question.

– mbomb007 – 2018-05-17T13:42:11.747

@mbomb007 This one ask "how to do X", not "how to do X while minimize criteria Y". There are no winning criteria. – user202729 – 2018-05-17T13:53:47.960

You would need to define what exactly do you want. A "macro" can't "print". (before that I would vote to close as unclear) – user202729 – 2018-05-17T13:54:52.773

1There doesn't have to be a winning criteria. It's a tips question. Asking if something is possible (something that is a challenge to solve) should be on topic. – mbomb007 – 2018-05-17T14:00:14.827

@mbomb007 From the [tips] tag info page: "For questions asking for tips on a specific piece of code, to make it a better answer to a programming challenge that is on-topic on this site." "on-topic on this site" implies have a winning criteria. – user202729 – 2018-05-17T14:03:27.983

4

@mbomb007 How did you come to that conclusion? Of all the answers to Let's decide what kind of non-challenge questions we want once and for all, only Requests for lists of language-specific golfing advice and Questions about improving code golf / code challenge skills have a positive score.

– Dennis – 2018-05-17T14:04:13.423

1It's basically a "let's solve this as a group" question. If seeing it solved would be a positive experience, then what's the problem? I don't think we need to be so legalistic – mbomb007 – 2018-05-17T14:22:48.990

1If this question were to fit into one of the meta categories in the linked question, the one it fits best into is Questions about improving code golf / code challenge skills. – mbomb007 – 2018-05-17T14:29:28.527

14Since no one else has yet said it, welcome to PPCG! You seem to have stirred up the hornet's nest with your first post (which isn't necessarily a bad thing!), so hopefully you'll stick around for the next one! – AdmBorkBork – 2018-05-17T14:37:58.933

If this post also said "I want to do this in shortest code possible" would people say it was on topic? That would make it fit into the "questions about improving code golf / code challenge skills" category. – dylnan – 2018-05-17T16:31:08.777

Just for clarity, since I don't really know C, is a macro basically a function? Someone could define a macro that returns a string representing itself, right? It doesn't have to output to stdout to be a quine? – mbomb007 – 2018-05-17T16:57:03.573

1@mbomb007 It's sort of a function that's computed at compile-time. So usually the RHS of a macro replaces the LHS wherever it appears in the rest of the program. – musicman523 – 2018-05-17T17:01:51.683

Right, but I'm asking if a macro that returns itself would count as a quine. – mbomb007 – 2018-05-17T20:33:05.203

2define "mostly in macros" – ASCII-only – 2018-05-18T01:02:42.413

@dylnan Yes. (15 character limit) – user202729 – 2018-05-18T03:00:15.610

3@mbomb007 Ignoring whether it's on-topic, it's definitely unclear. Macros can't print. The question needs more clarification from the OP before it can be reopened. – user202729 – 2018-05-18T03:19:45.747

No answers