The C Preprocessor
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης
Constants
#define MAXSIZE 256
This will lead to the value 256 being substituted for
each occurrence of the word MAXSIZE in the file.
ΗΥ150 – Προγραμματισμός
2
Ξενοφών Ζαμπούλης
Macros
Here is an example of a macro which won't work.
#define DOUBLE(x) x+x
For statement:
a = DOUBLE(b) * c;
will be expanded to
a = b+b * c;
Since * has a higher priority than +, the compiler will treat it
as:
a = b + (b * c);
But:
#define DOUBLE(x) (x+x)
Here the brackets around the definition force the expression
to be evaluated before any surrounding operators are applied.
ΗΥ150 – Προγραμματισμός
3
Ξενοφών Ζαμπούλης
Verbose
#define EBUG
#ifdef EBUG
printf(“Debug info: variable foo has value %d\n”,foo);
#endif
ΗΥ150 – Προγραμματισμός
4
Ξενοφών Ζαμπούλης
Verbose
#define EBUG
#ifdef EBUG
printf(“Debug info: variable foo has value %d\n”,foo);
#elseif
printf(“No need for debug\n”);
#endif
ΗΥ150 – Προγραμματισμός
5
Ξενοφών Ζαμπούλης
Verbose
gcc –DEBUG myprogram.c
ΗΥ150 – Προγραμματισμός
6
Ξενοφών Ζαμπούλης
Standard I/O
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης
UNIX File Redirection
To run prog1 but read data from file infile instead of
the keyboard, you would type
prog1 < infile
To run prog1 and write data to outfile instead of the
screen, you would type
prog1 > outfile
Both can also be combined as in
prog1 < infile > outfile
Redirection is simple, and allows a single program to
read or write data to or from files or the screen and
keyboard.
ΗΥ150 – Προγραμματισμός
8
Ξενοφών Ζαμπούλης
Character Input and Output
char c = getchar();
void putchar(char c);
ΗΥ150 – Προγραμματισμός
9
Ξενοφών Ζαμπούλης
EOF, The End of File Marker
EOF is a character which indicates the end of a
file. It is returned by read commands of the getc
and scanf families when they try to read beyond
the end of a file / input.
ΗΥ150 – Προγραμματισμός
10
Ξενοφών Ζαμπούλης
Πως ψάχνουμε για εντολές
βιβλιοθήκης
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης
Εντολή συστήματος “man”
If you already know the name of the function you want, you can read the
page by typing (to find about strcat).
> man 3 strcat
If you don't know the name of the function, a full list is included in the
introductory page for section 3 of the manual. To read this, type
> man 3 intro
There are approximately 700 functions described here. This number tends
to increase with each upgrade of the system. On any manual page, the
SYNOPSIS section will include information on the use of the function. For
example
#include <time.h> char *ctime(time_t *clock)
This means that you must have #include <time.h> in your file before you
call ctime. And that function ctime takes a pointer to type time_t as an
argument, and returns a string (char *). time_t will probably be defined in
the same manual page. The DESCRIPTION section will then give a short
description of what the function does. For example
ctime() converts a long integer, pointed to by clock, to a 26-character string
of the form produced by asctime().
ΗΥ150 – Προγραμματισμός
12
Ξενοφών Ζαμπούλης
Οργάνωση κώδικα
Σε μεγαλύτερα προγράμματα, οι δηλώσεις των συναρτήσεων
συγκεντρώνονται σε ένα ή περισσότερα header files
(κατάληξη .h), τα οποία συμπεριλαμβάνονται κατά την
προεπεξεργασία του κώδικα:
#include "name.h"
όπου name.h το όνομα του header file και μπορεί να
περιλαμβάνει και το path.
Oι headers που ορίζει ο προγραμματιστής περικλείονται σε “”.
Oι headers του συστήματος περικλείονται σε <>.
Με την συμπερίληψη των κατάλληλων headers:
–
–
ο compiler γνωρίζει τον τρόπο κλήσης των συναρτήσεων ή
εκτελέσιμων βιβλιοθηκών που χρειάζεται ένα τμήμα κώδικα.
ο προγραμματιστής μπορεί να επισκοπήσει εύκολα τη λειτουργικότητα
του κώδικα
ΗΥ150 – Προγραμματισμός
13
Ξενοφών Ζαμπούλης
ΗΥ-150
Προγραμματισμός
Ασκήσεις
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης
Φωλιασμένοι βρόχοι
/*Πως θα εκτυπώναμε το παρακάτω σχήμα;*/
*
*
**
**
***
***
****
****
*****
*****
******
******
******* *******
****************
ΗΥ150 – Προγραμματισμός
15
Ξενοφών Ζαμπούλης
Μηχανές Πεπερασμένων
Καταστάσεων
Finite State Machines
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης
Finite State Machines
letter | digit I EOF
letter
S
ΗΥ150 – Προγραμματισμός
A
17
Ξενοφών Ζαμπούλης
Finite State Machines viewed as Graphs
• A state
• The start state
a
• A transition
• Accepting/Terminal state
ΗΥ150 – Προγραμματισμός
18
Ξενοφών Ζαμπούλης
Example: Integer Literals
FSM that accepts integer literals with an optional
+ or - sign:
digit, ‘_’
B
digit
digit, ‘_’
+
S
A
-
ΗΥ150 – Προγραμματισμός
19
Ξενοφών Ζαμπούλης
FSM’s in Theory
Simple theoretical construct
–
–
–
Set of states (S)
Input vocabulary (I)
Transitional function T(s,i)
A way of denoting how an
program can change its
state over time.
ΗΥ150 – Προγραμματισμός
20
Ξενοφών Ζαμπούλης
FSM’s in Practice
Each state represents some desired behavior.
The transition function T resides across all states.
–
Each state “knows” how to transition to other states.
Accepting states (those that require more input) are
considered to be the end of execution for an FSM.
Input to the FSM continues as long as the game continues.
ΗΥ150 – Προγραμματισμός
21
Ξενοφών Ζαμπούλης
FSM’s in Games
Character AI can be modeled
as a sequence of mental
states.
“World” events can force a
change in state.
Monster In Sight
Gather
Treasure
Flee
No Monster
Monster Dead
ΗΥ150 – Προγραμματισμός
22
Fight
Cornered
Ξενοφών Ζαμπούλης
FSM Implementation - Code
Simplest method
After an action, the state
might change.
ΗΥ150 – Προγραμματισμός
void RunLogic( int state ) {
switch( state ) {
case 0: //Wander
Wander();
if( SeeEnemy() )
state = 1;
if( Dead() )
state = 2;
break;
case 1: //Attack
Attack();
state = 0;
if( Dead() )
state = 2;
break;
case 3: //Dead
DeleteAvatar();
break;
}
return statel
}
23
Ξενοφών Ζαμπούλης
Παράδειγμα: Robocode
Καθορισμός Καταστάσεων
–
Υλοποίηση αλλαγής
κατάστασης
–
Attack, Evade, Search, etc.
Include an error state that is
noticeable.
Αποσφαμάτωση
(debugging)
–
Verbosity levels are a must.
ΗΥ150 – Προγραμματισμός
24
Ξενοφών Ζαμπούλης
More FSM Extensions
Fuzzy State Machines
–
Multiple FSM’s
–
Degrees of truth allow multiple
FSM’s to contribute to character
actions.
High level FSM coordinates
several smaller FSM’s.
Polymorphic FSM’s
–
–
Allows common behavior to be
shared.
Soldier -> German -> Machine
Gunner
ΗΥ150 – Προγραμματισμός
25
Ξενοφών Ζαμπούλης
Run Length Encoding
Run length encoding (RLE) απλούστερη μέθοδος
συμπίεσης.
Κωδικοποιεί πληροφορία για την επανάληψη των
χαρακτήρων / στοιχείων εισόδου
Είναι ο αλγόριθμος που χρησιμοποιεί το FAX
(τηλεομοιοτυπία)
ΗΥ150 – Προγραμματισμός
26
Ξενοφών Ζαμπούλης
RLE
As a basic example, consider the following string of
numbers:
555588822222
There is a fair amount of redundancy there. In RLE
notation, this same string could be expressed as:
453852
ΗΥ150 – Προγραμματισμός
27
Ξενοφών Ζαμπούλης
RLE
123456
Apply the same RLE compression scheme as before:
111213141516
ΗΥ150 – Προγραμματισμός
28
Ξενοφών Ζαμπούλης
Παράδειγμα
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWW
WWWWWWWWWWWWWWWWWWWBWWWWWWWWWW
WWWW
12W1B12W3B24W1B14W
ΗΥ150 – Προγραμματισμός
29
Ξενοφών Ζαμπούλης
© Copyright 2026 Paperzz