Introduction To C PDF
Introduction To C PDF
Mike Holenderski
November 10, 2014
Before a variable can be used, it has to be declared,
specifying its type. For example,
int i;
means that variable i is of type int (i.e. it is an integer
number).
In C, boolean variables (i.e. those which can be either
true or false) are represented using the type char or
int, where 0 represents a false value, and anything else
represents a true value. It is customary to use 1 to
represent true.
The integer data types char and int can be either
signed or unsigned. Signed types can represent both
positive and negative values, whereas unsigned types can
only represent values greater or equal to 0. For example,
unsigned int j;
signed int k;
Comments
declares an unsigned integer variable j and a signed integer variable k. Note that integer types are signed by
default, so the previously declared variable i is a signed
integer.
2.2
Arrays
Variables
int a[3];
The program state is defined by the values assigned to all
variables in the program. Each variable is of a particular declares an array variable called a which contains 3 intype, which describes what kind of values the variable teger values.
can assume. We distinguish the following types:
2.1
2.3
Simple types
Structures
struct {
int x;
int y;
} point;
2 The
2.4
Type definition
2.7
Initial values
It may be convenient to introduce aliases for certain When declaring a variable one can also specify its initial
value, which the variable will hold at the moment that
types, in particular structures. For example,
the program starts executing. For example,
typedef struct {
int i = 42;
int x;
int y;
declares an integer i which will be initialized to 42.
} Point;
The value of an uninitialized variable is undefined.
declares a Point to be a structure type, containing two
integer fields x and y. The Point type can then be used
to declare other variables. For example,
Assignment
An assignment accomplishes a state change during program execution, by assigning a new value to a variable.
An assignment has the following syntax:
Point p1;
Point p2;
declares two structure variables p1 and p2, each containing an x and y field.
<variable> = <expression>;
2.6
Functions (Advanced)
3.1
==
!=
<=
>=
||
&&
b
b
b
b
b
b
check
check
check
check
check
check
if
if
if
if
if
if
a
a
a
a
a
a
Simple types
For example,
i = i + 1;
3.2
Arrays
void (*g)(void);
3.3
Structures
3.6
Functions (Advanced)
Individual fields of a structure variable can be be ac- A function variable can be assigned a particular function
cessed using the dot notation. For example,
definition (see Section 5). For example,
p2.y = p1.x + 2;
3.4
Constants
Control flow
4.1
Sequential composition
#define N 1000
Assignments and declarations can be combined into sewill make sure that any occurrence of N in the code will quences using the ; sign, meaning that the joined asbe substituted with 1000. Note that it is a textual sub- signments should be executed one after the other. For
stitution. For example,
example,
#define e 1+1
Point point;
int x = e*2;
point.x = 1;
point.y = point.x + 2;
will evaluate to x = 1 + 1 2 = 3, rather than x =
(1 + 1) 2 = 4.
will declare variable point and then assign 1 to point.x,
and subsequently 3 to point.y.
3.5 Pointers (Advanced)
Note that in C the ; sign must also follow the last
assignment.
A pointer can be assigned a reference to another variable. For example,
4.2
p = &p1;
Branching
assigns to the pointer p a reference to the structure p1. Sequential composition on its own can express only the
A pointer can be dereferenced, returning the value of most basic programs and is not sufficient for most programming tasks. A conditional branch can be introthe variable it is pointing to. For example,
duced into the sequence using the if-then-else statement.
p2 = *p;
For example,
assigns to the fields of the structure p2 the values of the
structure pointed to by the p pointer.
A pointer to a structure, e.g. the p pointer, must be
dereferenced to access the individual fields. For example,
if (i > 42) {
/* then branch: we know that i > 42 */
point.x = i;
point.y = 2*i;
} else {
/* else branch: we know that i <= 42 */
point.x = 0;
point.y = 0;
}
k = (*p).x;
assigns the value of the x field of the Point structure
pointed to by the p pointer.
There is an alternative notation for accessing a filed
of a structure pointed to by a pointer. For example,
k = p->x;
is equivalent to k = (*p).x;
3
curly braces { and } enclose the code comprising Local vs. global variables
a branch. Note that only one of the branches will be
The scope of a variable determines from where it can be
executed.
accessed. A local variable is declared inside of a funcIt is also possible to skip the else branch. For example,
tion and can be accessed only within that function. A
global variable is declared outside of functions (on the
if (i > 42) {
global level) and can be accessed within any function.
point.x = i;
For example,
point.y = 2*i;
}
int i;
checks if i is greater than 42, and if so, executes point.x
= i; point.y = 2*i;. Otherwise, it does nothing.
4.3
int Factorial(int x) {
int y = 1;
int k = 1;
while (k <= x) {
y = y * k;
k = k + 1;
}
return y;
}
Loop
int main(void) {
int k = 10;
int x;
i = 0;
while (i < k) {
x = Factorial(i);
i = i + 1;
}
}
Functions
References
[1] Niklaus Wirth, Algorithms & data structures, Prentice Hall, 1985
[2] B. Kernighan, D. Ritchie, The C Programming Language, 2nd Edition, Prentice Hall, 1988