c programming revision malcolm wilson. variables types int, char, double, long. no type for string...
TRANSCRIPT
![Page 1: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/1.jpg)
C Programming Revision
Malcolm Wilson
![Page 2: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/2.jpg)
Variables
• Types int, char, double, long. NO type for string see later.
• unsigned above.• assignment
X=2 ;C=‘v’;
![Page 3: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/3.jpg)
Keywords
• C has a small number of “keywords”
• http://tigcc.ticalc.org/doc/keywords.html
![Page 4: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/4.jpg)
Standard I/O
• printf()
• scanf()
• Format specifiers %d, %f, %c, %s
http://en.wikipedia.org/wiki/Printfhttp://en.wikipedia.org/wiki/Scanf
![Page 5: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/5.jpg)
Operations
• +, -, *, /, %, ^• Operation dependent on variable type• Try some
![Page 6: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/6.jpg)
Boolean
• Any value other than zero is true.• Watch out for == “is equal to”.
![Page 7: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/7.jpg)
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}
![Page 8: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/8.jpg)
Control
switch( myvar)case 1 :
{code blockbreak;}
case 2:{code blockbreak;}
default{code block}
http://msdn.microsoft.com/en-us/library/66k51h7a%28VS.80%29.aspx
![Page 9: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/9.jpg)
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);
![Page 10: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/10.jpg)
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) ;
![Page 11: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/11.jpg)
Scope and storage class
• Used for AVR• Static ,will remain even after function has
exited.• Global• Volatile , can be changed by unpredicable
actions.
![Page 12: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/12.jpg)
Preprocessor directives
• #include– “localfile”– <standard_locations> /usr/include
• #define– #define WIDTH 80 – #define LENGTH ( WIDTH + 10 ) – #define u8 unsigned char
![Page 13: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/13.jpg)
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.
![Page 14: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/14.jpg)
Structures
struct struct_name { structure_member; ... } instance_1,instance_2 instance_n;ORstruct struct_name
instance_1,instance_2 ,instance3 After defining the structure.http://cprogramminglanguage.net/c-structure.aspx
![Page 15: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/15.jpg)
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;
![Page 16: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/16.jpg)
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.
![Page 17: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/17.jpg)
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’;
![Page 18: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/18.jpg)
Dynamic memory allocation
• Allocates memory on the “heap”• malloc(n)• calloc(s, nbytes) intialises memory• free();
![Page 19: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/19.jpg)
sizeof()
• Used for malloc to allocate memory
![Page 20: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/20.jpg)
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);}
![Page 21: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/21.jpg)
argv and argc
• int main (int argc, char *argv[]) { }
• argc , argument count• argv , argument vector
![Page 22: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/22.jpg)
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}
![Page 23: C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;](https://reader036.vdocuments.site/reader036/viewer/2022062322/56649cf45503460f949c2e12/html5/thumbnails/23.jpg)
Deep C
• Lvalues, Rvalues• Inline functions• Pointers to functions• Pointers to pointers and multidimensional
arrays.