pptx - code school

Report
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
• statically typed
• weakly typed
control of memory
• explicitly manipulate individual bytes
• “portable assembly”
• compact data
• manual allocation
calling convention
(how functions are called in machine code)
• assembly from C
• C from assembly
systems programming
(operating systems, device drivers)
performance-sensitive code
(games, media players)
http://proglit.com/
basic data types
char
int
float
point
double
1-byte signed integer
n-byte signed integer
single-precision floatingdouble-precision floating-point
declarations
type name;
int monkey;
float zebra;
functions
returnType name(parameters)
{body}
int square(int n)
{
return n * n;
}
int cube(int n)
{
return square(n)
* n;
}
char foo(int a, float b,
float c)
{
…
}
casting
(type) expression
int x = 35;
foo((char) x);
http://proglit.com/
!0
!1
!0.6
0
!3
!-76.5
// 1
// 0
//
// 0
// 0
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);
}
int main()
{
printf(“Hello,
world!”);
return 0;
}
http://proglit.com/
value variables
float a = 9.3;
pointers
(a data type representing an address)
int *foo;
double *bar;
char *ack;
reference operator
&lvalue
int i;
int *p;
p = &i;
char c;
int *p;
p = &c;
error
//
reference operator
&lvalue
int i;
int *p;
p = &i;
char c;
int *p;
p = (int *) &c;
dereference operator
*pointer
int
int
p =
i =
i = 3;
*p;
&i;
*p + 2;
int i;
int
*p;
p =
&i;
*p =
6;
http://proglit.com/
pointer arithmetic
int
int
p =
2;
i =
i;
*p;
&i +
*p;
can subtract a pointer from a pointer
can’t add a pointer to a pointer
char *p;
p = (char *)
0xB000FFFF;
char c = *p;
int *p;
p = 0;
if (p) { … }
false
//
pointer comparisons
== !=
> <
>= <=
!
weak typing
(can violate data types)
float f;
f = 98.6;
f = f – 2;
char *p;
p = (char *)&f
+ 2;
*p = 1;
http://proglit.com/
memory allocation
• malloc
• free
void *p;
p = malloc(5);
float *f;
f =
malloc(13);
…
free(p);
free(f);
sizeof operator
sizeof type
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));
if (p == 0) {
… // allocation failed
} else {
… // allocation
succeeded
free(p);
http://proglit.com/
array
(a stack-allocated contiguous block of memory)
type name[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;
int sum(int *arr, int n)
{
int i = 0;
int sum = 0;
while (i < n) {
sum = *(arr + i) +
sum;
i = i + 1;
}
return sum;
}
int a[30];
…
int sumA = sum(a, 30);
int *b = malloc(20 *
sizeof(int));
…
int sumB = sum(b, 20);
char *fred()
{
char c[30];
return c;
}
char *fred()
{
char *c = malloc(30 *
sizeof(char));
return c;
}
char *foo = fred();
…
free(foo);
http://proglit.com/
strings
char *s =
“abc”;
char b = *(s +
1);
int i =
structure
(a programmer-defined compound data type)
struct typeName {members};
struct typeName name;
instance.member
struct cat {
char *name;
int age;
}; // semi-colon!
struct cat mittens;
mittens.name =
“Mittens”;
mittens.age = 5;
struct cat mittens;
struct cat fluffy;
…
mittens = fluffy;
struct cat *p = &fluffy;
mittens == fluffy
illegal
//
struct cat cats[8];
(*cats).name = “Oscar”;
(*(cats + 1)).name =
“Kitty”;
struct cat *cats =
malloc(8 * (sizeof struct
cat));
http://proglit.com/

similar documents