1 / 22

See C Go

See C Go. Provisional Title. Syntax. Types int , float, double, char, void Identifiers f oo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return, struct , for, if, const , static. Hello World! a + b = c. #include < stdio.h > int main() { /* This is a comment */

gudrun
Download Presentation

See C Go

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. 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 <stdio.h> intmain() { /* This is a comment */ inta = 1; intb = 2; int c; c = a + b; printf(“%d + %d = %d\n”, a, b, c); return 0; }

  4. More practical (but still useless) example #include <stdlib.h> #include<stdio.h> int main() { /* Declare a bunch of variables */ int r = 0; intindex = 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 intindex = 0; intoutside = 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! intadd(int a, int b) { intc = 0; c = a + b; return c; }

  7. Now to use it #include <stdio.h> #include <stdlib.h> intadd(int a, int b) { intc = 0; c = a + b; return c; } intmain() { /* Comments are your friend! */ inta ,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 <stdio.h> #include <stdlib.h> intmain() { /* Comments are your friend! */ inta ,b ,c = 0; a = rand(); b = rand(); c=add(a, b); /* Will this work? */ printf(“%d + %d = %d\n”, a, b, c); return 0; } intadd(int a, int b) { intc = 0; c = a + b; return c; }

  9. A further note on functions #include <stdio.h> #include <stdlib.h> intadd(int a, int b); intmain() { /* Comments are your friend! */ inta ,b ,c = 0; a = rand(); b = rand(); c=add(a, b); /* Will this work? */ printf(“%d + %d = %d\n”, a, b, c); return 0; } intadd(int a, int b) { intc = 0; c = a + b; return c; }

  10. Arrays intnumbers[10]; /* 10 ints */ intindex = 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 */ charchVar = ‘a’; /* This is a string */ char * pCharVar = “This is a string”;

  13. Pointer operations intmain() { charchVar = ‘a’; char * pCharVar = “This is a string”; intindex = 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) typedefstruct_foo { intsomeInt; floatsomeFloat; doublesomeDouble; } foo; foo foo1; foo1.someInt = 10; foo1.someFloat = 20.0; foo2.someDouble = 30.0;

  16. Adder revisited typedefstruct _input { int a; in b; } input; int add(input in) { intc = in.a + in.b; return c; } intmain() { intc; input in1; in1.a = 5; in1.b = 10; c = add(in1); return c; }

  17. When pointers aren’t arrays intadd(input * in) { intc; c = in->a + in->b; return c; } intmain() { intc; 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 typedefstruct _input { inta; intb; intc; } input; void add(input * inout) { inout->c = inout->a + inout->b; } intmain() { 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_tbytes); • input * in = (input*)malloc(sizeof(input)); • free: free chunks of memory • void free(void * pointer); • free(in);

  20. Final (maybe) version of add #include <stdio.h> #include <stdlib.h> /* Using the input struct defined above */ intmain() { 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 */ intadd(const input * in) { int c; in->a = 10; /* this would not compile */ c = in->a + in->b; return c; }

More Related