g++ helloWorld.cpp
:
compiling the code g++
is the Gnu C++ compiler, ./a.out
for running the executable #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }
#include <iostream>
for including
the library for using namespace std;
:
will be explained later.int main()
: the program starts here.{...}
: a scope.cout
: stream to console.<<
: put stuff into stream.return 0
returns the result
of a function (main).// helloWorld.cpp // a program that prints the immortal saying "hello world" #include <iostream> // include utilities for input and output using namespace std; // we will be using functions from the // standard C++ libary int main() // entry point { // starting a scope cout << "Hello World!" << endl; return 0; } // ending a scope
cout << ...
:
output to console ('to stream cout')
cin >> ...
:
read from console ('from stream cin')
;
if (something)
, do {...} #include <iostream> // input-output stream using namespace std; // std == standard int main () { int value; // define a integer variable cout << "enter an integer value: "; // message to console cin >> value; // read from console cout << " value, 2*(value): " << value; cout << " " << 2*value << "\n"; // if (value==7) // an if statement cout << " ---seven is magical---\n"; return 1; }
rand()
: (pseudo) random number generator, #include <iostream> /* another way for comments */ #include <stdlib.h> /* srand, rand */ #include <time.h> /* for the time */ using namespace std; // ================== // // ================== // int f_1(int a, int b) {return a - b;} // from function arguments: call by value void f_2(int &x); // forward declaration with call be reference // ================== // // ================== // int main() { // srand( (unsigned)time( NULL ) ); // initializing the random number // generator with a random seed: // the current time // int a = 1; int b = 2; int z = 333; // cout << "a :: " << a << endl; cout << "b :: " << b << endl; cout << "z :: " << b << endl << endl; // int c = f_1(a,b); f_2(z); // void functions return nothing // cout << "c = f_1(a,b) :: " << c << endl; cout << "z f_2(z)() :: " << z << endl; // } // =================================== // // === definition of function body === // // =================================== // void f_2(int &x) // '&' for reference (memory address) { x = rand() % 10; // '%' modulo }
#include <iostream> /* another way for comments */ using namespace std; int a = 22; // global variable, scope: global // ================== // // ================== // int f_1(int a) {return a;} // local beats global int f_2(int x) {return a;} // local version does not exist // ================== // // ================== // int main() { int x = 777; cout << "a, global :: " << a << endl; cout << "x, local :: " << x << endl << endl; cout << "f_1(3) :: " << f_1(3) << endl; cout << "f_2(x) :: " << f_2(x) << endl << endl; // double z = 6.66; // scope: main if (1==1) { double z = 5.55; // scope: if cout << "z, local :: " << z << endl; } // return 1; // end of mail }
printf()
takes exactly the same formating
options as in C
printf()
is taken over from C and needs int main(int argLength, char* argValues[])
#include <iostream> #include <stdio.h> // for printf #include <cstdlib> // for atof using namespace std; int main(int argLength, char* argValues[]) // parsing command line arguments { printf("number of input arguments: %3d\n",argLength); for(int i = 0; i < argLength; i++) cout << "argValues[" << i << "] = " << argValues[i] << endl; // double inputDouble = atof(argValues[1]); // casting string to double int inputInteger = (int)inputDouble; // casting double to integer // printf("\n"); printf("floating-point %10.3f number\n",inputDouble); printf(" exponential %10.3e number\n",inputDouble); printf(" fixed-width %10s string\n" ,argValues[2]); printf(" fixed-width %10d integer\n" ,inputInteger); // problems with strings string ss = "a C++ string can only be printed with printf()\n"; string tt = "if turned previously into a C-string by .c_str()\n"; printf("\n"); printf("%s%s",ss.c_str(),tt.c_str()); // return 0; } // end of main()
user@pc:~$ ./a.out 3345.345 hello number of input arguments: 3 argValues[0] = ./a.out argValues[1] = 3345.345 argValues[2] = hello floating-point 3345.345 number exponential 3.345e+03 number fixed-width hello string fixed-width 3345 integer a C++ string can only be printed with printf() if turned previously into a C-string by .c_str()