Centar za edukaciju-BiH



#1 03.12.2010 12:28
zxz Van mreze
Administrator
Registrovan od:03.02.2009
Postovi:10,610


Predmet:Struktura programskog jezika C
Strukturu programskog jezika C čine naredbe koje omogućavaju pravilan rad programa. Osnovnu strukturu programskog jezika C čine:

PRETPROCESORSKE DIREKTIVE (#include)
- svaki program u C-u mora imati barem jednu pretprocesorsku direktivu
- to su naredbe pretprocesoru te ne završavaju znakom točka-zarez
- služe za uključivanje datoteka zaglavlja, (header files) koje sadrže definicije funkcija i varijabli, u program ili za definiranje globalnih konstanti
- predprocesorskom direktivom moguće je uključiti i vlastite datoteke zaglavlja, ali tada morate zamjenite znakove '<' i '>' sa '“' i '“' prije main bloka
- primjer: #include <stdio.h>

DEKLARACIJE FUNKCIJA
- svaki program u C-u ne treba imati deklaracije funkcija
- ne završavaju znakom točka-zarez
- poželjno ih je koristiti kod programa veće složenosti
- funkcije se deklariraju poslije pretprocesorskih direktiva
- primjer: #declare ime 'zoran'

DEKLARACIJE VARIJABLI
- svaka se varijabla prije korištenja mora deklarirati
- deklarirati neku varijablu znači odrediti joj tip i naziv
- deklaracija varijabli se mora obaviti prije korištenja istoimene varijable
- nakon deklaracije varijabli slijedi znak točka-zarez

FUNKCIJE MAIN
- funkciju main mora imati svaki C program
- blok main je 'početna tocka' programa
- blok funkcije main (kao i blok drugih funkcija) započinje otvorenom vitičastom zagradom, a završava zatvorenom vitičastom zagradom
- u bloku ili tijelu funkcije main se moraju nalaziti neke naredbe

KOMENTARI
- komentari služe da bi programski kod učinili jednostavnijim za shvatiti
- poželjno ih je koristiti u svakom programu
- komentari se pišu unutar oznaka // komentar // ili /* komentar */
- komentari se ne mogu ugniježdivati pr. /* // komentar // */

NAREDABA
- svaki program se mora sastojati od nekih naredaba bilo da se nalaze u main bloku ili u bloku neke funkcije
- na kraju svake naredbe obavezno se stavlja znak tačka-zarez !
Podrška samo putem foruma, jer samo tako i ostali imaju koristi od toga.
Ovaj post je ureden 2 puta. Posljednja izmjena 05.05.2011 23:59 od strane zxz. ↑  ↓

#2 28.07.2017 07:57
itiankongu Van mreze
Clan
Registrovan od:13.07.2017
Postovi:4


Predmet:Re: Struktura programskog jezika C
Every C program must have one special function main (). This is the point where execution begins when the program is running. We will see later that this does not have to be the first statement in the program, but it must exist as the entry point. The group of statements defining the main () enclosed in a pair of braces ({}) are executed sequentially. Each expression statement must end with a semicolon. The closing brace of the main function signals the end of the program. The main function can be located anywhere in the program but the general practice is to place it as the first function.

Here is an elementary C program.

main ()
{
}

There is no way to simplify this program, or to leave anything out. Unluckily, the program does not do anything. Following the “main” program name is a couple of parentheses, which are an indication to the compiler that this is a function. The 2 curly brackets { }, properly called braces, are used to specify the limits of the program itself. The actual program statements go between the 2 braces and in this case, there are no statements because the program does absolutely nothing. You will be able to compile and run this program, but since it has no executable statements, it does nothing. Keep in mind however, that it is a legal C program.

main ( ) function should return zero or one. Turbo C accepts both int and void main ( ) and Turbo C coders use both int and void main ( ) in their programs. But in my belief, void main ( ) is not a standard usage. The reason is, whenever a program gets executed it returns an integer to the OS. If it returns ‘zero’, the program is executed successfully. Otherwise it means the program has been ended with error. So main ( ) shouldn’t be declared as void.d as void.

main( ) should be declared as

int main( )
{
……………..
……………..
return 0 ;
}
.For a much more interesting program, load the program

int main ()
{
printf (“Welcome to C language”);
return 0;
}
and display it on your monitor. It is same as the previous program except that it has one executable statement between the braces.

The executable statement is another function. Again, we won’t care about what a function is, but only how to use this one. In order to output text to the monitor, it’s placed within the function parentheses and bounded by quotes. The end result is that whatever is included between the quotes will be showed on the monitor when the program is run.

Notice the semi-colon; at the end of the line. C uses a semi-colon as a statement terminator, so semi-colon is required as a signal to the compiler that this line is complete. This program is also executable, so you’ll be able to compile and run it to see if it does what you think it should. With some compilers, you may get an error message while compiling, indicating that the function printf () should have a prototype.

#include<stdio.h>
#include<conio.h>
int main ()
{
printf (“Welcome to C language”);
return 0;
}

Here you’ll be able to see #include at the beginning of the program. It is a pre-processor directive. It’s not a part of our program; it’s an instruction to the compiler to make it do something. It says the C compiler to include the contents of a file, in this case the system file stdio.h. This is the name of the standard library definition file for all Standard Input Output. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. stdio.h is the name of the file in which the functions that we want to use are defined. A function is simply a group of related statements that we can use later. Here the function we used is printf . To use printf correctly C needs to know what it looks like, i.e. what things it can work on and what value it returns. The actual code which performs the printf will be tied in later by the linker. Note that without the definition of what printf looks like the compiler makes a guess when it sees the use of it. This can lead to the call failing when the program runs, a common cause of programs crashing.

The <> characters around the name tell C to look in the system area for the file stdio.h. If I had given the name “abc.h” instead it would tell the compiler to look in the current directory. This means that I can arrange libraries of my own routines and use them in my programs.

Imagine you run above program and then change it and run it again you may find that the previous output is still stucked there itself, at this time clrscr(); would clear the previous screen.
One more thing to remember while using clrscr() is that it should be called only after the variable declaration, like
int p,q,r;
clrscr()

Here is an example of minimal C program that displays the string Welcome to C language (this famous example included in all languages ​​moreover been done originally in C in 1978 from the creators of the language, Brian Kernighan and Dennis Ritchie) Example

#include<stdio.h>
#include<conio.h>
int main ()
{
clrscr();
printf (“Welcome to C language”);
return 0;
}
When you execute above program you won’t see ‘Welcome to C language’ on the console because the screen will just flash and go away .If you want to see the line you can use getch() function just below the printf() statement. Actually it waits until a key is pressed.

C – SIMPLE C PROGRAM EXAMPLE :

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf(“Welcome to C Language”);
getch();
return 0;
}

C Programming
↑  ↓

Stranice (1):1


Sva vremena su GMT +01:00. Trenutno vrijeme: 8: 56 am.