Professional Documents
Culture Documents
P20 Ostalo OR
P20 Ostalo OR
Preprocesorske direktive
i struktura programa
Doc. dr Vedran Ljubovi
Preprocesorske direktive
ukljuivanje biblioteka
#define PI 3.1415926
7.1.2016
simbolike konstante
Direktiva #define
#define PI 3.1415926
Ili recimo
#define y x
...
x = y + 1;
7.1.2016
Makro sa parametrima
7.1.2016
Makro sa parametrima
U tom sluaju
int x = kvadrat(x+2)-x;
bi dalo
int x = x+2*x+2-x;
to je oito netano.
7.1.2016
Makro operatori
7.1.2016
Direktiva #undef
7.1.2016
Direktiva #ifdef
7.1.2016
7.1.2016
7.1.2016
10
7.1.2016
11
Direktiva #include
7.1.2016
12
Header datoteke
7.1.2016
13
main.c
#include <stdio.h>
#include "faktorijel.h"
int main() {
double x, S=0;
int n, i;
printf("Unesite x: ");
scanf("%lf", &x);
printf("Unesite n: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
S += 1.0/faktorijel(i);
printf("S=%g", S);
return 0;
}
7.1.2016
14
main.c
#include <stdio.h>
#include "faktorijel.h"
int main() {
double x, S=0;
Datoteka faktorijel.h se nalazi
int n, i;
u istom folderu kao main.c
printf("Unesite x: ");
scanf("%lf", &x);
printf("Unesite n: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
S += 1.0/faktorijel(i);
printf("S=%g", S);
return 0;
}
7.1.2016
15
faktorijel.h
#ifndef FAKTORIJEL_H
#define FAKTORIJEL_H
long faktorijel(int x);
#endif
7.1.2016
16
Include guard
7.1.2016
17
faktorijel.c
long faktorijel(int x) {
int i, rez=1;
for (i=2; i<=x; i++)
rez *= i;
return rez;
}
7.1.2016
18
Dodatne datoteke
Code::Blocks
C9
7.1.2016
19
Komandna linija
7.1.2016
20
Komandna linija
7.1.2016
21
Osnovne naredbe
cd promjena direktorija
7.1.2016
22
Osnovne naredbe
cd promjena direktorija
cp kopiranje datoteke
7.1.2016
23
Pokretanje programa
7.1.2016
24
7.1.2016
25
gcc
7.1.2016
26
7.1.2016
27
Preprocesiranje
7.1.2016
28
Modifikacija main.c
#include <stdio.h>
#include "faktorijel.h"
#define RECIPRO(x) 1.0/x
#define KRAJ return 0
int main() {
double x, S=0;
int n, i;
printf("Unesite x: ");
scanf("%lf", &x);
printf("Unesite n: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
S += RECIPRO(faktorijel(i));
printf("S=%g", S);
KRAJ;
}
7.1.2016
29
Preprocesiranje
Kucamo:
gcc -E main.c -o main_pre.c
7.1.2016
30
main_pre.c
#
#
#
#
#
#
#
1 "main.c"
1 "<command-line>"
1 "main.c"
1 "c:\\program files\\.../stdio.h" 1 3
19 "c:\\program files\\.../stdio.h" 3
1 "c:\\program files\\.../_mingw.h" 1 3
32 "c:\\program files\\.../_mingw.h" 3
# 33 "c:\\program files\\.../_mingw.h" 3
# 20 "c:\\program files\\.../stdio.h" 2 3
7.1.2016
31
main_pre.c
...
# 129 "c:\\program files\\...include/stdio.h" 3
typedef struct _iobuf
{
char* _ptr;
int _cnt;
char* _base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
} FILE;
...
Definicija strukture FILE
7.1.2016
32
main_pre.c
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
fopen (const char*, const char*);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
freopen (const char*, const char*, FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
fflush (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
fclose (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
remove (const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
rename (const char*, const char*);
Prototipovi nekih
poznatih funkcija
7.1.2016
33
main_pre.c
# 2 "main.c" 2
# 1 "faktorijel.h" 1
long faktorijel(int x);
# 3 "main.c" 2
int main() {
double x, S=0;
int n, i;
printf("Unesite x: ");
scanf("%lf", &x);
printf("Unesite n: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
S += 1.0/faktorijel(i);
printf("S=%g", S);
return 0;
}
7.1.2016
Uraena je supstitucija
makro-a u main datoteci
34
Prevoenje
(zbunjujue, jer se sam program koji prevodi asemblerski kod u mainski kod
takoe zove asembler).
7.1.2016
35
main.asm
.file
"main.c"
.def ___main; .scl 2;
.type
32; .endef
.section .rdata,"dr"
LC1:
.ascii "Unesite x: \0"
LC2:
.ascii "%lf\0"
LC3:
.ascii "Unesite n: \0"
LC4:
.ascii "%d\0"
LC6:
.ascii "S=%g\0"
.text
.globl _main
.def _main; .scl 2;
.type
32; .endef
_main:
pushl
%ebp
movl %esp, %ebp
andl $-16, %esp
subl $64, %esp
call ___main
7.1.2016
36
Asembliranje
7.1.2016
37
Linkovanje
7.1.2016
38
Disasembliranje
7.1.2016
39
Debugger
7.1.2016
40