Upload
vahidmesicvaha
View
37
Download
3
Embed Size (px)
DESCRIPTION
Jezik C
Citation preview
STRUKTURE PODATAKA I ALGORITMI 1Uvod u programski jezik C
ŠTA JE C?
Programski jezik opšte namene
Jezik za sistemsko programiranje, ali i ...
BCPL, Martin Richards
B, Ken Thompson, 1970
ŠTA SADRŽI C?
Tipovi podataka Znakovi Brojevi (celi i sa pokretnim zarezom) Pokazivači Nizovi Strukture
Konstrukcije za kontrolu toka Naredbe grananja Naredbe višestrukog grananja Naredbe ponavljanja
ŠTA SADRŽI C?
Funkcije Vraćaju vrednosti osnovnih tipova Rekurzija Automatske lokalne promenljive Odvojene izvorne datoteke Različiti opsezi promenljivih
Pretprocesiranje
JOŠ NEŠTO O C-U
Jezik niskog nivoa
Ne sadrži operacije za rad sa složenim objektima
Nema poseban mehanizam za rezervisanje memorije
Ne obezbeđuje ulazno/izlazne mehanizme
Svi mehanizmi višeg nivoa moraju biti obezbeđeni pomoću funkcija koje se pozivaju
Ne podržava konkurentno programiranje
ANSI standard
ZDRAVO, SVETE!
Program koji na ekranu ispisuje tekst “hello, world”
#include <stdio.h>
main(){
printf("hello, world\n");}
• Program u C-u se sastoji od funkcija i promenljivih
• main je osnovna funkcija od koje počinje izvršavanje programa
• Svaki program mora imati main funkciju
• Funkcija main poziva druge funkcije
printf("hello, world\n"); Funkcija printf prikazuje izlazne podatke "hello, world\n" je konstantan string Znak \n obezbeđuje prelazak u novi red Sledeći kod nije ispravan
printf("hello, world");
Isto to samo malo drugačije
#include <stdio.h>main(){
printf("hello, ");printf("world");printf("\n");
}
SPECIJALNE SEKVENCE
\n novi red
\t tabulator
\b povratnik (backspace)
\" navodnik
\\ za obrnutu crtu (backslash)
PROMENLJIVE I ARITMETIČKI IZRAZI
Program koji prikazuje tabelu temperatura u Farenhajtima i Celzijusima po formuli
#include <stdio.h>
/* printFahrenheit-Celsius tablefor fahr = 0, 20, ..., 300 */
main(){
int fahr, celsius;int lower, upper, step;
lower = 0; /* lower limit of temperature scale */upper = 300; /* upper limit */step = 20; /* step size */
fahr = lower;while (fahr <= upper){
celsius = 5 * (fahr-32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;
}}
532
9C F
REZULTAT
printf("%d\t%d\n", fahr, celsius);
1 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
printf("%3d\t%6d\n", fahr, celsius);
1 -17 20 -6 40 4 60 15 80 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
VERZIJA SA REALNIM BROJEVIMA
#include <stdio.h>
/*print Fahrenheit-Celsius tablefor fahr = 0, 20, ..., 300; floating-point version */
main(){
float fahr, celsius;float lower, upper, step;
lower = 0; /* lower limit of temperatuire scale */upper = 300; /* upper limit */step = 20; /* step size */
fahr = lower;while (fahr <= upper){
celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;
}}
REZULTAT
printf("%3.0f %6.1f\n", fahr, celsius);
0 -17.8 20 -6.7 40 4.4 ...
SPECIFIKACIJE KONVERZIJE
%d prikazati kao dekadni ceo broj
%6d prikazati kao dekadni ceo broj u polju od bar 6 znakova
%f prikazati kao broj sa pokretnim zarezom
%6f prikazati kao broj sa pokretnim zarezom u polju od bar 6 znakova
%.2f prikazati kao broj sa pokretnim zarezom sa dve decimalne cifre
%6.2f prikazati kao broj sa pokretnim zarezom u polju od bar 6 znakova sa dve decimalne cifre
NAREDBA FOR
#include <stdio.h>
/* print Fahrenheit-Celsius table */main(){int fahr;
for (fahr = 0; fahr <= 300; fahr = fahr + 20)printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
SIMBOLIČKE KONSTANTE
#define ime tekst_zamene
Primer
#include <stdio.h>
#define LOWER 0 /* lower limit of table */#define UPPER 300 /* upper limit */#define STEP 20 /* step size */
/* print Fahrenheit-Celsius table */main(){
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
ULAZ I IZLAZ ZNAKOVA Tekstualni tok
(stream)
getcharc=getchar()
putcharputchar(c)
PrimerKopiranje ulaznog toka na izlaz.
#include <stdio.h>
/* copy input to output; 1st version */main(){
int c;
c = getchar();while (c != EOF) {
putchar(c);c = getchar();
}}
KRAĆE PISANJE
#include <stdio.h>
/* copy input to output; 2nd version */main(){int c;
while ((c = getchar()) != EOF)putchar(c);
}
BROJANJE ZNAKOVA#include <stdio.h>
/* count characters in input; 1st version */main(){
long nc;
nc = 0;while (getchar() != EOF)
++nc;
printf("%ld\n", nc);}
#include <stdio.h>
/* count characters in input; 2nd version */main(){
double nc;
for (nc = 0; gechar() != EOF; ++nc);
printf("%.0f\n", nc);}
BROJANJE REDOVA
#include <stdio.h>
/* count lines in input */main(){int c, nl;
nl = 0;while ((c = getchar()) != EOF)if (c == '\n')++nl;
printf("%d\n", nl);}
BROJANJE REČI#include <stdio.h>#define IN 1 /* inside a word */#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */main(){
int c, nl, nw, nc, state;
state = OUT;nl = nw = nc = 0;while ((c = getchar()) != EOF){
++nc;if (c == '\n') ++nl;if (c == ' ' || c == '\n' || c = '\t') state = OUT;else if (state == OUT){
state = IN;++nw;
}}printf("%d %d %d\n", nl, nw, nc);
}
NIZOVI#include <stdio.h>
/* count digits, white space, others */main(){
int c, i, nwhite, nother;int ndigit[10];
nwhite = nother = 0;for (i = 0; i < 10; ++i) ndigit[i] = 0;
while ((c = getchar()) != EOF)if (c >= '0' && c <= '9')
++ndigit[c-'0'];else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;else
++nother;
printf("digits =");for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);printf(", white space = %d, other = %d\n", nwhite, nother);
}
REZULTAT
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
FUNKCIJEFunkcija za računanje celobrojnog stepena n celog broja m.
#include <stdio.h>
int power(int m, int n);
/* test power function */main(){
int i;
for (i = 0; i < 10; ++i)printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;}
/* power: raise base to n-th power; n >= 0 */int power(int base, int n){
int i, p;p = 1;for (i = 1; i <= n; ++i)
p = p * base;return p;
}