Skip to main content

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

This may segfault on 64-bit systems with some compilers, such as newest Clang, due to the type of s being interpreted as 32-bit int instead of a pointer.

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment anyway :P


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and passes the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

Also here's a longer 64-byte version that will work on all combinations of compilers and ASCII environments that support the POSIX printf $ extension:

*s;main(){printf(s="*s;main(){printf(s=%c%s%1$c,34,s);}",34,s);}

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment anyway :P


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and passes the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

This may segfault on 64-bit systems with some compilers, such as newest Clang, due to the type of s being interpreted as 32-bit int instead of a pointer.

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment anyway :P


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and passes the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

Also here's a longer 64-byte version that will work on all combinations of compilers and ASCII environments that support the POSIX printf $ extension:

*s;main(){printf(s="*s;main(){printf(s=%c%s%1$c,34,s);}",34,s);}
added 9 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment. anyway :P


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and passes the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment.


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment anyway :P


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and passes the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}
added 184 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

WorksThis works in GCC, Clang, and tccTCC in a POSIX environment. InvokesIt invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment.


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

Works in GCC, Clang, and tcc. Invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of.


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}

C, 64 60 bytes

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}

So far, this is the shortest known C quine. There's an extended bounty if you find a shorter one.

This works in GCC, Clang, and TCC in a POSIX environment. It invokes an excessive amount of undefined behavior with all of them.

Just for fun, here's a repo that contains all the C quines I know of. Feel free to fork/PR if you find or write a different one that adds something new and creative over the existing ones.

Note that it only works in an ASCII environment. This works for EBCDIC, but still requires POSIX. Good luck finding a POSIX/EBCDIC environment.


How it works:

  1. main(s) abuses main's arguments, declaring a virtually untyped variable s. (Note that s is not actually untyped, but since listed compilers auto-cast it as necessary, it might as well be*.)
  2. printf(s="..." sets s to the provided string and the first argument to printf.
  3. s is set to main(s){printf(s=%c%s%1$c,34,s);}.
  4. The %c is set to ASCII 34, ". This makes the quine possible. Now s looks like this:
    main(s){printf(s="%s%1$c,34,s);}.
  5. The %s is set to s itself, which is possible due to #2. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.
  6. The %1$c is set to ASCII 34 ", printf's first** argument. Now s looks like this:
    main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
    ... which just so happens to be the original source code.

* Example thanks to @Pavel
** first argument after the format specifier - in this case, s. It's impossible to reference the format specifier.


I think it's impossible that this will get any shorter with the same approach. If printf's format specifier was accessible via $, this would work for 52 bytes:

main(){printf("main(){printf(%c%0$s%1$c,34);}",34);}
added 11 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 161 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 123 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 234 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 68 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
deleted 11 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 914 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 303 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 6 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 171 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
added 171 characters in body
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading
Source Link
MD XF
  • 14.3k
  • 5
  • 70
  • 108
Loading