1 / 16

CSI2172 site.uottawa/~fbinard/Teaching/CSI2172_SUMMER2006/

CSI2172 http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_SUMMER2006/. 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++. Start with C. #include <stdio.h>

reia
Download Presentation

CSI2172 site.uottawa/~fbinard/Teaching/CSI2172_SUMMER2006/

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. CSI2172http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_SUMMER2006/CSI2172http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_SUMMER2006/

  2. Bjarne: • Bjarne Stroustrup is the guy who inventedC++ • 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. Start with C #include <stdio.h> /* my first C program */ int main() { printf("Hello World\n"); return 0; } Library Function result. Typed Output Result

  4. #include <stdio.h> #include <stdlib.h> #include <iomanip.h> 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() • There is one and only one main(…) • main(…) can be anywhere • All program begin where main(…) begins and end where main(…) ends • 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. char • int • float • double Types : • char: 1 byte • int: signed integer • float: real number. 6 digits after decimal. • double: real number. 10 digits after decimal • 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 • short, long • signed, unsigned

  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 • '\a' keyboard bell • '\\' back slash • '\b' back space • '\?' question mark • '\f' form feed • '\n' new line • '\"' double quote • '\r' carriage return • '\t' horizontal tab • '\v' vertical tab • '\ooo' octal byte • '\xhh' hex byte • '\0' null byte • char • (default: signed) • signed: -128 to 127 • unsigned: 0 to 255

  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 • 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 <VariableName> [<Length>]; Examples: char name[20]; char firstName[20]; char sentence[300]; • "a\tb" --> a b • "abcd\b\bx" --> abx • "\"hello world\"" --> "hello world" • "I don\'t know" --> I don't know • "hello\nworld" --> hello world • "\a" --> (rings the keyboard bell) 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; }; n x y 1 2 3 2 2 3 3 0 3 4 2 4 5 2 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. goto(rare) void f() { ... for( ... ) { while( ... ) { ... if (wrong) goto error; ... } for( ... ) { if (wrong) goto error; ... } } ... error: ... 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 < 0 || choice > 4) break; switch(choice) { case 1: ... }}

More Related