Pa evo onda i nastavka.
2. Pređimo na stvar
Možda prebrzo dajem idući dio tutoriala, ali očekujem da bi iz onog prvog gradiva trebalo ostati malo nejasnoća i pitanja, pa krenimo mi s nečim konkretnijim. Još uvijek smo na potpunim osnovama, pa ni ovo neće biti teško. Nadam se da ste isprobali primjer iz prošlog dijela. Nisam napomenuo, kako biste u DEV-Cpp pokrenuli vaš program, samo stisnite F9.
U ovom dijelu počet ćemo koristiti varijable. Varijable su jednostavno rečeno vrijednosti kojima smo dodali ime tako da ih možemo označiti. Dakako, njihove vrijednosti možemo mijenjati na razne načine, zavisno što želimo učiniti s njima.
Prije svega, moramo razumjeti kako u C jeziku, varijable dijelimo po tipovima podataka koje sadrže. Jako je važno kojeg je određena varijabla tipa, jer to znači da se u tu varijablu mogu pohraniti samo podaci tog oblika. Slijedi pregled najvažnijih tipova podataka koje ćete koristiti u C-u. Skraćenice služe pri deklaraciji varijabli:
Integer..................int - Cjelobrojni tip podataka
Floating point.........float - realni brojevi, dakle ovdje je moguće pohraniti i decimalne brojeve
Character..............char - Znakovi
Naravno, to nisu svi tipovi podataka koje C koristi, tu su i short integer, long, double i slično, međutim, za ovakav početnički tečaj nisu ni bitni. Od ostalog treba napomenuti kako i navedeni tipovi podataka mogu biti i signed ili unsigned, što znači da mogu ili ne moraju sadržavati predznak. Također imajte na umu da character tip podataka zapravo sadrži ASCII kod, odnosno broj koji odgovara nekom znaku. Budete li se puno bavili sa slovima i riječima, ponekad će vam dobro doći i
ASCII tablicaSu kojoj možete točno vidjeti koji ASCII broj odgovara kojem znaku.
Prije svake radnje sa varijablama u programu potrebno ih je prvo deklarirati, odnosno rezervirati mjesto za njih u računalnoj memoriji. U deklaraciji navodite tip varijable, te njezino ime koje može biti bilo kakvo, samo mora početi slovom a ne brojem. Evo primjer:
U ovom primjeru deklarirali smo varijablu koju smo u naletu kreativnosti nazvali "broj" i rekli da je cjelobrojnog tipa (integer) dodavanjem već navedene skraćenice ispred imena. Sad smo joj odredili sudbinu, i ona će moći spremati samo cijele brojeve, a nikako decimalne. Ako želite deklarirati više varijabli istog tipa podatka, kraće ćete to učiniti na ovakav način -
Code:
float a, b, y, x, lol, rofl, pcplay;
- čime u jednoj liniji definirate cjelobrojne varijable nazvane "a", "b", "y", "x", "lol", "rofl" i "pcplay". Samo nemojte zaboraviti ";" na kraju.
Pa počnimo s našim programom onda. Cilj ovaj put je program koji zbraja dva broja i ispisuje rezultat na ekran. Krajnje jednostavno.
Prvo počnimo od pretprocesorskih naredbi. #include-at ćemo <stdio.h> bez kojeg nema ništa, a ovaj put bih ubacio i <stdlib.h>. Iskreno, ne znam točno šta ova druga radi, ali kad god pišem program, i ona je includeana :>
Code:
#include <stdio.h>
#include <stdlib.h>
Slijedi main() funkcija koja sadržava naš program. Napisat ću je odmah, pa ćemo poslije analizirati.
Code:
int main()
{
int a, b, c;
printf("Upisi prvi broj: ");
scanf("%d", &a);
printf("Upisi drugi broj: ");
scanf("%d", &b);
c = a + b;
printf("Rezultat je %d", c);
system("pause");
return EXIT_SUCCESS;
}
E sad se po prvi put susrećemo sa nekim naoko čudnovatim stvarima, primjerice ovi postotci i ampersandovi, što je pa to?
Za početak, pogledajmo onu prvu scanf naredbu. Kao što printf služi za ispisivanje nečega na standardni izlaz (monitor), tako scanf služi za očitavanje sa standardnog ulaza ( tipkovnice) što znači, da će nakon printf naredbe pričekati da korisnik nešto napiše i stisne enter.
Zatim moramo raščistiti što znači "%d". Sve ove oznake koje sadrže znak % su tzv.
format specifieri. Dakle, oni određuju format koji će se u ovom slučaju upisati u program. %d označava da se traži cjelobrojna dekadska vrijednost. Zapravo, evo odmah pregleda f. specifiera:
Integer.................%d (za dekadski brojevni sustav, za druge treba provjeriti, mislim da je oktalno %o, hexadekadski %h, itd.)
Floating point.........%f
Character..............%c
Ono što specifier %d govori scanf funkciji je "Kad korisnik unese jednu vrijednost, zapiši je kao cjeli broj", a onda ovaj dodatak izvan navodnika kaže "zapiši tu vrijednost u varijablu a". Znak & ispred toga je adresni operator, i tu mora biti zato jer scanf funkciji zapravo treba reći da zapiše vrijednost na memorijsku adresu varijable "a". Što to znači naučit ćete dosta kasnije, zasad je dovoljno samo da znate da morate stavljati taj & kod scanf funkcije.
Kako biste bolje razumjeli kako to funkcionira, recimo da kad bi stavili ovako nešto -
scanf ("%d %c", &a, &b)
to bi značilo da se očekuje od korisnika da unese jedan cijeli broj, stisne razmak, i unese neki znak, te da se broj sprema u varijablu a, i znak u b. Možemo staviti i "%d , %c", pa korisnik mora unijeti broj, stisnuti razmak, upisati zarez, opet razmak i upisati neki znak. Kako god zamislite.
Ista fora je i sa drugim upisivanjem broja, a zatim najobičnijom matematičkom formulom izračunamo c koji nam je zbroj prva dva broja. Nakon toga printf funkcija ispisuje rezultat. Primijetite da koristi istu sintaksu kao i scanf. Format specifier %d kaže "tu gdje ja sad stojim neka se ispiše cjelobrojna vrijednost", a nakon navodnika se napiše i odakle, s time da primijećujete da ovdje nisam koristio &. Dakle, kod printf-a za razliku od scanf-a NE IDE adresni operator &. Zašto u scanf za razliku od toga ide i & ne bih znao reći, ali tako to jeste. Pazite da ne pobrkate!
Ostalo već znate, funkcija za izlazak iz programa, i slično. Eventualno u sadržaj printf-ova možete ubaciti i \n koji funkcionira slično kao format specifier, samo što govori da se na njegovom mjestu pređe u novi red. Čisto radi preglednosti. Možete probati i slične programe sa dijeljenjem (/), množenjem (*), oduzimanjem (-). Mislim da ovo zaista nije bilo teško, ako nešto nije jasno, pitajte.
@ReiKo: Zato jer ne znam C++ :>
Odnosno, možda bih ga znao da ne moram opet ove godine slušati programiranje u C-u koje, eto, kroz dvije godine koliko ga radim već dovoljno znam da mogu napraviti tutorial. btw, nisam imao pojma da je za početnike bolji C++...