# Print the ASCII table

The task is to display n characters of the ASCII table.

You may write a function (or a program that takes the argument as a parameter, STDIN is allowed as well) that takes a parameter n, which will be the index of the last character to print.

The task is quite simple, so as an example here's a possible implementation in Python 2.7:

(lambda n:map(chr, range(n)))(256)


As I said it's a simple task. So this is code-golf and the shortest codes wins!

EDIT

As some of you pointed out this code doesn't print the result. It's just an example since I might struggle explaining the problem in english ;-).

EDIT2

Feel free to post the answer in any programming language, even if it's not the shortest code. Maybe there are some interesting implementations out there!

EDIT3

Fixed the example so it prints the result.

• 1. Does it have to be a function? 2. According to your reference code, n would be the first character that is not printed. – Dennis Oct 28 '14 at 12:49
• Actually the reference code prints nothing. It just returns a list of the characters and lets the REPL do whatever it wants with the result. – manatwork Oct 28 '14 at 12:50
• Can somebody please explain the downvote? I am sorry if my english isn't that good. If there's something unclear within the question please tell me. – oopbase Oct 28 '14 at 12:57
• for x in range(input()):print chr(x) Would actually print the characters, if you want to edit your example. – FryAmTheEggman Oct 28 '14 at 12:59
• nota [i for i in range(n)] is quite similar to range(n) – njzk2 Oct 28 '14 at 19:22

# Microscript, 9 bytes

i1{d1s}ah

Or alternatively:

i1cs1]fah

# C, 31 bytes

i;f(n){for(;i<n;)putchar(i++);}


Use:

i;f(n){for(;i<n;)putchar(i++);}

int main(void)
{
f(34);   //Call the function
return 0;
}

• The challenge says to output all ASCII characters, not just the printable ones, and also to print "n ACII characters", not "ASCII characters up to n", so you could save 4 characters by letter i default to 0 and changing the loop condition to <. And you could save 1 (or 2, depending how eager you are to invoke undefined behavior) character by moving the call to putchar() into the loop condition. – Stuntddude Jun 12 '15 at 0:26
• Thanks. Does the program look ok now? – Spikatrix Jun 12 '15 at 4:42
• @Stuntddude In whch way moving the putchar inside the loop condition would trigger UB? – edc65 Jun 12 '15 at 10:16

### Excel VBA - 143

For i = 1 To 127
Cells(i, 1) = i
Cells(i, 2) = Evaluate("DEC2HEX(" & i & ")")
Cells(i, 3) = Evaluate("DEC2OCT(" & i & ")")
Cells(i, 4) = Chr(i)
Next


{10h$!x}  # Q: 11 bytes {10h$til x}


# Gema, 43 characters

*=@set{i;0}@repeat{*;@int-char{$i}@incr{i}}  Sample run: bash-4.3$ gema '*=@set{i;0}@repeat{*;@int-char{$i}@incr{i}}' <<< 96 | xxd 0000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................ 0000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................ 0000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !"#$%&'()*+,-./
0000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f  0123456789:;<=>?
0000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f  @ABCDEFGHIJKLMNO
0000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f  PQRSTUVWXYZ[\]^_


# Hassium, 45 Bytes

func a(n)for(x=0;x<n;print((x++).toChar())){}


Run and see expanded here

# Pylons, 9

0{d1+,i}c


How it works:

0     # Push 0 to the top of the stack.
{     # Start a for loop.
d    # Duplicate the top of the stack.
1   # Push 1 to the stack.
+  # Add the top two elements of the stack.
,    # Switch to for loop iterators.
i   # Get input
}  # End for loop
c     # Print stack as joined array of chars and exit.


# Oracle SQL 11.2, 48 bytes

SELECT CHR(LEVEL)FROM DUAL CONNECT BY LEVEL<=:1;


# Japt, 7 bytes (non-competing)

Uo md q


Test it online!

### How it works

      // Implicit: U = input integer
Uo    // Create the range [0..U).
md    // Map each item X in this range to X.d().
// If X is a number, this turns into the character with that char code.
q     // Join the array with the empty string.
// Implicit: output last expression


# DUP, 21 bytes

