The C Programming Language
An Introduction for Programmers |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
//
and the end of the line
is a comment/*
and a */
is a commentchar
double
float
int
long
short
long double
convert the other to long double
double
convert the other to double
float
convert the other to float
long
convert the other to long
+
)-
)*
)/
)/
)%
)++
) and Decrement (--
)-
)>
<
>=
<=
==
!=
&&
||
!
&
|
^
<<
>>
~
if
Statementwhile
Loopsdo-while
Loopsfor
Loopsenum choices {NO, YES}; enum{JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
void
pointers can hold any type of pointer*
&
'\0'
character as their last element// This statement will allocate memory for // an array of 17 character (16 for the "real" // characters and 1 for the null character) and // assign the characters 'C', 'o', ... '\0' to the // elements char department[] = "Computer Science";
const
Qualifierconst
qualifier can appear
once in a declaration (but not after a comma)const
Qualifier (cont.)const int i = 5; i = 10; // Will not compile const char *department; department = "Computer Science"; // Will compile department[0] = 'X'; // Will not compile department = "Mathematics"; // Will compile const double grades[] = {90., 75.}; grades[1] = 100.; // Will not compile char course[] = "C Programming"; course = "Data Structures"; // Will not compile because the // the RHS is const but the LHS is not const char grade[] = "B-"; grade = "B+"; // Will not compile because the // LHS is const (i.e., non-modifiable)
p
is a pointer to an element of an array
then p++
increments p
to point to
the next element// Allocate memory for and initialize the array double grades[] = {90., 75., 55.}; // Assig the address of (the first element of) the array to p double *p = grades; // Change element 0 the obvious way grades[0] = 100.; // Change element 1 a less obvious way (using the address arithmetic // performed by the [] operator p[1] = 100.; // Change element 2 a less obvious way (using explicit address arithmetic) p += 2; // Change the pointer (by the size of two doubles *p = 100.; // Assign 100.0 to the address (by dereferencing p)
const
Qualifier and "Strings"// This statement will assign the address of the "string" // "Computer Science\0" to the pointer department. char *department = "Computer Science"; // The above isn't safe because the following will compile // and, when executed, cause a problem (e.g., a segmentation fault) // because read-only memory is being written to department[0] = 'X'; // So, it's better to do the following const char *department = "Computer Science";
return
statementvoid
Functions:
void
type and no return
statement or a return
statement with no expression
void
then the
compiler will check to make sure the actual parameter list
is emptysize = area(10.2, 20.9);
void sort(void *data[], int (*comp)(void *, void *)) { // The body of the algorithm which can include statements like: if ((*comp)(data[i], data[i+1]) < 0) { } }
struct employee { int age; double salary; };
.
->
is short for (*pointer).member
typedef
typedef int Length; typedef struct tnode *Treeptr; // A Treeptr is a pointer to a structure typedef struct tnode { // A Treenode is a structure char *word; int count; Treeptr left; Treeptr right; } Treenode;
union u_type { int ivalue; double dvalue; } u;
u
will be large enough to hold
the larger of an int
and double