PDA

View Full Version : Lesson 02 : C Programming


x-pert
07-01-2007, 06:20 PM
Hi guys...


This is the second lesson of C Programming. If you haven’t read the first one please read it before reading this. Coz in the first one I have mentioned what is programming kinda thing... (http://www.elakiri.com/forum/showthread.php?t=35788)


In this second lesson I’m gonna tell you how to write and compile the first C program. Dont worry about the syntax and rules and all the things... Just try to write it and compile and run... Eventually I will tell the meaning of each and every line and stuff like that...


You need to have a compiler to compile the program. Compiler changes the source code written in C language by you to a language understandable by the computer. So after you write a C source code, you have to compile it, then only you can run the program in a computer.


There are two main kinds of compilers: 1) for UNIX environment 2) for MS DOS/Windows environment.


UNIX
You will have to use a command line compiler such as UNIX compiler (cc) or Free Software foundation’s compiler (gcc)

DOS/ Windows
You can use Turbo C++. Borland C++ or Micro$oft visual C++ compilers


I’m not gonna talk about UNIX compiling method, as most of the EK members are using windows as I guess.... But if you wanna know that method pls let me know. I can put it too.

So lets see how to create and compile and run a C program in Windows environment...

First open a notepad and type the following command. :)

#include <stdio.h>
int main()
{
printf("Hello World\n");
return (0);
}


Dont worry guys... Will explain the codings later.

Then save the file as hello.c

Save it in the C:/ directory for the moment.


Compiling with Borland C++ compiler.Go to start->run->cmd
Or open the command prompt.
Type
C:> tcc -ml -v -N -w -ehello hello.c

tcc means Turbo C++ compiler
-ml means that we are using the large memory model (Dont think about it now)
-v tells the compiler to put the debugging info on the program
-N turns on the stack checking
-w turns on the wrnings
-ehello hello.c tells the compiler to build a program called hello from the hello.c source code.

Using the Borland C++ compiler
Same as above just type
C:> bcc -ml -v -N -w -ehello hello.c(tcc changes to bcc)

Using Micro$oft visual C++ compiler
Same procedure but type
C:> cl /AL /Zi /W1 hello.c

/AL tells the compiler to use the large memory model
/Zi turns on the debugging
/W1 turns on the warnings


To execute the program you just wrote and compiled, you have to type

% hello

With any compiler, you can use this command to execute your program.

Then Hello World should appear on your screen.

Now, If you save your program in a different location lets say in a folder called ElaKiri in your C:/ drive,
Then when you are compiling the program you should first go to that directory
Ex: C:/ElaKiri > bcc ehello hello.c
Thats why I told you to save the program in C for the first time.. ;)

ElaKiri... Now you have done the programming in the hard way...

There is an easy way :P :P :P :P :lol: :cool:
Always better to learn the hard way. (In UNIX thats the easy way)


If you have the softwares (Not just the compilers but the IDEs) such as Visual Studio 6, Turbo C++ or whatever C or C++ programming software, then you can easily type the source code in there and execute it...
I ll tell only Micro$oft’s way.... which is bit easy I beleive.

Open Visual studio.
Go to Project->new project
Create a new c application. (Actually I haven’t used it for 3 years now. So can’t remember the exact path. Just create a new project some way or the other.)

Kauru hari danna ekkenek innawanam kiyanna... habai loku case ekak nemei... easy podi wadak... Kohoma hari new project ekak patan arganna thiyanne....

Project type will be QuickWin application.
After creating a new project type the source code

#include <stdio.h>
int main()
{
printf("Hello World\n");
return (0);
}

Go to File->save and save the project which you have already created.
Then go to Project->build and it will build the source code
Then you can start/run the program.

Thats it for now...
Next time we’ll review some source codes.. So that you will get a flavour of C :D



Source : Oualline, C. Practical C Programming, 3rd edt., O’reilly Publications

ashan_ok
07-01-2007, 06:24 PM
ammata siri...elanee...thnx aiyya!! ;)

x-pert
07-01-2007, 06:26 PM
ElaKiri malayo... hehe :)

Gahapan ikmanata 4k... :P

Pushpe
07-01-2007, 06:43 PM
mewa mata nam therenne nae eath hugak hoda deyak :D :yes:

sanjaya
07-01-2007, 06:52 PM
include <iostream>
include <cstring>
using namespace std;

int main()
{
char name[20]="Elabuwa";
cout<<"Keep it up bro"<<endl;
cout<<" "<<name<<endl;
return 0;
}

:lol: :lol: :lol: :lol: :lol: :lol: C pas pas :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:

hul2000
07-01-2007, 09:01 PM
Thanks!

