see c go provisional title. syntax types int, float, double, char, void identifiers foo operators +...

22
SEE C GO Provisional Title

Upload: brisa-kale

Post on 31-Mar-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

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

SEE C GOProvisional Title

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

Syntax• Types

• int, float, double, char, void

• Identifiers• foo

• Operators• + - * / ^

• Delimiters• ; {} () “” ‘’

• Keywords• return, struct, for, if, const, static

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

Hello World! a + b = c#include <stdio.h>

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;

}

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

More practical (but still useless) example#include <stdlib.h>

#include <stdio.h>

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;

}

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

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 */

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

Your own (useless) function!

int add(int a, int b)

{

int c = 0;

c = a + b;

return c;

}

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

Now to use it#include <stdio.h>

#include <stdlib.h>

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;

}

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

A further note on functions#include <stdio.h>

#include <stdlib.h>

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;

}

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

A further note on functions#include <stdio.h>#include <stdlib.h>

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;

}

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

Arrays

int numbers[10]; /* 10 ints */

int index = 0;

for(index = 0; index < 10; index++)

{

numbers[index] = index;

}

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

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);

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

More pointers!

/* This is a single character */

char chVar = ‘a’;

/* This is a string */

char * pCharVar = “This is a string”;

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

Pointer operationsint 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;

}

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

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

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

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;

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

Adder revisitedtypedef 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;

}

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

When pointers aren’t arraysint 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;

}

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

Another structtypedef 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);

}

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

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);

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

Final (maybe) version of add#include <stdio.h>

#include <stdlib.h>

/* 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;

}

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

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

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

A more subtle pointer mistakevoid 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;

}