220 likes | 354 Views
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 */
E N D
See C Go Provisional Title
Syntax • Types • int, float, double, char, void • Identifiers • foo • Operators • + - * / ^ • Delimiters • ; {} () “” ‘’ • Keywords • return, struct, for, if, const, static
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; }
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; }
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 */
Your own (useless) function! intadd(int a, int b) { intc = 0; c = a + b; return c; }
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; }
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; }
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; }
Arrays intnumbers[10]; /* 10 ints */ intindex = 0; for(index = 0; index < 10; index++) { numbers[index] = index; }
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);
More pointers! /* This is a single character */ charchVar = ‘a’; /* This is a string */ char * pCharVar = “This is a string”;
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; }
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
Structs (C89 style) typedefstruct_foo { intsomeInt; floatsomeFloat; doublesomeDouble; } foo; foo foo1; foo1.someInt = 10; foo1.someFloat = 20.0; foo2.someDouble = 30.0;
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; }
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; }
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); }
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);
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; }
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
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; }