C Programming Revision

advertisement
C Programming Revision
Malcolm Wilson
Variables
• Types int, char, double, long. NO type for
string see later.
• unsigned above.
• assignment
X=2 ;
C=‘v’;
Keywords
• C has a small number of “keywords”
• http://tigcc.ticalc.org/doc/keywords.html
Standard I/O
• printf()
• scanf()
• Format specifiers %d, %f, %c, %s
http://en.wikipedia.org/wiki/Printf
http://en.wikipedia.org/wiki/Scanf
Operations
• +, -, *, /, %, ^
• Operation dependent on variable type
• Try some
Boolean
• Any value other than zero is true.
• Watch out for == “is equal to”.
Control
for(i=0, i<9, i++)
{ code block}
while (x<8)
{code block}
if (y==2)
{code block}
elseif(y==7)
{code block}
else
{code block}
Control
switch( myvar)
case 1 :
{code block
break;}
case 2:
{code block
break;}
default
{code block}
http://msdn.microsoft.com/enus/library/66k51h7a%28VS.80%29.aspx
Control
Code block is surrounded by {} if more than one
line. Don’t need {} if code one line long.
eg for(i=0; i<5; i++)
printf(“ number is: %d /n”, i);
Functions and Prototypes
• C is composed of functions, and must have at
least one function called main().
• Functions accept parameters and return
values
• A “prototype” should be written which
indicates what data types a function should
accept and return.
– Eg int mynumberfunction( int num1, int num2) ;
Scope and storage class
• Used for AVR
• Static ,will remain even after function has
exited.
• Global
• Volatile , can be changed by unpredicable
actions.
Preprocessor directives
• #include
– “localfile”
– <standard_locations> /usr/include
• #define
– #define WIDTH 80
– #define LENGTH ( WIDTH + 10 )
– #define u8 unsigned char
Arrays and strings
•
•
•
•
•
int myarray[5];
int myarray[5]={1, 2, 3};
int myarray[]={1,2,3,4,5};
char mychararray=“malcolm”;
A string is a “null terminated” char array.
Structures
struct struct_name {
structure_member;
...
} instance_1,instance_2 instance_n;
OR
struct struct_name instance_1,instance_2
,instance3
After defining the structure.
http://cprogramminglanguage.net/c-structure.aspx
Structures
• Using typedef to avoid struct structurename all the time.
• typedef struct{
unsigned int house_number;
char street_name[50];
int zip_code;
char country[50];
} address;
address billing_addr;
address shipping_addr;
Pointers
• Declared as using *
• int *p says p in a pointer to an integer.
• p points to the memory location where a
integer is stored.
• Confusing , in the code. *p means the
contents of memory location p.
• And &p is the memory address of p.
Pointers and arrays
• myarray is the same as &myarray[0]
• So if an array is initialised as char
name[]=“malcolm”;
• *(name+3) will be ‘c’;
Dynamic memory allocation
•
•
•
•
Allocates memory on the “heap”
malloc(n)
calloc(s, nbytes) intialises memory
free();
sizeof()
• Used for malloc to allocate memory
Pointers and structures
#include <stdio.h>
#include <stdlib.h>
main()
{
• printf("hello world \n");
• struct mystruct{
• int age;
• char buffer[20];
• }mydata;
• mydata.age=45;
• printf("age is %d \n", mydata.age);
•
•
•
•
}
struct mystruct * d;
d=malloc(sizeof(struct mystruct));
d->age=53;
printf("pointed age is %d \n", d->age);
argv and argc
• int main (int argc, char *argv[]) { }
• argc , argument count
• argv , argument vector
Boo Boo’s in C
• Forgetting the semicolon
• Using = in a boolean expression instead of ==.
• Completing a loop with no code being
executed.
– while(test);
{ code block}
Deep C
•
•
•
•
Lvalues, Rvalues
Inline functions
Pointers to functions
Pointers to pointers and multidimensional
arrays.
Download