Download presentation
Presentation is loading. Please wait.
1
C workshop Yuli Kaplunovsky - yuli@magniel.com Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie
2
My first program #include void main() { printf("Hello World!\n"); } Output: Hello World!
12
C 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’ won’t work) ALWAYS USE: –Indentation –Meaningful names for functions and variables –Plenty of remarks
13
Variables int – an integer number maximum value is 2,147,483,647 (or 2^31) minimum value is -2,147,483,648 double – real number, represented as floating point (64 bits long) char – represents a single character
14
Variables sample #1 #include void main() { int I,J,K; I = 10; J = 20; K = I + J; printf("K is %d\n", K); } Output: K is 30
15
Variable sample #2 #include 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
16
while #include /* 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 <= upper ) { celsius = 5 * (fahr - 32) / 9; printf("%d\t%d\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
17
for Syntax: for ( initialization ; condition ; do ) { block } #include void main() { int I; printf("I = "); for ( I = 0 ; I < 10 ; I++ ) printf("%d, ", I ); printf("\n"); } Output: I = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
18
for - another example #include void main() { int fahr; for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20 ) printf("%d %g\n", fahr, (5.0 / 9.0) * (fahr - 32) ); } 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
19
if if ( condition ) { block #1 } else { block #2 } (optional) Example: if ( Y > X ) Z = X; else Z = Y; For comparisons use: > = == Important remark: a block should be surrounded with {} if it contains more than one command.
20
If - multiple conditions if ( (condition1 || condition2 ) && condition3)... Examples: if ( Y > X && Y > Z ) Z = X; int 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
21
Functions Return-value function-name( parameters ) { … return value; } Calling the function: I = function-name( 10, 20 );
22
Function example #include 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
23
Arrays ALWAYS start from 0 Last item is N-1 Example: int Ar[10]; Ar[0] = 22; Ar[9] = Ar[0] + 22; I = 4; Ar[I] = Ar[I+1];
24
#include // Guess what this program does... void main() { double X[10] = { 2, 4.2, 11.2, 3, 99.2, -23.2, 33, 11, 43, 9 }; double Y; int I, J; for ( I = 0 ; I < 9 ; I++ ) { for ( J = I+1 ; J < 10 ; J++ ) { if ( X[I] > X[J] ) { // Switch variables in array Y = X[I]; X[I] = X[J]; X[J] = Y; } // print results for ( I = 0 ; I < 10 ; I++ ) printf("%g, ", X[I]); } Output: -23.2, 2, 3, 4.2, 9, 11, 11.2, 33, 43, 99.2,
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.