better explanation of quine with macros
This commit is contained in:
120
notes.md
120
notes.md
@@ -200,12 +200,7 @@ int main()
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**in one line :**
|
**with macros :**
|
||||||
```
|
|
||||||
int main(){ char *quine = THIS_LINE_&_THE_LINES_AROUND; printf(quine); return 0; }
|
|
||||||
```
|
|
||||||
|
|
||||||
**whole code idea :**
|
|
||||||
```
|
```
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define MAIN() int main() { char *quine = "BEFORE" "THIS_MACRO_ITSELF" "AFTER"; printf(quine); return 0; }
|
#define MAIN() int main() { char *quine = "BEFORE" "THIS_MACRO_ITSELF" "AFTER"; printf(quine); return 0; }
|
||||||
@@ -219,21 +214,28 @@ MAIN()
|
|||||||
|
|
||||||
```
|
```
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define MAIN(s) int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) "s"\n\nMAIN(str(MAIN(s)))\n"; printf(quine); return 0; }
|
#define MAIN(s) \
|
||||||
|
int main() \
|
||||||
|
{ \
|
||||||
|
char *quine = "#include <stdio.h>\n#define MAIN(s) " \
|
||||||
|
s \
|
||||||
|
"\n\nMAIN(s)\n"; \
|
||||||
|
printf(quine); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
MAIN(s)
|
MAIN(s)
|
||||||
```
|
```
|
||||||
|
|
||||||
**expansions :**
|
**expands to :**
|
||||||
-> `int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) "s "\n\nMAIN(MAIN(s))\n"; printf(quine); return 0; }`
|
|
||||||
|
|
||||||
**exploded view :**
|
|
||||||
```
|
```
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char *quine = "#include <stdio.h>\n#define MAIN(s) "s "\n\nMAIN(MAIN(s))";
|
char *quine = "#include <stdio.h>\n#define MAIN(s) "
|
||||||
printf(quine);
|
s
|
||||||
return 0;
|
"\n\nMAIN(s)\n";
|
||||||
|
printf(quine);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -245,21 +247,34 @@ int main()
|
|||||||
|
|
||||||
```
|
```
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define MAIN(s) int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) "s"\n\nMAIN(MAIN(s))\n"; printf(quine); return 0; }
|
#define MAIN(s) \
|
||||||
|
int main() \
|
||||||
|
{ \
|
||||||
|
char *quine = "#include <stdio.h>\n#define MAIN(s) " \
|
||||||
|
s \
|
||||||
|
"\n\nMAIN(MAIN(s))\n"; \
|
||||||
|
printf(quine); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
MAIN(MAIN(s))
|
MAIN(MAIN(s))
|
||||||
```
|
```
|
||||||
|
|
||||||
**expansions :**
|
**expands to :**
|
||||||
-> `int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) "int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) "s "\n\nMAIN(MAIN(s))"; printf(quine); return 0; }"\n\nMAIN(MAIN(s))\n"; printf(quine); return 0; }`
|
|
||||||
|
|
||||||
**exploded view :**
|
|
||||||
```
|
```
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char *quine = "#include <stdio.h>\n#define MAIN(s) "int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) "s "\n\nMAIN(MAIN(s))"; printf(quine); return 0; }"\n\nMAIN(MAIN(s))\n";
|
char *quine = "#include <stdio.h>\n#define MAIN(s) " int main()
|
||||||
printf(quine);
|
{
|
||||||
return 0;
|
char *quine = "#include <stdio.h>\n#define MAIN(s) "
|
||||||
|
s
|
||||||
|
"\n\nMAIN(MAIN(s))\n";
|
||||||
|
printf(quine);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
"\n\nMAIN(MAIN(s))\n";
|
||||||
|
printf(quine);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -276,32 +291,45 @@ int main()
|
|||||||
#define MAIN(s) content s content
|
#define MAIN(s) content s content
|
||||||
MAIN(str(MAIN(s)))
|
MAIN(str(MAIN(s)))
|
||||||
|
|
||||||
/* EQUIVALENT */
|
/* EQUIVALENT TO : */
|
||||||
|
|
||||||
#define MAIN(s) content s content
|
#define MAIN(s) content s content
|
||||||
MAIN((MAIN(s)))
|
MAIN((MAIN(s)))
|
||||||
```
|
```
|
||||||
|
|
||||||
**we can use this extra step to stringify the content :**
|
**we can use this extra step to stringify the content with #s :**
|
||||||
|
```
|
||||||
|
#define str(s) #s
|
||||||
|
#define MAIN(s) content s content
|
||||||
|
MAIN(str(MAIN(s)))
|
||||||
|
```
|
||||||
|
|
||||||
|
**example :**
|
||||||
```
|
```
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define str(s) #s
|
#define str(s) #s
|
||||||
#define MAIN(s) int main() { char *quine = "#include <stdio.h>\n#define str(s) #s\n#define MAIN(s) "s"\n\nMAIN(str(MAIN(s)))\n"; printf(quine); return 0; }
|
#define MAIN(s) \
|
||||||
|
int main() \
|
||||||
|
{ \
|
||||||
|
char *quine = "#include <stdio.h>\n#define str(s) #s\n#define MAIN(s) " \
|
||||||
|
s \
|
||||||
|
"\n\nMAIN(str(MAIN(s)))\n"; \
|
||||||
|
printf(quine); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
MAIN(str(MAIN(s)))
|
MAIN(str(MAIN(s)))
|
||||||
```
|
```
|
||||||
|
|
||||||
**expansions :**
|
**expands to :**
|
||||||
-> `int main() { char *quine = "#include <stdio.h>\n#define MAIN(s) ""MAIN(s)""\n\nMAIN(str(MAIN(s)))\n"; printf(quine); return 0; }`
|
|
||||||
|
|
||||||
|
|
||||||
**exploded view :**
|
|
||||||
```
|
```
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char *quine = "#include <stdio.h>\n#define MAIN(s) ""MAIN(s)""\n\nMAIN(str(MAIN(s)))";
|
char *quine = "#include <stdio.h>\n#define str(s) #s\n#define MAIN(s) "
|
||||||
printf(quine);
|
"MAIN(s)"
|
||||||
return 0;
|
"\n\nMAIN(str(MAIN(s)))\n";
|
||||||
|
printf(quine);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -315,24 +343,28 @@ int main()
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define xstr(s) #s
|
#define xstr(s) #s
|
||||||
#define str(s) xstr(s)
|
#define str(s) xstr(s)
|
||||||
#define MAIN(s) int main() { char *quine = "#include <stdio.h>\n#define xstr(s) #s\n#define str(s) xstr(s)\n#define MAIN(s) "s"\n\nMAIN(str(MAIN(s)))\n"; printf(quine); return 0; }
|
#define MAIN(s) \
|
||||||
|
int main() \
|
||||||
|
{ \
|
||||||
|
char *quine = "#include <stdio.h>\n#define xstr(s) #s\n#define str(s) xstr(s)\n#define MAIN(s) " \
|
||||||
|
s \
|
||||||
|
"\n\nMAIN(str(MAIN(s)))\n"; \
|
||||||
|
printf(quine); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
MAIN(str(MAIN(s)))
|
MAIN(str(MAIN(s)))
|
||||||
```
|
```
|
||||||
|
|
||||||
**expansions :**
|
**expands to :**
|
||||||
-> `int main() { char *quine = "#include <stdio.h>\n#define xstr(s) #s\n#define str(s) xstr(s)\n#define MAIN(s) ""int main() { char *quine = \"#include <stdio.h>\\n#define xstr(s) #s\\n#define str(s) xstr(s)\\n#define MAIN(s) \"s\"\\n\\nMAIN(str(MAIN(s)))\\n\"; printf(quine); return 0; }""\n\nMAIN(str(MAIN(s)))\n"; printf(quine); return 0; }`
|
|
||||||
|
|
||||||
**exploded view :**
|
|
||||||
```
|
```
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char *quine =
|
char *quine = "#include <stdio.h>\n#define xstr(s) #s\n#define str(s) xstr(s)\n#define MAIN(s) "
|
||||||
"#include <stdio.h>\n#define xstr(s) #s\n#define str(s) xstr(s)\n#define MAIN(s) "
|
"int main() { char *quine = \"#include <stdio.h>\\n#define xstr(s) #s\\n#define str(s) xstr(s)\\n#define MAIN(s) \" s \"\\n\\nMAIN(str(MAIN(s)))\\n\"; printf(quine); return 0; }"
|
||||||
"int main() { char *quine = \"#include <stdio.h>\\n#define xstr(s) #s\\n#define str(s) xstr(s)\\n#define MAIN(s) \"s\"\\n\\nMAIN(str(MAIN(s)))\\n\"; printf(quine); return 0; }"
|
"\n\nMAIN(str(MAIN(s)))\n";
|
||||||
"\n\nMAIN(str(MAIN(s)))\n";
|
printf(quine);
|
||||||
printf(quine);
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user