23
STRUKTURE PODATAKA I ALGORITMI 1 Uvod u programski jezik C

Jezik c

Embed Size (px)

DESCRIPTION

Jezik C

Citation preview

Page 1: Jezik c

STRUKTURE PODATAKA I ALGORITMI 1Uvod u programski jezik C

Page 2: Jezik c

ŠTA JE C?

Programski jezik opšte namene

Jezik za sistemsko programiranje, ali i ...

BCPL, Martin Richards

B, Ken Thompson, 1970

Page 3: Jezik c

Š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

Page 4: Jezik c

ŠTA SADRŽI C?

Funkcije Vraćaju vrednosti osnovnih tipova Rekurzija Automatske lokalne promenljive Odvojene izvorne datoteke Različiti opsezi promenljivih

Pretprocesiranje

Page 5: Jezik c

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

Page 6: Jezik c

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

Page 7: Jezik c

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");

}

Page 8: Jezik c

SPECIJALNE SEKVENCE

\n novi red

\t tabulator

\b povratnik (backspace)

\" navodnik

\\ za obrnutu crtu (backslash)

Page 9: Jezik c

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

Page 10: Jezik c

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

Page 11: Jezik c

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;

}}

Page 12: Jezik c

REZULTAT

printf("%3.0f %6.1f\n", fahr, celsius);

0 -17.8 20 -6.7 40 4.4 ...

Page 13: Jezik c

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

Page 14: Jezik c

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));

}

Page 15: Jezik c

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));

}

Page 16: Jezik c

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();

}}

Page 17: Jezik c

KRAĆE PISANJE

#include <stdio.h>

/* copy input to output; 2nd version */main(){int c;

while ((c = getchar()) != EOF)putchar(c);

}

Page 18: Jezik 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);}

Page 19: Jezik c

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);}

Page 20: Jezik c

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);

}

Page 21: Jezik c

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);

}

Page 22: Jezik c

REZULTAT

digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345

Page 23: Jezik c

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;

}