11
\$\begingroup\$

Challenge: Print the entire printable ASCII charset (not just a range!) in order.

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Rules: read closely

  • No other characters allowed in the output.

  • Program/function does not accept input.

  • Try not to hardcode/embed the output.

  • This is code golf, shortest answer wins.

\$\endgroup\$
8
  • 7
    \$\begingroup\$ Well they are a couple main things about this challenge that are why it's getting downvoted. A) the community really doesn't like it when you ban golfing language (whether or not I agree with this is a different matter) B) it's rather simple but I wouldn't say it's exactly trivial (kinda on the edge). Also saying "Try not to ..." usually isn't a good sign because it's not actually enforcing it (because it's hard to enforce this objectively), and it might mean your challenge may not be found that interesting (therefore getting downvotes). \$\endgroup\$
    – Downgoat
    Jul 11, 2016 at 0:38
  • 10
    \$\begingroup\$ I'm not sure how to interpret try not to hardcode the output here. For a constant output challenge, that's the only way... \$\endgroup\$
    – Dennis
    Jul 11, 2016 at 0:50
  • 1
    \$\begingroup\$ Do we have to print the output from a function or is returning a string acceptable? \$\endgroup\$
    – Dennis
    Jul 11, 2016 at 5:53
  • 2
    \$\begingroup\$ Does "no other characters" include embedded newlines? \$\endgroup\$
    – Neil
    Jul 11, 2016 at 7:48
  • 2
    \$\begingroup\$ This is NOT a duplicate of that challenge! Programs there had to accept input and print only part of the table. This challenge is easier and different. \$\endgroup\$
    – anna328p
    Jul 11, 2016 at 23:23

87 Answers 87

18
\$\begingroup\$

Brainfuck, 30 27 bytes

+[[>++<<+>-]>]<<<++[<.+>++]

Try it online!

How it works

+ changes the initial cell to 1. After this step, we enter the following, nested loop.

[        While the current cell is non-zero:
  [        While the current cell (C) is non-zero:
    >++      Increment the cell to C's right twice.
    <<+      Increment the cell to C's left.
    >-       Decrement C.
  ]
  >        Advance to the cell to C's right.
]

This computes consecutive powers of 2 until the the value 256 = 0 (mod 256) is reached. When the outer loop finishes, the tape is in the following state.

                                     v
001 002 004 008 016 032 064 128 000 000 000

<<<++ retrocedes three cells and increments twice, leaving the tape as follows.

                         v
001 002 004 008 016 032 066 128 000 000 000

Now we're ready to print the actual output. As a stop condition, we increment the cell above twice each time we print and increment the cell to its left. Since 66 + 95 × 2 = 256 = 0 (mod 256), we stop after printing all 95 printable ASCII characters. We achieve this as follows.

[      While the current cell (C) is non-zero:
  <      Retrocede to the cell to C's left.
  .+     Print its content and increment.
  >++    Increment C twice.
]
\$\endgroup\$
4
  • 3
    \$\begingroup\$ with Dennis brainfuck outgolfs procedual programming languages! \$\endgroup\$ Jul 11, 2016 at 6:59
  • \$\begingroup\$ @Dennis - You may want to add a leading >. Your implementation runs off the tape to the left, which few interpreters support. \$\endgroup\$
    – owacoder
    Jul 11, 2016 at 14:14
  • 3
    \$\begingroup\$ @owacoder On PPCG, languages are defined by their implementations. As long as there is one interpreter that behaves as desired (and the one on Try it Online! does), it's considered valid. \$\endgroup\$
    – Dennis
    Jul 11, 2016 at 15:23
  • \$\begingroup\$ @owacoder Well actually a lot of interpreters support negative tape indices nowadays \$\endgroup\$
    – ASCII-only
    Apr 27, 2018 at 5:12
7
\$\begingroup\$

MATL, 3 bytes

6Y2

Try it Online

And for the sake of a non-built-in (7 bytes)

32:127c
\$\endgroup\$
2
  • \$\begingroup\$ Non-builtin: '~':. \$\endgroup\$ Apr 30, 2018 at 12:09
  • \$\begingroup\$ @StewieGriffin Unfortunately that didn't work until 18.5.0. Here's what it looked like at the time of this challenge. \$\endgroup\$
    – Suever
    Apr 30, 2018 at 12:42
7
\$\begingroup\$

Brainfuck, 40 39 bytes

++++[->++++[->++>++++++<<]<]>>>-[-<.+>]

Try it online.

Explanation

++++[->++++[->++>++++++<<]<]

