# Print the ASCII printable character set

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. • 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). Jul 11, 2016 at 0:38 • I'm not sure how to interpret try not to hardcode the output here. For a constant output challenge, that's the only way... Jul 11, 2016 at 0:50 • Do we have to print the output from a function or is returning a string acceptable? Jul 11, 2016 at 5:53 • Does "no other characters" include embedded newlines? – Neil Jul 11, 2016 at 7:48 • 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. Jul 11, 2016 at 23:23 ## 86 Answers # 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. ]  • with Dennis brainfuck outgolfs procedual programming languages! Jul 11, 2016 at 6:59 • @Dennis - You may want to add a leading >. Your implementation runs off the tape to the left, which few interpreters support. Jul 11, 2016 at 14:14 • @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. Jul 11, 2016 at 15:23 • @owacoder Well actually a lot of interpreters support negative tape indices nowadays Apr 27, 2018 at 5:12 # MATL, 3 bytes 6Y2  Try it Online And for the sake of a non-built-in (7 bytes) 32:127c  • Non-builtin: '~':. Apr 30, 2018 at 12:09 • @StewieGriffin Unfortunately that didn't work until 18.5.0. Here's what it looked like at the time of this challenge. Apr 30, 2018 at 12:42 # 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. # 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 • eyyy my @" operator! Jul 12, 2016 at 0:22 • @CᴏɴᴏʀO'Bʀɪᴇɴ it's a very useful operator 10/10 Jul 12, 2016 at 0:22 ## Retina, 18 bytes  ~ {2$
T01p_p

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

T01p_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. # 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. # Octave, 13 bytes disp(' ':'~')  Try it on ideone. # 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. # Python 2, 30 bytes print bytearray(range(32,127))  Try it online! ## Pyth, 5 bytes srd\   r range from d space \<del> to the delete character, 0x7F (included literally in the program) s sum (concatenate all)  # 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. • If you start the loop in 31, you can save one byte removing i++ and using String.fromCharCode(++i). Jul 11, 2016 at 12:29 • console.log can become alert if you don't mind clicking through 97 boxes – user100690 Jun 22, 2021 at 9:25 # 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  # CJam, 6 bytes ',32>  The second byte is a DEL character. Try it online! # Perl 6, 17 bytes print |(' '..'~')  print chrs ^95+32  ## C, 40 bytes f(){for(char i=32;i<128;i++)putchar(i);}  • New here, but do you have to declare a func? (without the f(){ is not ok?) Dec 27, 2018 at 7:25 • @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. Dec 27, 2018 at 9:18 # Bash + general Linux utilities, 18 jot -s '' -c 95 32  # 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  # Ruby, 17 bytes $><<[*' '..?~]*''


or

print *[*' '..?~]


# Canvas, 1 byte

Ｃ


Try it here!

Without builtin:

## 15 14 bytes

９⁵×４－｛⁷⁷＋＋╷ｃ］∑


Try it here!

• :| that's really long without builtin Apr 27, 2018 at 1:35
• @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.) Apr 27, 2018 at 1:37
• lol nvm found a shorter one Apr 27, 2018 at 1:42
• 10 bytes Apr 27, 2018 at 13:08

# TSQL, 53 bytes - Vertical solution

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


Fiddle

# TSQL, 7571 68 bytes - Horizontal solution

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


Fiddle

• this can be executed on any version of sqlserver from this millennium Jul 11, 2016 at 8:11
• Horisontal=>Horizontal ;) Jul 11, 2016 at 11:40
• The second solution appears to be missing the leading space. You can save 3 bytes by changing SELECT to SET. Apr 30, 2018 at 15:51
• @BradC you are absolutely right, thanks. Fixed - considering my more complex answers, I can't believe i missed something this obvious. May 1, 2018 at 7:39
• @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. May 1, 2018 at 13:38

# 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


# APL (Dyalog Extended), 7 bytes (SBCS)

Full program.

' '…'~'


Try it online!

Do I need to explain?

### 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 :

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

# 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
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


# 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


# Vyxal, 5 bytes

kPs5ȯ


Try it Online!

-2 byte thanks to @AaronMiller

• 5 bytes Jul 12, 2021 at 17:26
• @AaronMiller thanks! Nice trick of slicing ascii characters Jul 12, 2021 at 17:28

# SQL, 76 75 bytes

(Microsoft SQL Server 2012+)

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


Demo

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

putStr[' '..'~']


## Pyke, 5 bytes

~KS4>


Try it here!

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

## 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).

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