[1+a:0[$1+$a;<][^,]#]


Try it here!

Anonymous lambda. Usage:

256[1+a:0[$1+$a;<][^,]#]!


# Explanation

[                     {start lambda}
1+a:                 {inputnum is end of range (saved to a)}
0                {0 is start of range}
[       ][  ]#  {while loop}
$1+$a;<        {dup, increment, check if top of stack < a}
^,    {if so, output char}
] {end lambda}


## Perl 6, 9 bytes

(^*).chrs


The header and code alone are too short, so have this extra sentence.

# Java 7674 70

Thanks to Mego for saving 4 bytes.

public void f(int x){for(int u=0;u<x;u++)System.out.println((char)u);}


Previous versions:

class f{public void f(int x){for(int u=0;u<x;u++)System.out.println((char)u);}}
class f{public f(int x){for(int u=0;u<x;u++)System.out.println((char)u);}}


The second version needs to be called by new f(n);

Feel free to give me ideas for making it even smaller.

• You can do a standalone function: public void f(int x){...}. For even less bytes, if you're working with Java 8, you can use a lambda: x->java.util.stream.IntStream.range(0,x).mapToObj(n->(char)n); – Mego Dec 14 '16 at 14:44
• In fact, you don't even need the public - a default-scoped function is fine. – Mego Dec 19 '16 at 5:32

## QBIC, 14 bytes, NC

:[a|?chr$(b-1)  Takes a number form the command line and loops from 1 to that number, feeding each iteration into CHR$.

Non-competing, QBIC was thought of a year after this challenge was posted.

# Pushy, 2 bytes

Non-competing as the language postdates the challenge.

Input is given on the command line, $pushy asciitable.pshy 90. Note that this will not work for numbers larger than 127. X"  Explanation:  \ Implicit: Input number on stack X \ Get range [0, n) " \ Interpret as charcodes and print  # Java 8, 45 bytes A lambda expression which takes an integer and prints the table to STDOUT. Golfing suggestions welcome! x->{for(char i=0;i<x;System.out.print(i++));}  It's not clear whether OP wants x to be the last character printed, or the first character not printed. If it's the second, just change i<x to i<=x :) # Python, 30 bytes An unnamed lambda function which returns the result as a string. lambda n:('%c'*n)%(*range(n),)  Try it online! This uses the printf-style string formatting: when '%c' is formatted with an integer, it converts the integer to a character and displays that. So, we multiply the string by n, and format it with all values in range(n). # 29 bytes If we want to be cheeky and return a list of characters instead of a string, we can get 1 byte shorter: lambda n:[*map(chr,range(n))]  Try it online! # QC 18 bytes '00901)01016010003 '00 Read from stdin and store in memory at address 00 901 Print memory from address 01 until 0 is reached )0101 Increment memory at 01 by 1 6010003 Jump to 03 if value at 01 and 00 are not equal  Edited because of invalid amount of bytes ## REXX, 28 bytes say xrange('0'x,d2c(arg(1)))  # Sinclair ZX81 171 bytes for the listing (method 1):  1 LET N$=""
2 GOTO 2+(INKEY$<>"") 3 LET A$=INKEY$4 IF A$=CHR$118 THEN GOTO 9 5 IF CODE A$<28 OR CODE A$>37 THEN GOTO 2 6 PRINT A$;
7 LET N$=N$+A$8 GOTO 2 9 LET N=VAL N$
10 IF N>255 THEN LET N=255
11 PRINT AT 0,0;
12 FOR I=0 TO N
13 PRINT CHR$N; 14 NEXT I  This is using the INKEY$ command to build a string of numbers, which is checked by comparing each input to the character range from 0 to 9 inclusing in line 5. Each character is stored in the N$ variable and outputted to the screen. CHR$ 118 is the ZX81 equivalent of a new line "\r\n" in PHP for instance.

There is a check to see if you have entered a valid range once you have pressed NEW LINE (0 to 255 inclusive) - as you can only enter numbers, the range is not going to be below zero.

Then there is a simple FOR...NEXT loop from line 12 to output the non-ASCII compatible character set. So I lose points because the ZX81 does not handle ASCII without some conversion tables.

Also, the ZX81 character set is incomplete to upper-case and inversed upper-case only; the remaining characters are either graphical symbols or they are ZX81 BASIC keywords, such as PI, INKEY$, PRINT etc... There is a much simpler method that I will post later, but is more prone to human error. ## Method 2, 34 bytes for the listing, no error checks and sanitizing inputs:  1 INPUT N 2 FOR I=0 TO N 3 PRINT CHR$ I;
4 NEXT I


Of course I'm sure that someone has already pointed out that true ASCII is 7 bit and the first 8 bit compatible ASCII set was PETSCII.

# Commodore 64/128

This solution uses PETSCII which is an 8-bit wide ASCII-compatible character set.

tax
.loop
sta $0400,x dex txa bne loop rts  The assembly is doing what the BASIC listing is doing (without switching the character set to business mode first), but is obviously many times quicker. As the 6502 (6510/85xx) processor has an 8-bit accumulator and [8-bit] registers, we start at zero and count down, so 0 - 1 is 0xff. Each character is stored in the screen RAM located at 0x0400 (1024) and sta$0400,x says store the content of X to location $0400 + x. The bne loop will branch back based on the zero flag, once that is set (i.e., x=0x00) it will drop out of the loop and return to BASIC with rts. Once assembled, run with sys 49152 - this solution should work on the C64 and 128. It will work on other Commodore machines as long as you move the routine to a free area of RAM and relocate the starting point of the screen RAM from 1024. # Jelly, 2 bytes ḶỌ  Try it online! According to current reference implementation as of time of posting. # Jelly, 3 bytes ‘ḶỌ  Try it online! Inclusive. Reference implementation is exclusive. # R, 24 bytes cat(intToUtf8(1:scan()))  Try it online! # Tcl, 47 bytes set i 0 time {puts [format %c$i];incr i} $argv  Try it online! • Making the user set n is not valid – ASCII-only May 9 '18 at 22:10 • @ASCII-only: I am in the process of reviewing my older answers. Thanks for pointing out – sergiol May 9 '18 at 23:54 # Excel VBA, 26 bytes An anonymous VBE immediate window function that takes input from cell [A1] For i=1To[A1]:?Chr(i):Next  # Yabasic, 34 bytes Input""n For i=1To n ?Chr$(i)
Next


Try it online!

# Kotlin, 42 bytes

{n:Int->for(c in 0..n-1)print(c.toChar())}


Try it online!

# Check, 2 bytes

,o


Try it online!

Check has:

• Implicit input
• A range builtin
• A builtin to output a list of codepoints as characters

# Momema, 25 bytes

0*-8j0j+1-=*0-9*00+-1*0j1


Outputs the characters in reverse order. Try it online!

## Explanation

0   *-8     #            [0] = read num
j   0       #  label j0: goto j0
j   +1-=*0  #  label j1: goto j(1 - !![0])
-9  *0      #            print char [0]
0   +-1*0   #            [0] = 1 + [0]
j   1       #  label j2: goto j0