Lesson of 15 February 2010¶
Some simple C/C++ programs
A minimal program
File example1.cc
1/*
2 I am a comment
3 */
4
5// I am a single line comment
6
7/*
8 A C/C++ program is a "function" called "main()"
9 which return an integer.
10 Return 0 means all ok!.
11 Return != from 0 signal an error to the OS
12 */
13
14// standard I/O of C (libc)
15#include <stdio.h>
16
17// standard I/O of C++ (libstdc++)
18#include <iostream>
19
20using std::cout ; // use object cout of namespace std
21using namespace std ; // use all the functions and object of namespace std
22
23int // the type of return value
24main() { // brackets contains the bosy of the program
25
26 // standard C way of I/O
27 printf("I am a string\n") ; // \n = new line character
28 // \r = return character
29 // \t = tabulation
30 // cout = character out object
31 cout << "I am a string output by cout\n" ;
32
33 return 0 ; // return 0 to the OS
34}
Built in types
File example2.cc
1// standard I/O of C (libc)
2#include <stdio.h>
3
4// standard I/O of C++ (libstdc++)
5#include <iostream>
6
7using namespace std ; // use all the functions and object of namespace std
8
9int // the type of return value
10main() { // brackets contains the bosy of the program
11
12 // the standard type of C/C++
13 int a, b, c ; // declare and define a, b, c as integer variable
14 c = 1 ;
15 a = 1 ; // assign a to the value 1
16 a = b / c ; // integer division
17 a = b % c ; // remainder of the division of b by c
18 /*
19 other integer like type
20 short int (smaller integer normally 2 byte)
21 long int (larger integer normlly 8 byte)
22
23 unsigned int (int without sign)
24 unsigned short int (short int without sign) [ short == short int]
25 unsigned long int (long int without sign) [ long == long int]
26 */
27
28 float d, e, f ; // declare and define d, e,f as single precision floating point variables
29 d = 1.0 ;
30 e = 2.0 ;
31 e += d ; // ==> e = e + d
32 // in general "a OP= b" is equivalent to " a = a OP b "
33
34 double g, h, i ; // declare and define d, e,f as double precision floating point variables
35
36 return 0 ; // return 0 to the OS
37}
Conditional and loops
File example3.cc
1// standard I/O of C (libc)
2#include <stdio.h>
3
4// standard I/O of C++ (libstdc++)
5#include <iostream>
6
7using namespace std ; // use all the functions and object of namespace std
8
9int
10main() {
11
12 int a, b, c, d ;
13
14 a = 1 ;
15 if ( a < 3 ) cout << " a < 3 " ;
16 else cout << " a >= 3 " ; // use {} or more than 1 instruction
17 /*
18 comparisoin operators
19 == Check equality
20 != Check inequality
21 > greather then
22 < less then
23 >= greather or equal then
24 <= less or equal then
25 */
26
27 if ( a<3 && b == 0 ) cout << "a<3 and b==0\n" ;
28 /*
29 logical operation
30
31 || or
32 && and
33 ! not
34 */
35
36 for ( int i=0 ; i < 10 ; ++i ) // ++i => i = i+1
37 cout << i << "\n" ;
38
39 /*
40 for ( A ; B ; C ) EXPR ;
41
42 A is executed ONCE at the beginning of the loop
43 B is execued EACH time at the beginning of the loop
44 EXPR is executed each time B is true
45 C is executed at the end of each loop
46 */
47
48 int i = 10 ;
49 while ( --i > 0 ) cout << i << "\n" ;
50 /*
51 while ( A ) EXPR ;
52 EXPR is executed each time A is true
53 */
54
55 i = 10 ;
56 do {
57 cout << i << "\n" ;
58 } while ( --i > 0 ) ;
59 /*
60 do { EXPR ; } while ( A ) ;
61 EXPR is executed, then check A
62 */
63
64 return 0 ; // return 0 to the OS
65}
Functions and recursion
File example4.cc
1// standard I/O of C (libc)
2#include <stdio.h>
3
4// standard I/O of C++ (libstdc++)
5#include <iostream>
6
7using namespace std ; // use all the functions and object of namespace std
8
9// define the function factorial
10int
11fact( int n ) {
12 int res = 1 ;
13 for ( int i = 2 ; i <= n ; ++i )
14 res *= i ;
15 return res ;
16}
17
18// define the function factorial recursively
19int
20factRec( int n ) {
21 if ( n <= 1 ) return 1 ;
22 return n* factRec(n-1) ;
23}
24
25
26int
27main() {
28 int n = 4 ;
29 cout << "fact " << n << " = " << fact(n) << "\n" ;
30 cout << "factRec " << n << " = " << factRec(n) << "\n" ;
31 return 0 ; // return 0 to the OS
32}
Splitting code in more files
File example5.h
1// standard I/O of C (libc)
2#include <stdio.h>
3
4// standard I/O of C++ (libstdc++)
5#include <iostream>
6
7using namespace std ; // use all the functions and object of namespace std
8
9// declaration the function factorial
10extern int fact( int n ) ;
11
12// define the function factorial recursively
13extern int factRec( int n ) ;
File example5.cc
1// standard I/O of C (libc)
2#include "example5.h"
3
4int
5main() {
6 int n = 4 ;
7 cout << "fact " << n << " = " << fact(n) << "\n" ;
8 cout << "factRec " << n << " = " << factRec(n) << "\n" ;
9 return 0 ; // return 0 to the OS
10}
File example5b.cc
1// standard I/O of C (libc)
2#include "example5.h"
3
4// define the function factorial
5int
6fact( int n ) {
7 int res = 1 ;
8 for ( int i = 2 ; i <= n ; ++i )
9 res *= i ;
10 return res ;
11}
12
13// define the function factorial recursively
14int
15factRec( int n ) {
16 if ( n <= 1 ) return 1 ;
17 return n* factRec(n-1) ;
18}