Aphoo
07-01-2007, 09:03 PM
Thnx...

Lich
07-01-2007, 09:06 PM
Ela .. Thanks a lot... Compile karanna Visual Studio nae.. eka heta hamba unama balanawa...

hul2000
07-01-2007, 09:06 PM
Here are some useful links for C++

C++ Tools
http://www.freebyte.com/programming/cpp/

C++ Related Links
http://www.robertnz.net/cpp_site.html

C++ Compiler Comparison Chart
http://cmeerw.org/prog/freecpp/

ICE
07-01-2007, 09:07 PM
ela macho..

keep up d gud work

Mihindu_Gajaba
07-01-2007, 09:11 PM
thanks for the details machan

ICE
07-01-2007, 09:12 PM
include <iostream>
include <cstring>
using namespace std;

int main()
{
char name[20]="Elabuwa";
cout<<"Keep it up bro"<<endl;
cout<<" "<<name<<endl;
return 0;
}

:lol: :lol: :lol: :lol: :lol: :lol: C pas pas :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:

macho c valin liyapan.. meka C thread 1k ne.. :lol: :lol: :lol:

shalinda
07-01-2007, 09:13 PM
shaaaaaaaaaaaaaaaaaa thxxxx

ICE
07-01-2007, 09:16 PM
Dev-C++

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.

http://www.bloodshed.net/images/devcpp5_scr.jpg


Free downlaod (http://www.bloodshed.net/devcpp.html)

This is a very easy to use IDE for windows for C and C++...

x-pert
07-02-2007, 06:23 AM
mewa mata nam therenne nae eath hugak hoda deyak :D :yes:


haha :P

Malak gahala wareng.. ethakota theerei.... :P

x-pert
07-02-2007, 06:24 AM
include <iostream>
include <cstring>
using namespace std;

int main()
{
char name[20]="Elabuwa";
cout<<"Keep it up bro"<<endl;
cout<<" "<<name<<endl;
return 0;
}

:lol: :lol: :lol: :lol: :lol: :lol: C pas pas :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:


#include <stdio.h>
int main()
{
printf("No worries machang\n ElaKiri\n");
return (0);
}

x-pert
07-02-2007, 06:25 AM
hul2000, Aphoo, ICE >> Thanks machangzzz.... ;) :cool:

x-pert
07-02-2007, 06:26 AM
Ela .. Thanks a lot... Compile karanna Visual Studio nae.. eka heta hamba unama balanawa...


Thanks kiyawanawata machang.. :yes:

Anna ICE daala thiyanawa Developer Soft ware ekak.. eeka DL karagaththanam hari wade... ;) :cool:

x-pert
07-02-2007, 06:27 AM
mihindu_gajaba, Shalinda >> Thanks machangzz... Thawa enawa issarahata.. digatama set wenna machang..

x-pert
07-02-2007, 06:28 AM
Dev-C++

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.

http://www.bloodshed.net/images/devcpp5_scr.jpg


Free downlaod (http://www.bloodshed.net/devcpp.html)

This is a very easy to use IDE for windows for C and C++...


Ammatasiriii.... Me thiyanne gindara baduwak.... ElaKiri work ICE bro...

Thanks for the support machang...

SAN_APIIT
07-02-2007, 06:41 AM
Dev-C++

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.

Free downlaod (http://www.bloodshed.net/devcpp.html)

This is a very easy to use IDE for windows for C and C++...

This is my favorite tool for C++, even though u can code c coding here don't use this for c. some bugs when using pointers.... :(

SAN_APIIT
07-02-2007, 06:44 AM
#include <stdio.h>
int main()
{
printf("No worries machang\n ElaKiri\n");
return (0);
}

Well first of all really great writing... :yes:
I think u need to publish a book...:)
Elakiri, this will help lot of members, keep it up...:D


PS- why did u use a int main() if u have nothing to return....? :lol:

x-pert
07-02-2007, 06:47 AM
Well first of all really great writing... :yes:
I think u need to publish a book...:)
Elakiri, this will help lot of members, keep it up...:D


PS- why did u use a int main() if u have nothing to return....? :lol:


Wait wait.... still learning... After getting my PhD will deffa publish some books :) :lol:

No machang... Int main and return o I use as a standard method bro...
Or else its kinda confusing...
You will get to know why I did it after a while...

SAN_APIIT
07-02-2007, 06:54 AM
Wait wait.... still learning... After getting my PhD will deffa publish some books :) :lol:

No machang... Int main and return o I use as a standard method bro...
Or else its kinda confusing...
You will get to know why I did it after a while...

I was talking about ur writing skills. fattane..;)

