How do we write a program in Command line development environment?

14,014

Solution 1

DOS is not dead.... yet!

fahad

There are a number of methods by which you can enter code in DOS (see EDIT further on down).


(1) You can send keystrokes directly to a file

You do this by redirecting output to CON (the console) to a file. The only oddity of this method is that you end the 'session' by entering a CTRL-Z when you are finished.

It's basic, but this is how it goes.

Firstly, suppose you want to display "Hello World" on the screen, a simple batch file containing the following two lines is all that is required:

   @echo off
   echo Hello World

The '@echo off' is commonly found at the start of all batch files. It simply instructs the command interpretter NOT to display each command as it is being executed (or parsed).

One more thing before we start. Throughout this answer, I will assume your program is named 'helloworld.bat'.

Enter the following lines one after the other pressing the ENTER key at the end of each line:

   copy con helloworld.bat
   @echo off
   echo Hello World
   ^Z

The '^Z' is displayed when you press the CTRL-Z key combination (don't forget to press the ENTER key as well).

When you press the ENTER key after CTRL-Z, DOS displays the familiar '1 File(s) copied' messege.

You can now execute the batch file program by simply entering the program's name like this:

   helloworld

And DOS will display the following:

   Hello World

It can't get any more basic than that.


(2) You can use DOS' EDIT program

This is a DOS based IDE retained from around the mid-90's. Simply enter the following command:

   edit

And EDIT will open in the same DOS window. When you close EDIT, you are returned back to DOS again.

EDIT also works with your mouse.

Once EDIT opens, enter the following two lines of code:

   @echo off
   echo Hello World

Then, click on [File], [Save], type: 'helloworld.bat' in the "File Name" input field, use your mouse to change directories in the "Directories:" pane if you want to, then click [OK]. To return to DOS, click [File], [Exit].

EDIT version 4.5 (I think) was context-sensitive and displayed code using different colours to seperate key word, different data type, symbols etc.


(3) Use Windows' built-in Notepad

This is simple. At the command prompt, enter the following command:

   notepad

And Notepad will fire up. It's a simple text editor and does the job when entering small programs.


(4) Use Notepad++. It's FREE!!

Notepad++ is the programmer's choice. It's free, full of useful features and customisable. Find it on the net by searching for "notepad++".


Solution 2

You simply use whatever text editor you like to create the C sourse file(s) then invoke the compiler command line(s) to compile and link the program (typically, an IDE is doing exactly that, but in a behind-the-scene manner). How the command line is invoked depends on the exact toolchain you're using.

You might also need to set up an environment for you particular compiler toolchain (the right paths and various other env variables might need set up).

For Visual C++ the environment might be set up using a batch file installed by Visual Studio:

vcvarsall x86

Invoking the compiler could be as simple as:

cl helloworld.c

or for C++ (for some reason it issues a non-fatal warning if you don't give it an option configuring details about how it should implement exceptions):

cl  /EHsc helloworld.cpp

The particulars are very dependent on the compiler you're using - you should read the docs for that compiler.

Also, the options you use depend on your particular situation and needs. Scripts/batch files and/or makefile can help you manage the complexity of the options you might need to use.

Solution 3

From your comment, "Just some knowledge so I can say that I know one way to do programming without IDE" I would say learn to write simple batch files. They can be run from Explorer but they exist as a holdover from the DOS days.

Start a command prompt window (Start->Run->'cmd'), this will open a window and show a prompt, most likely "c:\" or some other path.

Type the following command (followed by )

echo "Hello World"

You should see:

"Hello World"  
c:\

Now, using whatever editor you'd like, create a text file with that command as the only line. Name the file "hello.bat". When you are at the command prompt you can execute the batch file like so:

c:\hello.bat
"Hello World"
c:\

You have now programmed using the DOS command line. For more commands and such, start with the help system.

c:\help

Which will display all the available commands for your batch file.
Microsoft has an online reference here.

Share:
14,014
GreenMatt
Author by

GreenMatt

An IT guy working in scientific environments and trying to make the world a little better place.

Updated on November 20, 2022

Comments

  • GreenMatt
    GreenMatt over 1 year

    I have been writing my code in IDE,I just read that there also existed a Command Line Development Environment in which the code is written in DOS.I googled but found no results on how to use the command line development environment.My OS is Windows XP.I would be very thankful for your help me write the hello world program in DOS and also explain how to run it.

    • wkl
      wkl over 13 years
      Are you talking about compiling code on a command line, or an actual DOS-based IDE or console text editor?
    • Admin
      Admin over 13 years
      There is no IDE in DOS as much as I know.I read that there exists a command line environment and I wanted to know how to use that.
    • David
      David over 13 years
      @fahad: What do you want to do with "a command line environment." You have the Windows Command Prompt, Windows PowerShell, Cygwin, various other command-line tools, etc. available. There is no "one" command line environment. And even within such an environment, you need to use various additional tools to accomplish particular tasks. So, what tasks are you looking to accomplish?
    • Admin
      Admin over 13 years
      Just some knowledge so I can say that I know one way to do programming without IDE.
    • GreenMatt
      GreenMatt over 13 years
      @fahad: There were IDE's on DOS systems before Windows. E.g. Turbo C was an IDE that ran under DOS.
    • Admin
      Admin over 13 years
      @GreenMat:Thanks for the info, my bad