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