Title: C Programming Revision
1C Programming Revision
- Simple C program layout
- C data types
- printf output
- C pointers and arrays
- Stack and Heap memory
- Global vs Local variables
2C-Programming Revisions
159.234
C-LANGUAGE STRUCTURE
/ include statements / include
ltstdio.hgt include ltstring.hgt / define
statements / define MAX 2000 / function
prototypes / void subfunc(int argument) /
global variables / int i,j char c float
x,y char s80
/ functions / void subfunc(int argument)
int i / local variables /
statements... / main program / int main()
statements...
3C-Programming Revisions
159.234
VARIABLES
must be declared before they are used. Names
start with a letter, subsequent characters can be
letters, numbers or the underscore. Types
char (unsigned) int
(unsigned)(short, long) float (double)
pointers Assignment statements i 3 /
or i 0x03 hex / x 4.5 / x
4.5e0 scientific / c 'a' / c 0x61
c 97 /
4C-Programming Revisions
159.234
VARIABLES
must be declared before they are used. Names
start with a letter, subsequent characters can be
letters, numbers or the underscore. Types
char (unsigned) int
(unsigned)(short, long) float (double)
pointers Assignment statements i 3 /
or i 0x03 hex / x 4.5 / x
4.5e0 scientific / c 'a' / c 0x61
c 97 /
5C-Programming Revisions
159.234
Expressions operators
- / ltlt gtgt (left and right shift)
(one's complement) (bitwise
AND, OR and XOR) i (ij) 3 i c -
48 Shortcut operators are , - etc
i i j k
6C-Programming Revisions
159.234
Arrays
int m100 float f10 char s80 m4
3 fi fi1 Strings are arrays of
characters. Constants are enclosed in double
quotes. The last character in a valid string is
the NUL. Arrays may have more than one
dimension char sarray2580
sarray104 'A' strcpy(sarray3,Lucienne
") (Actions on whole arrays are via function
calls)
7C-Programming Revisions
159.234
Structures
struct person char surname80 char
forename80 int age struct person
x,y10 Individual elements within a structure
are called fields and are accessed using the dot
operator x.age 26 strcpy(x.surname,"Kay")
8C-Programming Revisions
159.234
New types
New types can be defined by the
programmer typedef struct person
person_type person_type x
9C-Programming Revisions
159.234
INITIALIZING VARIABLES
Global variables are initialised to 0 by default.
Local variables contain random data. Variables
are initialised when they are declared char c
'A' int i 10 char s80 "hello world" int
m5 4,7,5,9,1 char sa380 "string
one", "string two", "string three"
10C-Programming Revisions
159.234
FUNCTIONS
Functions have arguments and can return
values int fct1(int i,int j) char fct2(char
c) void fct3(char s) Arguments which are
simple types, have their values copied into the
corresponding formal parameter. For arrays, a
pointer to the first element of the array is
copied.
11C-Programming Revisions
159.234
STRING FUNCTIONS
strcpy(s1,s2) l strlen(s1) if
(strcmp(s1,s2) 0) strcat(s1,s2) Whe
n a return statement is executed the function
terminates. When the exit function is called the
program terminates.
FUNCTIONS FOR TERMINATING
12C-Programming Revisions
159.234
OUTPUT
the library routine printf is used for output.
The first argument is a text string, which
contains formatting information. gets for
complete lines of text. getchar/getch for
characters. scanf for numbers and words.
INPUT
13C-Programming Revisions
159.234
FILES
Files file I/O routines use a FILE pointer.
FILE f f fopen(filename,mode)
fprintf(f,"s",s) fgets(string,size,f)
fscanf(f,"d",i) fclose(f)
14C-Programming Revisions
159.234
CONDITIONAL STATEMENTS
if and switch if (test)
statements... test contains logical
operators lt lt ! gt gt
! if (test) statements...
else statements...
15C-Programming Revisions
159.234
SWITCH
switch (int or char) case number or
character statements... break
case number or character statements...
break . . default
statements...
16C-Programming Revisions
159.234
LOOPS
while (test) statements...
for (initialisetestincrement)
statements... do statements...
while (test)
17C-Programming Revisions
159.234
POINTERS
Pointers int p stuct person q The
operator p i q y0 /or q y
/ The operator (or -gt for structs) (p)
6 q-gtage 26
18C-Programming Revisions
159.234
POINTERS
To alter the value of an actual parameter, a
pointer to that variable must be passed.
fct1(i) void fct1(int ip) (ip)
6 Pointers can be assigned dynamically. p
(char )malloc(128) free(p)
19C-Programming Revisions
159.234
RECURSION
Recursion A function may call itself (direct
recursion) or may call itself via a call to
another function (indirect recursion). A
recursive function must always have a route
through it, that does not involve any more
recursive calls.
20- Example output
- (terminated with D or Z)
- include ltstdio.hgt
- include ltmath.hgt
- int main()
- char ch / is really an
int from -128 to 0 to 127 / - unsigned char uch / is really an int from
0 to 255 / - short int si / can just say
short si / - int i / usually same
as machine word ie 32 bits / - long int li / could be 32
but may be 64 bits / - long long int ll / generally is 64
bits on gcc systems / - float f / IEEE format floating point,
32 bits usually / - double d / double precision - about 15
significant figures / - for(i0ilt5i) / loop from 0...4 ie do
it 5 times / - printf("d 2 d\n", i, i 2 ) /
print a string with 2 ints / -
-
- ch '\n' / the newline character
/ - uch 255
0 2 0 1 2 2 2 2 4 3 2 6 4 2
8 li 2147483647 a a b b c c d d e e hello hello
21- include ltstdio.hgt
- void printArray( int iarr, int n ) /
function to print out an int array / - int i / print to stderr, could
have used stdout instead / - for(i0iltni)
- fprintf(stderr, "d\t", iarri ) /
separate them with tabs / - fprintf(stderr,"\n") /
finish with a newline / -
- int main()
- int i / an integer takes 32 bits
of memory on the stack / - int iarray4 / will take /
- int ipointer
- printf("size of int d\n", sizeof(i) )
- printf("size of iarray d\n", sizeof(iarray)
) - printf("size of ipointer d\n",
sizeof(ipointer) ) - i 0
- iarrayi 42
- printArray( iarray, 4 )
- for(i0ilt4i)
- iarrayi i 10
- Example output
- size of int 4
- size of iarray 16
- size of ipointer 4
- 42 49 6 3
- 0 10 20 30
- ipointer 3221224552
- contents of ipointer 4
- 1 11 21 31
22Pointers
- Pointers in C and C are a special data type
that is capable of pointing to the memory
location of a normal data type. - Pointers are typed - eg pointer to an int is
not the same as pointer to a char - Pointers let us do address manipulation and
are a powerful but dangerous feature of the
language
- In C and C arrays are implemented via pointer
arithmetic - int iarray10 tells the compiler you want the
space for 10 integers set aside (not yet
initialised) - int iptr sets aside memory for a pointer but
not for an actual integer!
23Stack Memory
- When you write and run a program each function
can set up its own variable s on what is
known as the stack - a part of the
computers memory organised like a stack of
cards.
- Variables set up on the stack typically last only
for the duration of execution of the function
that declares them.
24Stack example
- int myfunc() / prototype /
- int main()
- int i
- myfunc()
- return 0
-
- int myfunc()
- int j 10
-
- i is a stack variable set up by the function
main() - j is a stack variable set up by the function
myfunc() and will last only for the duration of
the call to myfunc()
25Global Variables - use with Caution
- int k 42
- int myfunc()
- int j 10
- k k j
- return k
-
- int main()
- int i 3
- int k 7
- i myfunc()
- printf(i d, k d\n, i, k )
- return 0
-
- k is a global variable
- It is accessible to both main() and myfunc()
- main() declares its own stack variable k which
hides the global k - Output would be
- 52, 7
26DO NOT USE GLOBAL VARIABLES
27Heap Memory
- The Heap is a pool of memory available to all
functions which they must access using
malloc() and related functions - Mallocd memory stays allocated until you free
it - This must be used with great care (otherwise get
Bus Error or Segmentation Fault) - Remember to allocate memory before you try to
use it - Remember to free up memory once you are finished
with it. - C has neater ways of managing this
- int ia / a global pointer /
- int myfunc()
- ia (int )malloc( 10 sizeof(int) )
- ia2 7
-
- int main()
- myfunc()
- ia0 8
- ia9 87
- free( ia )
- return 0
28Miscellany of C Features
- if( ) else
- while( )
- do while( )
- Use of ints 0 or not zero in place of
false/true - Functions can call themselves (recursion)
- No string type in C so use arrays of char,
terminated by \0 character
- Need include ltmaths.hgt
- to use the maths library functions eg
sqrt() - printf()/scanf() fprintf()/fscanf() I/O
routines - putc()/getc() for single character I/O
29Summary
- Think of pointers as memory addresses
- Memory your program uses can be stack or heap
memory - Stack variables last only as long as the called
function does - Heap variables last until you free them up
- Look at the C programming revision material on
the web site. - C is based around the idea of functions which
call one another - OO programming goes beyond this
- Next - C simple I/O