Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,

Similar presentations


Presentation on theme: "SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,"— Presentation transcript:

1 SEE C GO Provisional Title

2 Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return, struct, for, if, const, static

3 Hello World! a + b = c #include int main() { /* This is a comment */ int a = 1; int b = 2; int c; c = a + b; printf(“%d + %d = %d\n”, a, b, c); return 0; }

4 More practical (but still useless) example #include int main() { /* Declare a bunch of variables */ int r = 0; int index = 0; /* Call a function to seed random number generator */ srand(10); for(index = 0; index < 10; index++) { r = rand() % 10; printf(“Random number %d is ”, r); if(r % 2) printf(“odd.\n”); else printf(“even.\n”); } return 0; }

5 A word on scope int index = 0; int outside = 0; for(index = 0; index < 10; index++) { int inside = index + 1; outside = inside; } printf(“%d\n”, inside); /* this will fail */ printf(“%d\n”, outside); /* this will work */

6 Your own (useless) function! int add(int a, int b) { int c = 0; c = a + b; return c; }

7 Now to use it #include int add(int a, int b) { int c = 0; c = a + b; return c; } int main() { /* Comments are your friend! */ int a,b,c = 0; a = rand(); b = rand(); c = add(a, b); printf(“%d + %d = %d\n”, a, b, c); return 0; }

8 A further note on functions #include int main() { /* Comments are your friend! */ int a,b,c = 0; a = rand(); b = rand(); c = add(a, b); /* Will this work? */ printf(“%d + %d = %d\n”, a, b, c); return 0; } int add(int a, int b) { int c = 0; c = a + b; return c; }

9 A further note on functions #include int add(int a, int b); int main() { /* Comments are your friend! */ int a,b,c = 0; a = rand(); b = rand(); c = add(a, b); /* Will this work? */ printf(“%d + %d = %d\n”, a, b, c); return 0; } int add(int a, int b) { int c = 0; c = a + b; return c; }

10 Arrays int numbers[10]; /* 10 ints */ int index = 0; for(index = 0; index < 10; index++) { numbers[index] = index; }

11 Pointer Purgatory Special meaning of * and & int a = 0; int * b; a = 10; b = &a; printf(“%d\n”, a); printf(“%d\n”, b); printf(“%d\n”, *b); printf(“%d\n”, &a);

12 More pointers! /* This is a single character */ char chVar = ‘a’; /* This is a string */ char * pCharVar = “This is a string”;

13 Pointer operations int main() { char chVar = ‘a’; char * pCharVar = “This is a string”; int index = 0; for(index = 0; index < strlen(pCharVar); index++) { printf(“%c\n”, pCharVar[index]); /* Pay attention here */ printf(“%s\n”, pCharVar + index); } return 0; }

14 Arrays and Pointers Pointers and arrays are often interchangeable char hello[6] = “Hello”; char * hello = “Hello”; char hello[] = “Hello”; hello[0] is H for all of them Don’t do hello[6] if you don’t want to crash your program

15 Structs (C89 style) typedef struct _foo { int someInt; float someFloat; double someDouble; } foo; foo foo1; foo1.someInt = 10; foo1.someFloat = 20.0; foo2.someDouble = 30.0;

16 Adder revisited typedef struct _input { int a; in b; } input; int add(input in) { int c = in.a + in.b; return c; } int main() { int c; input in1; in1.a = 5; in1.b = 10; c = add(in1); return c; }

17 When pointers aren’t arrays int add(input * in) { int c; c = in->a + in->b; return c; } int main() { int c; input in; in.a = 5; in.b = 10; c = add(&in); printf(“%d + %d = %d\n”, in.a, in.b, c); return 0; }

18 Another struct typedef struct _input { int a; int b; int c; } input; void add(input * inout) { inout->c = inout->a + inout->b; } int main() { input in; in.a = 5; in.b = 10; add(&in); printf(“%d\n”, in.c); }

19 Dynamic Memory malloc: allocate chunks of memory void * malloc(size_t bytes); input * in = (input*)malloc(sizeof(input)); free: free chunks of memory void free(void * pointer); free(in);

20 Final (maybe) version of add #include /* Using the input struct defined above */ int main() { input * in = NULL; int c; in = (input*)malloc(sizeof(input)); in->a = 5; in->b = 10; printf(“%d + %d = %d\n”, in->a, in->b, in->c); free(in); in = NULL; return 0; }

21 Common dynamic memory mistakes Forgetting to free allocated memory Using a pointer after the memory has been freed Double freeing a pointer Accessing a pointer that points to NULL

22 A more subtle pointer mistake void add(input * in) { in->a = 10; /* This is usually bad */ in->c = in->a + in->b; } /* const keyword will prevent anyone from modifying the contents of a pointer or value */ int add(const input * in) { int c; in->a = 10; /* this would not compile */ c = in->a + in->b; return c; }


Download ppt "SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,"

Similar presentations


Ads by Google