int main() {
std::cout << "Hello, World!" << std::endl;
int a, b;
a = 5;
b = 6;
printf( "a + b = %d\n", a+b );
}
xg++ -E helloworld.cpp # preprocess
g++ -Wall -S helloworld.cpp # Compile to assembler
g++ -Wall -c helloworld.cpp # Compile to object file
g++ helloworld.cpp # compile and link to ./a.out
g++ helloworld.cpp -o hello.exe # compile to hello.exe
g++ -Wall helloworld.cpp -o hello.exe # all warnings
./a.out
./hello.exe
unterminated comment
You left out a semicolon at the end of a statement.
parse error before <something>
Probably a missing semicolon or brace before an element, though it could be something else.
syntax error before <something>
You tried to use an operator or keyword that doesn't belong there. Alternatively, you might have mispelled something (remember that C++ is case sensitive).
missing terminating " character
You left out the terminating double-quotes of a string literal.
cout
undeclared (first use this function)
You forgot to put #include <iostream>
at the top of your source file.
warning: multi-line string literals are deprecated
You did something like this:
xxxxxxxxxx
cout << "Hello
World";
type identifier;
type identifier(value);
identifier = expression;
const type identifier(value);
cout << "Hello"
and input cin >> identifier
Group | Type names | Notes on size / precision |
---|---|---|
Character types | char | Exactly one byte in size. At least 8 bits. |
char16_t | Not smaller than char . At least 16 bits. | |
char32_t | Not smaller than char16_t . At least 32 bits. | |
wchar_t | Can represent the largest supported character set. | |
Integer types (signed) | signed char | Same size as char . At least 8 bits. |
signed short int | Not smaller than char . At least 16 bits. | |
signed int | Not smaller than short . At least 16 bits. | |
signed long int | Not smaller than int . At least 32 bits. | |
signed long long int | Not smaller than long . At least 64 bits. | |
Integer types (unsigned) | unsigned char | (same size as their signed counterparts) |
unsigned short int | ||
unsigned int | ||
unsigned long int | ||
unsigned long long int | ||
Floating-point types | float | |
double | Precision not less than float | |
long double | Precision not less than double | |
Boolean type | bool | |
Void type | void | no storage |
Null pointer | decltype(nullptr) |
enum
, struct
, typedef
xxxxxxxxxx
for (int i(0); ++i; i<5) {
cout << i << endl;
}
xxxxxxxxxx
int i(2);
int j(3);
{
int i(5);
cout << i << " " << j << endl;
}
cout << i << " " << j << end;
xxxxxxxxxx
using namespace std;
int main()
{
for ( int fahr(-100); fahr <= 400; fahr += 25 ) {
cout << "Fahr =" << fahr << ", Cels = " << 5./9.*(fahr-32) << endl;
}
return 0;
}
xxxxxxxxxx
xxxxxxxxxx
struct foo {
int member;
};
/* GRANDPARENT_H */
1
decimal integer literal0x14
hexadecimal integer literal3.14
floating point literal'x'
character literal"Hello world"
string literal-2
2 + 4
x<y ? x : y
{ } , ;
()
and []
are operators, not separatorsxxxxxxxxxx
using namespace std;
int main () {
// declare simple variables
int i;
double d;
// declare reference variables
int& r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
int& r;
(compile-time error, not initialized)int& r=5;
(compile-time error, lvalue-rvalue mismatch)
name, input parameters, body, return value
xxxxxxxxxx
type name( type1 param1, type2 param2, ... ) {
// body start
type ret;
[...]
// body end
return ret;
}
prototype, definition, function call
overloading
xxxxxxxxxx
double log( double a, double b );
double log( double a, int b );
int log( int a, int b );
call by value, default value
xxxxxxxxxx
double log( double a, double b = 10.0 );
[...]
cout << log(20,2) << endl;
cout << log(20,10) << endl;
cout << log(20) << endl;
call by reference, call by reference to const
xxxxxxxxxx
int swap( int i, int j ); // WRONG
int swap( int& i, int& j ); // CORRECT
int max_degree_of_graph( Graph g ); // copies the graph
int max_degree_of_graph( Graph& g ); // references the graph, the graph can be changed
int max_degree_of_graph( const Graph& g ); // references, it cannot be changed
no nested functions in C++
internal/external