Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI2172

Similar presentations


Presentation on theme: "CSI2172"— Presentation transcript:

1 CSI2172 http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_SUMMER2006/

2 Bjarne: Bjarne Stroustrup is the guy who invented C++ C++ means: “a C incremented”. It’s a clever way to say “C 2.0” If it works in C, it’s easy to make it work in C++

3 #include /* my first C program */ int main() { printf("Hello World\n"); return 0; } Start with C Library Function result. Typed Output Result

4 #include double func1(char, int); int func2(double, float); int main() { printf(“Value is %d”, func2(func1(‘a’, 2), 5.2f); return 0; } double func1(char a, int b){return a+b;} int func2(double a, float b){return a-b;}

5 The Tao of main() E There is one and only one main(…) E main(…) can be anywhere E All program begin where main(…) begins and end where main(…) ends E main(…) also returns

6 Variables: int a, b = 2; char x, y = 'a'; unsigned long int i1 = 0, i2 = 3; double pi = 3.14; double d = 5*pi;

7 Types : char int float double E char: 1 byte E int: signed integer E float: real number. 6 digits after decimal. E double: real number. 10 digits after decimal short, long signed, unsigned  Exemples: char (signed char), unsigned char, short int (signed short int), unsigned short int, int (signed int), unsigned int, long int (signed long int), unsigned long int, float, double, long double

8 More than you want to know about the int type int (default: signed short) short: 2 bytes (16 bits) signed: -32768 to 32767 unsigned: 0 to 65535 long: 4 bytes (32 bit) signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 In general: sizeof(short) <= sizeof(int) <= sizeof(long)

9 Some of my favorite chars char (default: signed) signed: -128 to 127 unsigned: 0 to 255 C '\a' keyboard bell C '\\' back slash C '\b' back space C '\?' question mark C '\f' form feed C '\n' new line C '\"' double quote C '\r' carriage return C '\t' horizontal tab C '\v' vertical tab C '\ooo' octal byte C '\xhh' hex byte C '\0' null byte

10 array int a[12]; //Array of 12 int char a[]; //Array of char, sized on initialization a = “hjah”; //” = short hand for NULL terminated array of chars, 5 elements float a[5][4]; //Matrix. Array of pointers Arrays are indexed from 0 to n-1 where n is the number of elements in the array /*INITIALISATION D'1 TABLEAU */ #define ROW 2 #define COLUMN 3 int main(){ int n[100], ctr; for(ctr = 0; ctr < 99; ctr++){ n[ctr] = 0; } int w[5] = {32, 21, 56, 32, 45}; char a[] = "Hello"; char b[6] = {'H','e','l','l','o','\0'}; int mat[ROW][COLUMN] = {{1,2}, {2,6}, {5,7}}; mat[0][2] = 3; }

11 strings H "a\tb" --> ab H "abcd\b\bx" --> abx H "\"hello world\"" --> "hello world" H "I don\'t know" --> I don't know H "hello\nworld" --> hello world H "\a" --> (rings the keyboard bell) There isn’t any string type in C. Strings are just arrays of char. Then there is just a bit of built-in syntactic sugar and some conventions. char [ ]; Examples: char name[20]; char firstName[20]; char sentence[300]; The last symbol is always '\0' (NULL). So for n characters, you need n+1 bytes.

12 /* Class example. Create an array of 11 chars, fill it with letters from ‘b’ to ‘j’ and print it to the output as a string */ char a[11]; a[10] = 'a'-'a'; for(int ctr=0; ctr < 10; ctr++){ a[ctr] = 'b'+ ctr; } printf("%s", a);

13 Decision,decision… if (...) stmt if (...) stmt1 else stmt2 if (...) { body } else { body } if (1)... true if (2)... true if (-1.5)... true if (0)... false int x = 2, y; if (x < 3) y = 5; else y = 4; The odd guy: cond ? e1 : e2 ----------------------------------- double x = n % 2 == 0 ? 4.3 : -2.3;

14 Branching(++): int x = 0, y = 0; switch(n) { case 1: case 2: x = 2; case 3: y = 3; break; case 4: x = 1; y = 4; case 5: x = 2; break; default: y = -1; }; nx y 1 2 3 2 2 3 3 0 3 4 2 4 52 0 >5 0 -1

15 Repeat after me: for(e1;e2;e3)... int f=1, i; for(i=2; i<=n; ++i) f = f *i; for(;;)... while(cond)... int f=1, i=2; while(i <= n) f = f * i++; do body while(cond); int f=1, i = 1; do { f = f * i++; } while(i <= n);

16 Take me outta here break & continue int i, r = 0; for(i=0; i<n; ++i) { if (r % 2 == 0) continue; r += i; } int choice = 0; while(1) { choice = user_input(); if (choice 4) break; switch(choice) { case 1:...} goto(rare) void f() {... for(... ) { while(... ) {... if (wrong) goto error;... } for(... ) { if (wrong) goto error;... } }... error:...


Download ppt "CSI2172"

Similar presentations


Ads by Google