Hello world/Standard error: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 4: Line 4:


=={{header|Ada}}==
=={{header|Ada}}==
<ada>
<lang ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;


Line 11: Line 11:
Put_Line (Standard_Error, "Goodbye, World!");
Put_Line (Standard_Error, "Goodbye, World!");
end Goodbye_World;
end Goodbye_World;
</ada>
</lang>
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
The procedures <code>print</code> and <code>printf</code> output to ''stand out'',
The procedures <tt>print</tt> and <tt>printf</tt> output to ''stand out'',
whereas <code>put</code> and <code>putf</code> can output to any open '''FILE''', including ''stand error''.
whereas <tt>put</tt> and <tt>putf</tt> can output to any open '''FILE''', including ''stand error''.


{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
Line 31: Line 31:
=={{header|C}}==
=={{header|C}}==
Unlike puts(), fputs() does not append a terminal newline.
Unlike puts(), fputs() does not append a terminal newline.
<c>#include <stdio.h>
<lang c>#include <stdio.h>


int main()
int main()
Line 40: Line 40:
return 0;
return 0;
}
}
</c>
</lang>
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<csharp>static class StdErr
<lang csharp>static class StdErr
{
{
static void Main(string[] args)
static void Main(string[] args)
Line 48: Line 48:
Console.Error.WritleLine("Goodbye, World!");
Console.Error.WritleLine("Goodbye, World!");
}
}
}</csharp>
}</lang>


=={{header|C++}}==
=={{header|C++}}==
<cpp>#include <iostream>
<lang cpp>#include <iostream>


using std::cerr;
using std::cerr;
Line 60: Line 60:


return 0;
return 0;
}</cpp>
}</lang>


=={{header|E}}==
=={{header|E}}==
Line 78: Line 78:


=={{header|Java}}==
=={{header|Java}}==
<java>public class Err{
<lang java>public class Err{
public static void main(String[] args){
public static void main(String[] args){
System.err.println("Goodbye, World!");
System.err.println("Goodbye, World!");
}
}
}</java>
}</lang>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 102: Line 102:
In Objective-C one can use the standard C library and the stderr as in the C language; nonetheless a common way to output to stderr for logging purpose and/or error notification is the <tt>NSLog</tt> function, that works almost like <tt>fprintf(stderr, "...")</tt>, save for the fact that the string is a NSString object.
In Objective-C one can use the standard C library and the stderr as in the C language; nonetheless a common way to output to stderr for logging purpose and/or error notification is the <tt>NSLog</tt> function, that works almost like <tt>fprintf(stderr, "...")</tt>, save for the fact that the string is a NSString object.


<objc>#import <Foundation/Foundation.h>
<lang objc>#import <Foundation/Foundation.h>


int main()
int main()
Line 110: Line 110:
NSLog(@"Goodbye, World!");
NSLog(@"Goodbye, World!");
return 0;
return 0;
}</objc>
}</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
<ocaml>prerr_endline "Goodbye, World!"; (* this is how you print a string with newline to stderr *)
<lang ocaml>prerr_endline "Goodbye, World!"; (* this is how you print a string with newline to stderr *)
Printf.eprintf "Goodbye, World!\n"; (* this is how you would use printf with stderr *)</ocaml>
Printf.eprintf "Goodbye, World!\n"; (* this is how you would use printf with stderr *)</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<perl>print STDERR "Goodbye, World!\n";</perl>
<lang perl>print STDERR "Goodbye, World!\n";</lang>


=={{header|PHP}}==
=={{header|PHP}}==
<php>fprintf(STDERR, "Goodbye, World!\n");</php>
<lang php>fprintf(STDERR, "Goodbye, World!\n");</lang>


=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.x}}
{{works with|Python|2.x}}
<python>import sys
<lang python>import sys


print >> sys.stderr, "Goodbye, World!"</python>
print >> sys.stderr, "Goodbye, World!"</lang>


{{works with|Python|3.x}}
{{works with|Python|3.x}}
<python>import sys
<lang python>import sys


print("Goodbye, World!", file=sys.stderr)</python>
print("Goodbye, World!", file=sys.stderr)</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>$stderr.puts("Goodbye, World!")</ruby>
<lang ruby>$stderr.puts("Goodbye, World!")</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==

Revision as of 15:56, 3 February 2009

Task
Hello world/Standard error
You are encouraged to solve this task according to the task description, using any language you may know.

A common practice in computing is to send error messages to a different output stream than normal text console messages. The normal messages print to what is called "standard output" or "standard out". The error messages print to "standard error". This separation can be used to redirect error messages to a different place than normal messages.

Show how to print a message to standard error by printing "Goodbye, World!" on that stream.

Ada

<lang ada> with Ada.Text_IO; use Ada.Text_IO;

procedure Goodbye_World is begin

  Put_Line (Standard_Error, "Goodbye, World!");

end Goodbye_World; </lang>

ALGOL 68

The procedures print and printf output to stand out, whereas put and putf can output to any open FILE, including stand error.

Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - note that printf and putf were not ported into ELLA's libraries.
main:(
  put(stand error, ("Goodbye, World!", new line))
)

Output:

Goodbye, World!

C

Unlike puts(), fputs() does not append a terminal newline. <lang c>#include <stdio.h>

int main() { fprintf(stderr, "Goodbye, "); fputs("World!\n", stderr);

return 0; } </lang>

C#

<lang csharp>static class StdErr {

   static void Main(string[] args)
   {
       Console.Error.WritleLine("Goodbye, World!");
   }

}</lang>

C++

<lang cpp>#include <iostream>

using std::cerr; using std::endl;

int main () {

 cerr << "Goodbye, World!" << endl;
 return 0;

}</lang>

E

stderr.println("Goodbye, World!")

Forth

Works with: GNU Forth
outfile-id
  stderr to outfile-id
  ." Goodbye, World!" cr
to outfile-id

Haskell

import System.IO
hPutStrLn stderr "Goodbye, World!"

Java

<lang java>public class Err{

  public static void main(String[] args){
     System.err.println("Goodbye, World!");
  }

}</lang>

Modula-3

MODULE Stderr EXPORTS Main;

IMPORT Wr, Stdio;

BEGIN
  Wr.PutText(Stdio.stderr, "Goodbye, World!\n");
END Stderr.

Objective-C

Works with: GNUstep
Works with: Cocoa

In Objective-C one can use the standard C library and the stderr as in the C language; nonetheless a common way to output to stderr for logging purpose and/or error notification is the NSLog function, that works almost like fprintf(stderr, "..."), save for the fact that the string is a NSString object.

<lang objc>#import <Foundation/Foundation.h>

int main() {

  fprintf(stderr, "Goodbye, World!\n");
  fputs("Goodbye, World!\n", stderr);
  NSLog(@"Goodbye, World!");
  return 0;

}</lang>

OCaml

<lang ocaml>prerr_endline "Goodbye, World!"; (* this is how you print a string with newline to stderr *) Printf.eprintf "Goodbye, World!\n"; (* this is how you would use printf with stderr *)</lang>

Perl

<lang perl>print STDERR "Goodbye, World!\n";</lang>

PHP

<lang php>fprintf(STDERR, "Goodbye, World!\n");</lang>

Python

Works with: Python version 2.x

<lang python>import sys

print >> sys.stderr, "Goodbye, World!"</lang>

Works with: Python version 3.x

<lang python>import sys

print("Goodbye, World!", file=sys.stderr)</lang>

Ruby

<lang ruby>$stderr.puts("Goodbye, World!")</lang>

UNIX Shell

echo "Goodbye, World!" > /dev/stderr