Title: C workshop Yuli Kaplunovsky - yuli@magniel.com Today - Introduction to C
1C workshopYuli Kaplunovsky -
yuli_at_magniel.comToday - Introduction to C
Recommended book The C programming Language /
Kernighan Ritchie
2My first program
- include ltstdio.hgt
- void main()
-
- printf("Hello World!\n")
-
Output Hello World!
3(No Transcript)
4(No Transcript)
5(No Transcript)
6(No Transcript)
7(No Transcript)
8(No Transcript)
9(No Transcript)
10(No Transcript)
11(No Transcript)
12C structure
- Function oriented (goto is not recommended)
- First function is always called main
- Contains many libraries (e.g. stdio.h, stdlib.h,
math.h) with many predefined functions. - CaSe SeNsItIvE (e.g. Main instead of main
wont work) - ALWAYS USE
- Indentation
- Meaningful names for functions and variables
- Plenty of remarks
13Variables
- int an integer number maximum value is
2,147,483,647 (or 231) minimum value is
-2,147,483,648 - double real number, represented as floating
point (64 bits long) - char represents a single character
14Variables sample 1
- include ltstdio.hgt
- void main()
-
- int I,J,K
- I 10
- J 20
- K I J
- printf("K is d\n", K)
Output K is 30
15Variable sample 2
- include ltstdio.hgt
- void main()
-
- double X,Y,Z
- X 10.0
- Y 20.0
- Z X / Y
- printf("Z is g\n", Z)
Output Z is 0.5
16while
- include ltstdio.hgt
- / Print Fahrenheit-Celsius table
- for fahr 0, 20, .., 300 /
- void main()
-
- int fahr, celsius
- int lower, upper, step
- lower 0 / lower limit of temerature
table / - upper 300 / upper limit /
- step 20 / step size /
- fahr lower
- while ( fahr lt upper )
- celsius 5 (fahr - 32) / 9
- printf("d\td\n", fahr, celsius )
- fahr fahr step
Output 0 -17 20 -6 40 4 60
15 80 26 100 37 120 48 140
60 160 71 180 82 200 93 220
104 240 115 260 126 280 137 300
148
17for
- Syntax for ( initialization condition do )
block - include ltstdio.hgt
- void main()
-
- int I
- printf("I ")
- for ( I 0 I lt 10 I )
- printf("d, ", I )
- printf("\n")
-
Output I 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
18for - another example
Output 0 -17.7778 20 -6.66667 40
4.44444 60 15.5556 80 26.6667 100
37.7778 120 48.8889 140 60 160 71.1111 180
82.2222 200 93.3333 220 104.444 240
115.556 260 126.667 280 137.778 300 148.889
- include ltstdio.hgt
- void main()
-
- int fahr
-
- for ( fahr 0 fahr lt 300 fahr fahr
20 ) - printf("d g\n", fahr, (5.0 / 9.0)
(fahr - 32) ) -
19if
- if ( condition ) block 1 else
block 2 (optional) - Exampleif ( Y gt X ) Z Xelse Z Y
- For comparisons use gt lt lt gt
- Important remark a block should be surrounded
with if it contains more than one command.
20If - multiple conditions
- if ( (condition1 condition2 ) condition3)
... - Examplesif ( Y gt X Y gt Z ) Z Xint
bTerminate if ( I 10 bTerminate )
break - break is used to get out of for while loops
- condition2 is FALSE when bTerminate is 0
and is TRUE when bTerminate is NOT 0
21Functions
- Return-value function-name( parameters )
return value - Calling the functionI function-name( 10, 20 )
22Function example
- include ltstdio.hgt
- int Add2( int A, int B )
-
- int C
- C A B
- return C
-
- void main()
-
- int I
- I Add2( 10, 20 )
- printf("Add2 function returns d\n", I)
-
Output Add2 function returns 30
23Arrays
- ALWAYS start from 0Last item is N-1
- Exampleint Ar10Ar0 22Ar9 Ar0
22I 4 ArI ArI1
24- include ltstdio.hgt
- // Guess what this program does...
- void main()
-
- double X10 2, 4.2, 11.2, 3, 99.2,
-23.2, 33, 11, 43, 9 - double Y
- int I, J
- for ( I 0 I lt 9 I )
- for ( J I1 J lt 10 J )
- if ( XI gt XJ ) // Switch
variables in array - Y XI
- XI XJ
- XJ Y
-
-
-
- // print results
- for ( I 0 I lt 10 I )
- printf("g, ", XI)
Output -23.2, 2, 3, 4.2, 9, 11, 11.2, 33, 43,
99.2,