1 / 24

C workshop Yuli Kaplunovsky - yuli@magniel Today - Introduction to C

C workshop Yuli Kaplunovsky - yuli@magniel.com Today - Introduction to C. Recommended book: The C programming Language / Kernighan &amp; Ritchie. My first program. #include &lt;stdio.h&gt; void main() { printf(&quot;Hello World!<br>&quot;); }. Output: Hello World!. C structure.

yestin
Download Presentation

C workshop Yuli Kaplunovsky - yuli@magniel Today - Introduction to C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C workshopYuli Kaplunovsky - yuli@magniel.comToday - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie

  2. My first program #include <stdio.h> void main() { printf("Hello World!\n"); } Output: Hello World!

  3. 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

  4. 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

  5. Variables sample #1 #include <stdio.h> void main() { int I,J,K; I = 10; J = 20; K = I + J; printf("K is %d\n", K); } Output: K is 30

  6. Variable sample #2 #include <stdio.h> 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

  7. while #include <stdio.h> /* 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

  8. for • Syntax: for ( initialization ; condition ; do ) { block } #include <stdio.h> 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,

  9. for - 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 <stdio.h> void main() { int fahr; for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20 ) printf("%d %g\n", fahr, (5.0 / 9.0) * (fahr - 32) ); }

  10. 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.

  11. 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

  12. Functions • Return-value function-name( parameters ) { … returnvalue; } • Calling the function:I = function-name( 10, 20 );

  13. Function example #include <stdio.h> 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

  14. Arrays • ALWAYS start from 0Last item is N-1 • Example:int Ar[10];Ar[0] = 22;Ar[9] = Ar[0] + 22;I = 4; Ar[I] = Ar[I+1];

  15. #include <stdio.h> // 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,

More Related