Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Standard output

From Programmer's Wiki

Jump to: navigation, search
 

Comparison of how different languages do screen output. Many languages have different calls for plain output, along with a second call that creates a new line.

The advantage to using console output is that it's usually very fast and can be redirected to other applications, pipes, or files.

Contents

[edit] Language: BASIC

PRINT "Hello world"; : REM No newline
PRINT "Hello world" : REM Newline

[edit] Language: C

#include <stdio.h>

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

The printf function takes a format string and an arbitrary number of additional parameters that supply values to the string. For instance, %s indicate a string, %d a numeric, %c a character, and so forth. Between these flags numerical arguments indicate field with, precision, and justification.

For simply printing a string as-is, with a newline automatically appended at the end:

puts("Hello world.");

[edit] Language: C++

#include <iostream>

using namespace std;

cout << "Hello world!";
cout << "Hello world!" << endl;

[edit] Language: Java

 System.out.print("Text");
 System.out.println("Entire Line of Text");

[edit] Language: Pascal

Pascal uses single quotes for strings.

write('Hello World!');
writeln('Hello World!');

[edit] Language: Perl

Print with new line.

#!/usr/bin/perl

print "Hello World\n";

[edit] Language: PHP

You can either use print.

print "Brought to you by PHP.";
print("Brought to you by PHP.");

Or echo which is slightly faster.

echo "Some text here.";
echo ("Some text here.");

With both, if you use single quotes, the string will be taken literally. Whereas, with double quotes (above) variables and special characters will be expanded. For example, to write something with a newline at the end:

print("This is one line of text\n");

[edit] Language: Python

print "Hello world!"

[edit] Language: Ruby

puts "Text" # adds a newline character at the end
print "Text" # does not add a newline character at the end

[edit] Language: Tcl

puts -nonewline "Hello "
puts "world!"

[edit] See Also

Standard input

Standard error

Rate this article:
Share this article: