SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators +...
-
Upload
brisa-kale -
Category
Documents
-
view
221 -
download
0
Transcript of SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators +...
SEE C GOProvisional 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>
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;
}
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;
}
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 */
Your own (useless) function!
int add(int a, int b)
{
int c = 0;
c = a + b;
return c;
}
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;
}
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;
}
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;
}
Arrays
int numbers[10]; /* 10 ints */
int index = 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 */
char chVar = ‘a’;
/* This is a string */
char * pCharVar = “This is a string”;
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;
}
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)
typedef struct _foo
{
int someInt;
float someFloat;
double someDouble;
} foo;
foo foo1;
foo1.someInt = 10;
foo1.someFloat = 20.0;
foo2.someDouble = 30.0;
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;
}
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;
}
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);
}
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);
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;
}
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 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;
}