Hello world/Line printer

From Rosetta Code
Revision as of 19:48, 29 April 2011 by rosettacode>Kernigh (AWK, Factor are also incorrect.)
Task
Hello world/Line printer
You are encouraged to solve this task according to the task description, using any language you may know.

Cause a line printer attached to the computer to print a line containing the message Hello World!

Note: A line printer is not the same as standard output. A line printer is an older-style printer which prints one line at a time to a continuous ream of paper.

Applesoft BASIC

Assumes a printer card is installed in the Apple II's number 1 expansion slot.

<lang basic> PR#1 PRINT "HELLO WORLD!" </lang>

AutoHotkey

<lang AutoHotkey> Fileappend, Hallo World!, print.txt Run, print "print.txt" </lang>

AWK

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to a regular file named "prn", not a line printer.

<lang AWK> BEGIN { print("Hello World!") >"prn" } </lang>

BASIC

Works with: QBasic
Works with: ZX Spectrum Basic
Works with: Liberty BASIC

<lang qbasic>LPRINT "Hello World!"</lang>

Batch File

<lang dos>ECHO Hello world!>PRN</lang>

C

This example is incorrect. Please fix the code and remove this message.

Details: It prints to standard out rather than to a line printer.

<lang C>#include <stdio.h>

int main() {

  printf("Hello world!\n");
  return 0;

}</lang>

C++

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to stdout

<lang cpp>#include <iostream>

int main() {

   std::cout << "Hello world!" << std::endl; // std::endl provides a portable descriptor for line ending.
   return 0;

}</lang>

Factor

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to stdout, not a line printer.

<lang factor> USE: io IN: hello-world

hello ( -- ) "Hello World!" print ;

MAIN: hello</lang>

when you want a fully working program or in the listener:

<lang factor>"Hello World!" print</lang>

Haskell

<lang haskell> import System.Cmd

cmd = "echo \"Hello World!\" | lpr"

main = system cmd </lang>

Integer BASIC

See Applesoft BASIC.

J

<lang j>require'print' print'Hello world!'</lang>

Perl

Assuming that the line printer is attached to /dev/lp0 <lang perl>open O, ">/dev/lp0"; print O "Hello World!\n"; close O;</lang>

PHP

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to stdout

<lang PHP><?php echo 'Hello world!'; ?></lang>

PicoLisp

<lang PicoLisp>(out '(lpr "-P" "Printer01")

  (prinl "Hello world") )</lang>

PostScript

Technically not really correct as this has to be sent to the printer directly. It will output Hello world, then, though. <lang postscript><</PageSize [595 842]>> setpagedevice  % set page size to DIN A4 /Courier findfont  % use Courier 12 scalefont setfont  % 12 pt 28 802 moveto  % 1 cm from the top and left edges (Hello world) show  % draw the string</lang>

PureBasic

Library: PureLPRINT

<lang PureBasic>MyPrinter$ = LPRINT_GetDefaultPrinter() If LPRINT_OpenPrinter(MyPrinter$)

 If LPRINT_StartDoc("Printing a RC-Task")
   LPRINT_Print(Chr(27) + "E") ; PCL reset for HP Printers
   LPRINT_PrintN("Hello World!")
   LPRINT_NewPage()
   LPRINT_EndDoc()
 EndIf
 LPRINT_ClosePrinter()  

EndIf</lang>

Python

Assuming that the line printer is attached to /dev/lp0 <lang python>lp = open("/dev/lp0") lp.write("Hello World!/n") lp.close()</lang>

REXX

There is no direct way for REXX programs to write to the printer, but
a shell command could be used.

In DOS (or under Windows): <lang rexx> str='Hello World' '@ECHO' str ">PRN" </lang>

Ruby

This example is incorrect. Please fix the code and remove this message.

Details: It prints to standard out rather than to a line printer (which is a peripheral device).

<lang ruby> puts "Hello World" </lang>

Tcl

Unix

<lang tcl>exec lp << "Hello World!"</lang> <lang tcl>set f [open |lp w] puts $f "Hello World!" close $f</lang>

Windows

<lang tcl>set f [open prn w] puts $f "Hello World!" close $f</lang>

UNIX Shell

Works with: Bourne Shell

<lang bash>echo 'Hello World!'|lp</lang>

Works with: POSIX

Alternately, there are the character devices /dev/lp0, /dev/lp1, /dev/lpN and so on which correspond to line printers (if there are any attached to the system). Data written to these devices is sent to attached printers.

<lang bash>echo 'Hello World' > /dev/lp0</lang>