Académique Documents
Professionnel Documents
Culture Documents
History
C was initially developed by Dennis Ritchie between 1969 and 1973 at AT&T
Bell Labs.
C was designed as the implementation language of the Unix operating system.
C is a relatively small language that remained remarkably stable.
C is one of the most widely used programming languages of all time.
H. St
uben Overview of C Jacobs University Bremen, January 2015
Programming paradigms
procedural / subroutine-oriented
Fortran (1957), C (1972)
object-oriented
C++ (1983), Java (1995)
functional
Haskell (1990)
H. St
uben Overview of C Jacobs University Bremen, January 2015
C standardisation
year
1978
common name
K&R
1989
ANSI C standard
C89
1999
ISO C standard
C99
2011
ISO C standard
C11
H. St
uben Overview of C Jacobs University Bremen, January 2015
H. St
uben Overview of C Jacobs University Bremen, January 2015
H. St
uben Overview of C Jacobs University Bremen, January 2015
int main(void)
// in C the main program is also a function
{
double x, a = 1.0, b = 2.0;
...
x = f(a, b);
// function call
...
// data types of parameters must match
return 0;
}
double f(double x, double y)
{
return x + y;
}
// function definition
H. St
uben Overview of C Jacobs University Bremen, January 2015
step 2
understand the meaning of the parameters (this course)
step 3
call the function with data objects that have
matching data types (correct syntax)
appropriate contents (right semantics)
H. St
uben Overview of C Jacobs University Bremen, January 2015
Data types
topics
H. St
uben Overview of C Jacobs University Bremen, January 2015
H. St
uben Overview of C Jacobs University Bremen, January 2015
is a double
named constants
const
const
const
const
int i = 42;
long int j = 42;
float x = 42.0;
double y = 42.0;
preprocessor definitions
#define
#define
#define
#define
I
J
X
Y
42 defines an int
42L defines a long int
42.0F defines a float
42.0 defines a double
H. St
uben Overview of C Jacobs University Bremen, January 2015
10
Structures
a structure is a data type defined by the programmer
example:
struct point;
struct point { double x, y; };
int main(void)
{
struct point p;
// structure variable declaration . . .
struct point q = { 1.0, 1.5 }; // . . . and initialisation
p.x = 2.0;
p.y = 2.5;
...
H. St
uben Overview of C Jacobs University Bremen, January 2015
11
Arrays
an array is a data structure that stores elements of the same type
consecutively in memory
in standard C code the number of elements must be positive and known at
compile time (!)
example:
double v[10];
// declaration of a one-dimensional array
double m[5][3]; // declaration of a two-dimensional array
v[0] = 0.1;
m[4][2] = 0.0;
H. St
uben Overview of C Jacobs University Bremen, January 2015
12
Variable-length arrays
in C99 and GNU C variable-length arrays (automatic arrays) are implemented
example:
double fun(int n)
{
double x[n];
...
}
H. St
uben Overview of C Jacobs University Bremen, January 2015
13
Pointers
a pointer is a variable that holds the memory address of another variable
there are two unary operators for working with pointers
address / reference operator &
indirection / dereference operator *
example:
double x, *px;
double y = 2.0;
double *py = &y;
x = 1.0;
px = &x;
// assigning a value to x
// assigning a value to px using the address operator
x = y;
x = *py;
*px = y;
14
Generic pointers
a pointer declared as void* can hold any address
example:
the first parameter of the MPI Send function is a generic pointer
H. St
uben Overview of C Jacobs University Bremen, January 2015
15
Pointers to structures
there is a special syntax for accessing members via a pointer to a structure
example:
struct point { double x, y; };
struct point p;
struct point *ptr = &p;
ptr -> x = 2.0;
ptr -> y = 2.5;
H. St
uben Overview of C Jacobs University Bremen, January 2015
16
x[i] = 42;
// these three statements have the same effect
*(x + i) = 42;
*(px + i) = 42;
px = &x[i];
px = x + i;
17
H. St
uben Overview of C Jacobs University Bremen, January 2015
18
H. St
uben Overview of C Jacobs University Bremen, January 2015
19
H. St
uben Overview of C Jacobs University Bremen, January 2015
20
// explicit cast
// implicit type conversion
// implicit type conversion according to function prototype
type casting and conversion only works for integer, floating-point and pointer
types
H. St
uben Overview of C Jacobs University Bremen, January 2015
21
typedef
typedef can be used to define new names
examples:
typedef double Temperature;
// definition . . .
typedef struct point Point;
typedef void MPI_User_function(void *invec, void *inoutvec,
int *len, MPI_Datatype *datatype);
Temperature T;
Point p, *q;
MPI_User_function my_fun;
H. St
uben Overview of C Jacobs University Bremen, January 2015
22
Complicated declarations
examples
double *f(...)
double (*g)(...)
double *(*h)(...)
H. St
uben Overview of C Jacobs University Bremen, January 2015
23
H. St
uben Overview of C Jacobs University Bremen, January 2015
24
47;
11;
48;
12;
H. St
uben Overview of C Jacobs University Bremen, January 2015
25
H. St
uben Overview of C Jacobs University Bremen, January 2015
26
H. St
uben Overview of C Jacobs University Bremen, January 2015
27
Lifetime
Block (stack)
Block (stack or CPU register)
Program
Program
Dynamic (heap)
Scope
Block
Block
Block or compilation unit
Block or compilation unit
Default initializer
Uninitialized
Uninitialized
Zero
Zero
Uninitialized
H. St
uben Overview of C Jacobs University Bremen, January 2015
28
void sub(void)
{
double x;
int j = 1;
static int count = 0;
// automatic, local
// automatic, local
// static, local
...
{
int i;
...
// automatic, local
}
}
H. St
uben Overview of C Jacobs University Bremen, January 2015
29
References
Rothwell, The GNU C Reference Manual
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.pdf
http://en.wikipedia.org
Kernighan and Ritchie, The C Programming Language
Klemens, 21st Century C
H. St
uben Overview of C Jacobs University Bremen, January 2015
30