Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction to C Programming
Why C?
C is a simple language, makes it easier to focus on
important concepts
Java is a high level, object oriented language, C is a lower
level , imperative language, important to learn both
Lays groundwork for many other languages
Function Definitions
Functions look a lot like methods you are used to in Java,
but are not part of a class:
return-type function-name(arguments) {
variable-declarations; /* Must come first!! */
function-body;
}
double d;
long test[100];
test[0] = 2;
/* OK command */
test[99] = 3;
/* OK command */
test[100] = 4;
/* ERROR !!!*/
No bounds checking in C!
double avg;
Example on Board
Structures
Structures are like classes that have only public data
fields and no methods:
struct Gate {
int
type;
struct Gate *left;
struct Gate *rght;
};
/* Type of gate. */
/* Left input. */
/* Right input. */
Pointers
Pointers in C are explicit (implicit in Java)
A pointer is simply a value that can refer to another
location in memory
int *pVal; /* Pointer (uninitialized) to unallocated integer value. */
pVal = 0;
/* Initialize pointer to indicate that it is not allocated. */
.
.
Pointer
.
pVal
.
.
/* Allocate unassigned integer and */
/* assign memory address to pVal. */
pVal = (int *)malloc(sizeof(int));
*pVal = 42; /* Assign value to integer pointed to by pVal. */
Pointers
Pointers in C are explicit (implicit in Java)
A pointer is simply a value that can refer to another
location in memory
int *pVal; /* Pointer (uninitialized) to unallocated integer value. */
pVal = 0;
/* Initialize pointer to indicate that it is not allocated. */
.
.
Pointer
.
pVal
.
.
Indicates a
/* Allocate unassigned integer and */
null pointer.
/* assign memory address to pVal. */
pVal = (int *)malloc(sizeof(int));
*pVal = 42; /* Assign value to integer pointed to by pVal. */
Pointers
Pointers in C are explicit (implicit in Java)
A pointer is simply a value that can refer to another
location in memory
int *pVal; /* Pointer (uninitialized) to unallocated integer value. */
pVal = 0;
/* Initialize pointer to indicate that it is not allocated. */
.
.
Pointer
.
pVal
.
.
Value
/* Allocate unassigned integer and */
???
/* assign memory address to pVal. */
pVal = (int *)malloc(sizeof(int));
*pVal = 42; /* Assign value to integer pointed to by pVal. */
Pointers
Pointers in C are explicit (implicit in Java)
A pointer is simply a value that can refer to another
location in memory
int *pVal; /* Pointer (uninitialized) to unallocated integer value. */
pVal = 0;
/* Initialize pointer to indicate that it is not allocated. */
.
.
Pointer
.
pVal
.
.
Value
/* Allocate unassigned integer and */
42
/* assign memory address to pVal. */
pVal = (int *)malloc(sizeof(int));
*pVal = 42; /* Assign value to integer pointed to by pVal. */
42
Pointer Syntax
Use * to declare a pointer, also to get value of pointer
Use & to get address of a variable (address == pointer)
pi
double *ptr;
double pi, e;
ptr
??.???
ptr = π
e
*ptr = 3.14159;
ptr = &e;
??.???
*ptr = 2.71828;
printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e);
Pointer Syntax
Use * to declare a pointer, also to get value of pointer
Use & to get address of a variable (address == pointer)
pi
double *ptr;
double pi, e;
ptr
??.???
&pi
ptr = π
e
*ptr = 3.14159;
ptr = &e;
??.???
*ptr = 2.71828;
printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e);
Pointer Syntax
Use * to declare a pointer, also to get value of pointer
Use & to get address of a variable (address == pointer)
pi
double *ptr;
double pi, e;
ptr
3.14159
&pi
ptr = π
e
*ptr = 3.14159;
ptr = &e;
??.???
*ptr = 2.71828;
printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e);
Pointer Syntax
Use * to declare a pointer, also to get value of pointer
Use & to get address of a variable (address == pointer)
pi
double *ptr;
double pi, e;
ptr
3.14159
&e
ptr = π
e
*ptr = 3.14159;
ptr = &e;
??.???
*ptr = 2.71828;
printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e);
Pointer Syntax
Use * to declare a pointer, also to get value of pointer
Use & to get address of a variable (address == pointer)
pi
double *ptr;
double pi, e;
ptr
3.14159
&e
ptr = π
e
*ptr = 3.14159;
ptr = &e;
2.71828
*ptr = 2.71828;
printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e);
Pointer Syntax
Use * to declare a pointer, also to get value of pointer
Use & to get address of a variable (address == pointer)
pi
double *ptr;
double pi, e;
ptr
3.14159
&e
ptr = π
e
*ptr = 3.14159;
ptr = &e;
2.71828
*ptr = 2.71828;
printf("Values: %p %g %g %g\n", ptr, *ptr, pi, e);
p = &g;
p->type = 3; /* Set Gate type of struct that p points to. */
/* Same as (*p).type.
*/
/* Same result (in this case) as g.type. */
p = 0;
d = %g\n", d);
Dynamic Memory
No new operator
Use malloc(num-of-bytes) instead
Use sizeof to figure out how big (how many bytes)
something is
struct Gate *p = (struct Gate *)
malloc(sizeof(struct Gate));
assert(p != 0);
/* Function prototype. */
Preprocessor
A preprocessor scans C programs before compiling
Used to:
Include/embed other files (usually used to embed .h files)
Make symbolic constants
Conditionally compiles code
Other uses
#define MAX 423
Questions ??