No need to do it... just use a void ne. :lol: ,
I can hardly think of a main returning to a function unless u use a recursive...
Any way we have c in our first year....:)

x-pert
07-02-2007, 06:57 AM
I was talking about ur writing skills. fattane..;)

No need to do it... just use a void ne. :lol: ,
I can hardly think of a main returning to a function unless u use a recursive...
Any way we have c in our first year....:)


Yes machang... But this is the standard way of doing it bro... Earlier we used void. But now due to the portability and re-engineering aspects we standardised it to use it as int and return 0.

:)

Glad to have you here machang... Correct me if I do some mistake bro ok...

hmmm.. writing skills... hehe First one was not my writing... most of them are from a book. But second one I did.... Glad you like it bro...
From now on... Its completely my writing... Comment on them later...

SAN_APIIT
07-02-2007, 07:03 AM
Yes machang... But this is the standard way of doing it bro... Earlier we used void. But now due to the portability and re-engineering aspects we standardised it to use it as int and return 0.

:)

Glad to have you here machang... Correct me if I do some mistake bro ok...

hmmm.. writing skills... hehe First one was not my writing... most of them are from a book. But second one I did.... Glad you like it bro...
From now on... Its completely my writing... Comment on them later...

Ela machchang... :yes:
keep it up bro... :D

ICE
07-02-2007, 07:03 AM
This is my favorite tool for C++, even though u can code c coding here don't use this for c. some bugs when using pointers.... :(

thanx for the info machan.....:yes: :yes:

i havent used this software for many programs since im always using linux for programming...

i post about this for windows users who r not familiar with linux environment.

SAN_APIIT
07-02-2007, 07:11 AM
thanx for the info machan.....:yes: :yes:

i havent used this software for many programs since im always using linux for programming...

i post about this for windows users who r not familiar with linux environment.

Elane... open source kollec wage...:lol:
Any way the best compiler i found for C is BorlandC 3.1. witch has a dos mode compiler and a windows IDE. (Graphic.h supported)

For C++ borland 5.5 and DEV-CPP thama. :D

x-pert
07-02-2007, 07:12 AM
Ela machchang... :yes:
keep it up bro... :D


Thanks machang....

ICE
07-02-2007, 07:14 AM
Yes machang... But this is the standard way of doing it bro... Earlier we used void. But now due to the portability and re-engineering aspects we standardised it to use it as int and return 0.

:)


ohh z that the reason....

i thought i was for the OS 2 determine the program executed correctly.

thank u weva machan..

x-pert
07-02-2007, 07:17 AM
ohh z that the reason....

i thought i was for the OS 2 determine the program executed correctly.

thank u weva machan..


Thats another reason... Then we can use another wrapper program above the C program to catch the exception and stop crashing the program due to an error...

You are correct too.... :cool:

ICE
07-02-2007, 07:18 AM
Thats another reason... Then we can use another wrapper program above the C program to catch the exception and stop crashing the program due to an error...

You are correct too.... :cool:

happa math harida....

ela ela....:lol: :lol:

SAN_APIIT
07-02-2007, 07:20 AM
happa math harida....

ela ela....:lol: :lol:

:rolleyes: :lol: :lol: :lol:

Hama welawema waradinne nane ban....:lol: :lol: :lol: :lol:

ICE
07-02-2007, 07:21 AM
:rolleyes: :lol: :lol: :lol:

Hama welawema waradinne nane ban....:lol: :lol: :lol: :lol:

ou ou ban....:lol: :lol: :lol:

x-pert
07-02-2007, 07:22 AM
:rolleyes: :lol: :lol: :lol:

Hama welawema waradinne nane ban....:lol: :lol: :lol: :lol:


:P :P :P :P :P :P ;)

MCLINER
07-02-2007, 08:07 AM
wow x-pert great wrk men......umba RAHATH wenna one..

x-pert
07-02-2007, 09:50 AM
wow x-pert great wrk men......umba RAHATH wenna one..


Rahath unoth kauda me lesson eka complete karanne :P :P :P :P

x-pert
07-02-2007, 06:34 PM
Need more comments

ICE
07-03-2007, 08:52 PM
onna damma comment 1k ...

habai mukuth nah .. :lol:

nEoN_wHitE
07-03-2007, 09:04 PM
ammata siri.. maxxa ne.. thanx macho:D

SAN_APIIT
07-03-2007, 09:11 PM
Ela machchang keep it up.....

x-pert
07-04-2007, 06:20 AM
ammata siri.. maxxa ne.. thanx macho:D


Welcome machang... ;) hehe ;)

x-pert
07-04-2007, 06:20 AM
Ela machchang keep it up.....


Deffaaa.. Digatama karanawa.... digatama set wela inna machang... ElaKiri...