The nested loops basically mean you multiply the number of plusses together, so 4 × 4 × 2 = 32 in one cell and 4 × 4 × 6 = 96. Here is the tape after running this:

00 00 32 96
 ^

>>>- moves the pointer to the fourth cell and decrements it. Now we're done with the setup. 32 is the code for space, the first printable ASCII character. 95 is the number of characters we have to print. Here is the tape now:

00 00 32 95
          ^

[-<.+>] runs until the current cell (the fourth one) is zero. It decrements the counter and prints the character and increments it for the next time.

\$\endgroup\$
7
\$\begingroup\$

Cheddar, 29 bytes

->(32:126).map((i)->@"i).fuse

Range from 32-126, loop over it and get the string at the given char code @" and the fuse together (join)

Cheddar, 7 bytes

32@"126

Unfortunately this is broken as of the current release but I'm sure you can go back some versions where this works

\$\endgroup\$
2
  • \$\begingroup\$ eyyy my @" operator! \$\endgroup\$ Jul 12, 2016 at 0:22
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ it's a very useful operator 10/10 \$\endgroup\$
    – Downgoat
    Jul 12, 2016 at 0:22
6
\$\begingroup\$

Retina, 18 bytes


~
{2`
$`
T01`p`_p

The leading linefeed is significant.

Try it online!

Explanation

Stage 1: Substitution


~

We start by replacing the empty (non-existent) input with a single ~.

Stage 2: Substitution

{2`
$`

The regex of this substitution is still empty, since the ` separates configuration from regex and {2 is therefore just the configuration. The { indicates that the remaining two stages should be run in a loop until they stop changing the output. The 2 indicates that this specific stage has a limit of 2, meaning that only the first two matches of the regex will be replaced. Since the regex is empty, that means we get an empty match in front of the string and an empty match after the first character.

This match is replaced with the prefix $` which refers to everything in front of the match. For the first match, there is nothing in front of it, so this doesn't insert anything, but for the second match, there is the leading character in front of it, which therefore gets duplicated.

Stage 3: Transliteration

T01`p`_p

Here, T activates transliteration mode, and 0 and 1 are limits (where 0 just means "don't set this limit"). Together, they mean "transliterate only the first character in the string". The actual transliteration maps from p to _p. Here, p expands to the printable ASCII characters and _ means "remove" this character, so the expanded lists look like this:

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
_ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

That means spaces get removed and all other characters get decremented by one.

To see how the last two stages act together here is the string after each of the first few and last stages:

~
~~
}~
}}~
|}~
||}~
{|}~

...

"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
!!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Since the state of the string is only checked after every other stage to determine whether to end the loop, and the two stages cancel each other once we reach the leading space (since Stage 2 adds a space and Stage 3 removes it), this terminates the loop and therefore the program.

\$\endgroup\$
5
\$\begingroup\$

Python 2.7, 36 bytes:

print''.join(map(chr,range(32,127)))

Simple enough. A full program that prints out the entire ASCII sequence in order.

\$\endgroup\$
4
\$\begingroup\$

Octave, 13 bytes

disp(' ':'~')

Try it on ideone.

\$\endgroup\$
4
\$\begingroup\$

Bash, 54 bytes

man ascii|fold -w1|LC_ALL=C sort -u|tr -cd '[:print:]'

Try it online!

I like that this uses the ASCII man page (which contains an ASCII table) to extract the characters.

\$\endgroup\$
4
\$\begingroup\$

Python 2, 30 bytes

print bytearray(range(32,127))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Pyth, 5 bytes

srd\

Try it here.

 r         range from
  d        space
   \<del>  to the delete character, 0x7F (included literally in the program)
s          sum (concatenate all)
\$\endgroup\$
0
3
\$\begingroup\$

JavaScript, 54 bytes

for(i=32;i<128;i++)console.log(String.fromCharCode(i))

Wasted quite a few bytes printing it... Also prints each char on a new line.

\$\endgroup\$
2
  • 6
    \$\begingroup\$ If you start the loop in 31, you can save one byte removing i++ and using String.fromCharCode(++i). \$\endgroup\$ Jul 11, 2016 at 12:29
  • \$\begingroup\$ console.log can become alert if you don't mind clicking through 97 boxes \$\endgroup\$
    – user100690
    Jun 22, 2021 at 9:25
3
\$\begingroup\$

Java 8, 48 bytes

v->{for(char i=31;++i<127;)System.out.print(i);}

Try it online.

Explanation:

v->{                       // Method with empty unused parameter and no return-type
  for(char i=31;++i<127;)  //  Loop characters from ' ' to '~'
    System.out.print(i);}  //   And print it
