Http://
This presentation is the property of its rightful owner.
Sponsored Links
1 / 58

http:// proglit.com / PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on
  • Presentation posted in: General

http:// proglit.com /. the c language. SA. BY. 1972 by Ken Thompson and Dennis Ritchie. developed concurrently with Unix C++ and Objective-C in 1980’s ANSI C89, C90, and C99 GCC (Gnu Compiler Collection) MSVC (Microsoft Visual C++). http:// proglit.com /. imperative/procedural

Download Presentation

http:// proglit.com /

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Http proglit com

http://proglit.com/


Http proglit com

the c

language


Http proglit com

SA

BY


1972 by ken thompson and dennis ritchie

1972 by Ken Thompson and Dennis Ritchie


Http proglit com

developed concurrently with Unix

C++ and Objective-C in 1980’s

ANSI C89, C90, and C99

GCC (Gnu Compiler Collection)

MSVC (Microsoft Visual C++)


Http proglit com

http://proglit.com/


Http proglit com

  • imperative/procedural

  • statically typed

  • weakly typed


Control of memory

control of memory

explicitly manipulate individual bytes

“portable assembly”

compact data

manual allocation


Calling convention

(how functions are called in machine code)

calling convention

assembly from C

C from assembly


Systems programming

systems programming

performance-sensitive code

(operating systems, device drivers)

(games, media players)


Http proglit com

http://proglit.com/


Basic data types

basic data types

char1-byte signed integer

intn-byte signed integer

floatsingle-precision floating-point

doubledouble-precision floating-point


Declarations

declarations

typename;

int monkey;

float zebra;


Functions

functions

returnTypename(parameters) {body}


Int square int n return n n int cube int n return square n n

int square(int n)

{

return n * n;

}

int cube(int n)

{

return square(n) * n;

}


Char foo int a float b float c

char foo(int a, float b, float c)

{

}


Casting

casting

(type) expression

int x = 35;

foo((char) x);


Http proglit com

http://proglit.com/


0 1 1 0 0 6 0 3 0 76 5 0

!0// 1

!1// 0

!0.6// 0

!3// 0

!-76.5// 0


Http proglit com

void roger(int x)

{

if (x == 3) {

int z = 9;

foo(z);

} else {

bar(z); // z does not exist

}

ack(z); // z does not exist

}


Void roger int x int z 9 if x 3 foo z else bar z ack z

void roger(int x)

{

int z = 9;

if (x == 3) {

foo(z);

} else {

bar(z);

}

ack(z);

}


Int main printf hello world return 0

int main()

{

printf(“Hello, world!”);

return 0;

}


Http proglit com

http://proglit.com/


Value variables

value variables

  • float a = 9.3;


Pointers

pointers

(a data type representing an address)

int*foo;

double*bar;

char*ack;


Reference operator

reference operator

&lvalue

char c;

int*p;

p = &c; // error

inti;

int*p;

p = &i;


Reference operator1

reference operator

&lvalue

char c;

int*p;

p = (int*) &c;

inti;

int*p;

p = &i;


Dereference operator

dereference operator

*pointer

inti = 3;

int*p;

p = &i;

i = *p + 2;


Int i int p p i p 6

int i;

int*p;

p = &i;

*p = 6;


Http proglit com

http://proglit.com/


Pointer arithmetic

pointer arithmetic

inti;

int*p;

p = &i + 2;

i = *p;


Http proglit com

can subtracta pointer from a pointer

can’t adda pointer to a pointer


Char p p char 0xb000ffff char c p

char*p;

p = (char*) 0xB000FFFF;

char c = *p;


Int p p 0 if p false

int*p;

p = 0;

if (p) { … } // false


Pointer comparisons

pointer comparisons

  • == !=

  • > <

  • >= <=

  • !


Weak typing

(can violate data types)

weak typing

float f;

f = 98.6;

f = f – 2;

char*p;

p = (char*)&f + 2;

*p = 1;


Http proglit com

http://proglit.com/


Memory allocation

memory allocation

malloc

free


Void p p malloc 5 float f f malloc 13 free p free f

void*p;

p = malloc(5);

float*f;

f = malloc(13);

free(p);

free(f);


Sizeof operator

sizeof operator

sizeoftype

sizeof int

sizeof double

sizeof(float *)


Int p p malloc 7 sizeof int p 6 35 free p

int*p;

p = malloc(7 * sizeof(int));

*(p + 6) = 35;

free(p);


Int p p malloc 7 sizeof int if p 0 allocation failed else allocation succeeded free p

int*p;

p = malloc(7 * (sizeofint));

if (p == 0) {

… // allocation failed

} else {

… // allocation succeeded

free(p);

}


Http proglit com

http://proglit.com/


Array

(a stack-allocated contiguous block of memory)

array

typename[size];

float jack[8];

char jill[200];


Int i 32 char c 11 int p p i i 6 i 1 8 c 7 char 5

inti[32];

char c[11];

int *p;

p = i;

*i = -6;

*(i + 1) = 8;

*(c + 7) = (char) 5;


Http proglit com

int sum(int *arr, int n)

{

int i = 0;

int sum = 0;

while (i < n) {

sum = *(arr + i) + sum;

i = i + 1;

}

return sum;

}


Http proglit com

int a[30];

intsumA = sum(a, 30);

  • int *b = malloc(20 * sizeof(int));

  • intsumB = sum(b, 20);


Http proglit com

char *fred()

{

char c[30];

return c;

}


Http proglit com

char *fred()

{

  • char*c = malloc(30 * sizeof(char));

    return c;

    }

    char*foo = fred();

    free(foo);


Http proglit com

http://proglit.com/


Strings

strings

char*s = “abc”;

char b = *(s + 1);

int i = strlen(s);


Structure

(a programmer-defined compound data type)

structure

structtypeName{members};

structtypeName name;

instance.member


Http proglit com

structcat{

  • char*name;

    int age;

    }; // semi-colon!

struct cat mittens;

mittens.name = “Mittens”;

mittens.age = 5;


Http proglit com

struct cat mittens;

  • struct cat fluffy;

    mittens = fluffy;

    struct cat *p = &fluffy;

    mittens == fluffy // illegal


Http proglit com

struct cat cats[8];

(*cats).name = “Oscar”;

(*(cats + 1)).name = “Kitty”;


Http proglit com

struct cat *cats =

malloc(8 * (sizeofstructcat));


Http proglit com

http://proglit.com/


  • Login