Input/Output cheat sheet |
Input in C |
These examples require the following line at the top:
#include <stdio.h>
Read an integer, skipping leading whitespace.
int x;
scanf( "%d", &x ); Read integers, one at a time, skipping whitespace, until the end of input.
int x;
while( scanf( "%d", &x ) ) { /* Do something with x. */ } Read a character, skipping leading whitespace.
char c;
scanf( "%c", &c ); Read the next character, whatever it is. Returns -1 when there is nothing left to read.
int c = getchar();
Read characters one by one until the end of input.
int c;
while( ( c = getchar() ) >= 0 ) { char ch = ( char )c; /* Do something with ch. */ } Read a word, skipping leading whitespace. Make sure w[] has enough space.
char w[1024];
scanf( "%s", w ); Read one line at a time until the end of input. This code gives a warning at compile time. Make sure s[] has enough space.
char s[1024];
while( gets( s ) ) { /* s[] contains the line without the '\n' at the end. */ } |
Output in C |
int p = 17;
Result: "Case #17: 69".long long x = 69; printf( "Case #%d: %Ld\n", p, x );
double x = 0.1234, y = 0.1235;
Result: "0.123 0.124".printf( "%.3f %.3f\n", x, y );
int x = 13;
Result: "13| 13|0013|13 |00000013|".printf( "%d|%4d|%04d|%-4d|%0*d|\n", x, x, x, x, 8, x ); |
Input in C++ |
These examples require the following lines at the top:
#include <iostream>
#include <string> using namespace std; Read an integer, skipping leading whitespace.
int x;
cin >> x; Read integers, one at a time, skipping whitespace, until the end of input.
int x;
while( cin >> x ) { // Do something with x. } Read a character, skipping leading whitespace.
char c;
cin >> c; Read a word, skipping leading whitespace.
string w;
cin >> w; Read one line at a time until the end of input.
string s;
while( getline( cin, s ) ) { // s contains the line without the '\n' at the end. } |
Output in C++ |
int p = 17;
Result: "Case #17: 69".long long x = 69; cout << "Case #" << p << ": " << x << endl; For more complicated output formatting, printf() is probably the best choice (see above). |
Input in Java 1.5 |
These examples require the following lines at the top:
import java.io.*;
import java.util.*; Scanner in = new Scanner( System.in ); Read an integer, skipping leading whitespace.
int x = in.nextInt();
Read integers, one at a time, skipping whitespace, until the end of input.
while( in.hasNextInt() )
{ int x = in.nextInt(); // Do something with x. } Read the next character, whatever it is. Returns -1 if there is nothing more to read.
int c = System.in.read();
Read a word, skipping leading whitespace.
String w = in.next();
Read one line at a time until the end of input.
while( in.hasNextLine() )
{ String s = in.nextLine(); // s contains the line, including the '\n' at the end. } |
Output in Java 1.5 |
int p = 17;
Result: "Case #17: 69".long x = 69; System.out.println( "Case #" + p + ": " + x );
double x = 0.1234, y = 0.1235;
Result: "0.123 0.124".System.out.printf( "%.3f %.3f\n", x, y );
int x = 13;
Result: "13| 13|0013|13 |00000013|".System.out.printf( "%d|%4d|%04d|%-4d|%0*d|\n", x, x, x, x, 8, x ); |