\$\endgroup\$
2
\$\begingroup\$

CJam, 6 bytes

',32>

The second byte is a DEL character. Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 6, 17 bytes

print |(' '..'~')
print chrs ^95+32
\$\endgroup\$
2
\$\begingroup\$

C, 40 bytes

f(){for(char i=32;i<128;i++)putchar(i);}
\$\endgroup\$
2
  • \$\begingroup\$ New here, but do you have to declare a func? (without the f(){ is not ok?) \$\endgroup\$
    – Déjà vu
    Dec 27, 2018 at 7:25
  • 1
    \$\begingroup\$ @RingØ In C, you can't just write code without a function. each submission has to compile or run, so in JS for example it would be ok to not put the f() but it's required for C. \$\endgroup\$
    – anna328p
    Dec 27, 2018 at 9:18
2
\$\begingroup\$

Bash + general Linux utilities, 18

jot -s '' -c 95 32
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 3 bytes

@Pw

@P is the string that contains the printable ASCII characters so… yeah, not very interesting.

It's not clear whether OP accepts built-ins or not, and since others have posted answers that use built-ins, I'll use the 3 bytes version until OP clarifies this point.

With no built-in, 14 bytes

32:126e          Get a number between 32 and 126
       :"~c"w    Format that number to STDOUT as a char code
             \   Backtrack
\$\endgroup\$
2
\$\begingroup\$

Ruby, 17 bytes

$><<[*' '..?~]*''

or

print *[*' '..?~]
\$\endgroup\$
2
\$\begingroup\$

Canvas, 1 byte

Try it here!


Without builtin:

15 14 bytes

9⁵×4-{⁷⁷++╷c]∑

Try it here!

\$\endgroup\$
4
  • \$\begingroup\$ :| that's really long without builtin \$\endgroup\$
    – ASCII-only
    Apr 27, 2018 at 1:35
  • \$\begingroup\$ @ASCII-only yeah. ⁴⁷⁷++╷ is the shortest way i could find to make the number 95 (Canvas only has literals for 0-9, and predefined variables for 10, Infinity, 256, 13, 64, 11, 12, 16, and 128.) \$\endgroup\$
    – hakr14
    Apr 27, 2018 at 1:37
  • \$\begingroup\$ lol nvm found a shorter one \$\endgroup\$
    – hakr14
    Apr 27, 2018 at 1:42
  • 2
    \$\begingroup\$ 10 bytes \$\endgroup\$
    – ASCII-only
    Apr 27, 2018 at 13:08
2
\$\begingroup\$

TSQL, 53 bytes - Vertical solution

DECLARE @ int=32x:PRINT char(@)SET @+=1IF @<127GOTO x

Fiddle

TSQL, 75 71 68 bytes - Horizontal solution

DECLARE @ char(95)=''WHILE 95>LEN(@)SET @=char(126-LEN(@))+@ PRINT @

Fiddle

\$\endgroup\$
11
  • \$\begingroup\$ this can be executed on any version of sqlserver from this millennium \$\endgroup\$ Jul 11, 2016 at 8:11
  • 1
    \$\begingroup\$ Horisontal=>Horizontal ;) \$\endgroup\$
    – aloisdg
    Jul 11, 2016 at 11:40
  • 1
    \$\begingroup\$ The second solution appears to be missing the leading space. You can save 3 bytes by changing SELECT to SET. \$\endgroup\$
    – BradC
    Apr 30, 2018 at 15:51
  • \$\begingroup\$ @BradC you are absolutely right, thanks. Fixed - considering my more complex answers, I can't believe i missed something this obvious. \$\endgroup\$ May 1, 2018 at 7:39
  • 1
    \$\begingroup\$ @t-clausen.dk But I still don't get a leading space from your latest code, looks like all you did was change the SELECT to SET. If you change both 94s to 95 that seems to fix it. \$\endgroup\$
    – BradC
    May 1, 2018 at 13:38
2
\$\begingroup\$

Elixir, 24 bytes

IO.puts Enum.uniq 32..?~

Try it online!

Prints a trailing newline. If that's not acceptable, it's +1 byte for write instead of puts.

uniq is the cheapest operation I managed to find for converting range to list. As a bonus, here are 4 different programs that all accomplish the task in 27 bytes:

IO.puts Enum.to_list 32..?~
IO.puts Enum.into 32..?~,[]
IO.puts Enum.take 32..?~,95
IO.puts for n<-32..?~,do: n
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Extended), 7 bytes (SBCS)

Full program.

' '…'~'

Try it online!

Do I need to explain?

\$\endgroup\$
2
\$\begingroup\$

x86_16 machine code - 21 15 bytes

-6 bytes, thanks @anna328p

B8 20 0E        MOV AX, 0E20H
            .LOOP:
CD 10           INT 10H
40              INC AX
3C 7E           CMP AL, 7EH
75 F6           JNE .LOOP
B8 4C00         MOV AX, 4C00H
CD 21           INT 21H

Running on DOSBox :

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Is line 17 needed? \$\endgroup\$
    – anna328p
    Mar 7, 2021 at 8:35
  • \$\begingroup\$ @anna328p of course, The program will hang if there is no exit function after the loop \$\endgroup\$ Mar 7, 2021 at 9:15
  • \$\begingroup\$ I got it down to 15 bytes: uCAOzRBAPH91+bhMAM0h (base64) \$\endgroup\$
    – anna328p
    Mar 20, 2021 at 20:44
  • \$\begingroup\$ cool ! , thank you \$\endgroup\$ Mar 21, 2021 at 4:31
2
\$\begingroup\$

Perl 5 using only keywords with lowercase alphabets only separating each of them only a space, 227 129 bytes

eval q y print chr length xor s qq q while uc le q q q x length q q truncate tie q x length q q foreach q y for q q q x ord q q q

Try it online!

How it works

eval q y
      print chr length
      xor s qq q
   while
      # default variable has spaces only; equibalent to $_
      uc
      # instead of <=126
      le q q q
         # 126==14*9
         x length q q truncate tie q
         x length q q foreach q
# just noticed i could golf the word
y for q q q x ord q q q

Previous: 227 bytes.

eval q y print chr length xor s qq q while chr length lt chr length q q length length length length length length length length length length length length length length length length length length q y foreach q q q x ord q q q

Try it online!

How it works

Using length of default variable to be accumulator.

eval q y
      # print chr($accumulator)
      print chr length xor
      # increment it
      s qq q
   while
      # instead of <
      chr length lt
      # 127 is unfortunately a prime; any efficient ideas within the restrictions?
      chr length q q length length length length length length length length length length length length length length length length length length q
# $accumulator=32
y foreach q q q x ord q q q
\$\endgroup\$
0
2
\$\begingroup\$

Python 3, 20 bytes

lambda:range(32,127)

Try it online! Returns a range of codepoints.

lambda:range(32,127)  # full program
lambda:               # return function taking no arguments that returns...
       range(  ,   )  # range object of all integers between...
             32       # literal...
       range(  ,   )  # minus 1 and...
                127   # literal
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 5 bytes

kPs5ȯ

Try it Online!

-2 byte thanks to @AaronMiller

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 5 bytes \$\endgroup\$ Jul 12, 2021 at 17:26
  • \$\begingroup\$ @AaronMiller thanks! Nice trick of slicing ascii characters \$\endgroup\$
    – Wasif
    Jul 12, 2021 at 17:28
1
\$\begingroup\$

SQL, 76 75 bytes

(Microsoft SQL Server 2012+)

declare @ int=32;while(@<127)begin;print char(@);set @+=1;end

Demo

\$\endgroup\$
1
  • \$\begingroup\$ Not sure outputting on separate lines satisfies the challenge. Also, can save a few bytes with a label/and GOTO instead of while. \$\endgroup\$
    – BradC
    Apr 30, 2018 at 14:29
1
\$\begingroup\$

Haskell, 16 bytes

putStr[' '..'~']
\$\endgroup\$
1
\$\begingroup\$

Pyke, 5 bytes

~KS4>

Try it here!

Pyke's printable variable isn't sorted and it contains tabs and newlines etc... :(

\$\endgroup\$
1
\$\begingroup\$

C - 35 bytes

f(i){for(i=31;++i<128;putchar(i));}

Call:

int main() {
    f();
}

Uses the horrible int-as-a-string trick I learnt from Lynn. (Will again if OP confirms that a vertical output is OK).

\$\endgroup\$
4
  • \$\begingroup\$ That only works on little-endian machines though. putchar(i) is the same length. \$\endgroup\$
    – owacoder
    Jul 11, 2016 at 11:50
  • \$\begingroup\$ @owacoder mmh, I had puts there originally, but then I changed it because of the newlines and didn't question it further. Thanks! \$\endgroup\$
    – Quentin
    Jul 11, 2016 at 11:55
  • \$\begingroup\$ i;f(){for(;++i<96;putchar(i+32));} also works, and saves a byte. \$\endgroup\$
    – owacoder
    Jul 11, 2016 at 11:57
  • \$\begingroup\$ 34 bytes \$\endgroup\$
    – c--
    Jul 28, 2022 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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