Lesson of 7 July 2009¶
Some simple C programs
A minimal program
File example1.cc
1/*
2 A multiline comment.
3 */
4
5// a single line comment
6
7
8/*
9 A simple program that write "ciao mamma" to the console
10 */
11
12#include "myinclude.h"
13#include <stdio.h>
14
15int // the return type of the main function
16main() { // main is the entry point of the executable
17 printf("ciao mamma\n") ; // \n is the new line character
18 return 0 ; // exit from the program, all thing go well
19}
Example of types and keyrowds of C
File example2.cc
1/*
2 Introduction to the type of C
3 */
4
5#include <stdio.h>
6
7int // the return type of the main function
8main() { // main is the entry point of the executable
9 int a, b, c ; // declare the variable a, b and c as of integer type (normally 4 byte)
10 short int a1, b1, c1 ; // declare the variable a1, b1 and c1 as of short integer type (normally 2 byte)
11 long int a2, b2, c2 ; // declare the variable a2, b2 and c2 as of long integer type (normally 4 or 8 byte)
12
13 float C ; // declare C as a single precision variable (4 byte normally)
14 double D ; // declare D as a double precision variable (8 byte normally)
15
16 char a3, a4 ; // declare a3 and a4 as charater (1 byte)
17
18 unsigned short int aa, bb, cc ; // declare the variable aa, bb and cc
19 // as of short integer type without sign (normally 2 byte)
20
21 // the prefix short and long can be used to define the size of integer
22 // the prefix signed, unsigned can be used for int and char
23
24 ;;;;;; // ; is a separation characted ;; separate EMPTY instructions
25
26 printf("ciao mamma\n") ; // \n is the new line character
27
28 // some instruction for conditional execution
29 C = 1.0 ;
30 D = 1.3 ;
31 if ( C == D )
32 printf( "C and D are equal\n" );
33 else
34 printf( "C and D are NOT equal\n" );
35
36 if ( C = D ) printf( "C = D is true but it is really false!\n" ) ;
37
38 // some loop intruction
39 int i=10 ; // declare i as intger and initialize to 10
40 // while ( expr ) intruction ;
41 // when expr is different from 0 or true execute "intruction"
42 while ( --i ) printf( "%d\n", i ) ;
43 // --i means i = i-1 the operation is performed before the value of i is used
44 i = 10 ; // re-initialize i to 10
45 while ( i-- ) printf( "%d\n", i ) ;
46 // i-- means i = i-1 the operation is performed after the value of i is used
47
48
49 // for ( init ; expr ; incr ) instruction ;
50 // is equivalent to
51 // init ; // executed only one time at the beginning
52 // while ( expr ) { intruction ; incr }
53 //
54 // the
55 // while(expr) instr ;
56 // loop is equivalent to
57 // for ( ; expr ; ) instr ;
58 //
59 // infinite loop
60 // while (1) expr ;
61 // for (;;) expr ;
62
63 for ( int j = 1 ; j < 10 ; j *= 2 ) { // j *= 2 ==> j = j*2 ;
64 // in general res OP= expr ==> res = res OP expr
65 // where OP can be + - / * ~ | &
66 printf( "for loop, i = %10d\n", j) ;
67 }
68
69 // do { ... } while () is near the same of while loop
70 //
71 unsigned ii=10 ;
72 do {
73 printf( "do while loop i = %u\n", ii ) ;
74 } while ( --ii > 0 ) ;
75
76 // the last conditional instruction is switch
77 //
78 // switch ( integer expressin ) {
79 // case NUMBER:
80 // inst ;
81 // ....
82 // inst ;
83 // [ break ; ]
84 // case NUMBER:
85 // inst ;
86 // ....
87 // inst ;
88 // [ break ; ]
89 // ....
90 // [ default: ]
91 // inst ;
92 // ....
93 // inst ;
94 // [ break ; ]
95 // }
96 //
97 for ( i=0 ; i < 10 ; ++i ) {
98 switch ( i ) {
99 case 0: printf("pass 0, i=%d\n",i) ; break ;
100 default: printf("pass default, i=%d\n",i) ;
101 case 1: printf("pass 1, i=%d\n",i) ;
102 case 4: printf("pass 4, i=%d\n",i) ; break ;
103 case 5:
104 case 6:
105 case 7:
106 printf("pass 5,6,7, i=%d\n",i) ; break ;
107 }
108 }
109
110 return 0 ; // exit from the program, all thing go well
111}
Print first N numbers
File myinclude.h
1int pippo() ; // declare a body of a function