The Seven Steps to Writing a Simple C Program

   


The Seven Steps to Writing a Simple C# Program

After you have successfully installed the .NET SDK and chosen a suitable editor you are ready to write and run your C# programs. This can be accomplished by performing the following seven steps (shown here by using pseudocode):

  1. Open the command console.

  2. Use the chosen text editor to type and edit the C# source code and save this text as a .cs file.

  3. Use the C# language compiler to turn this source code into a PE (.exe) or DLL (.dll) file.

  4. If the compiler detects any syntax errors in step 3, go back to step 2 and correct these errors. Continue through steps 3, 4, and so on.

  5. Run the program.

  6. Verify that the output matches your expectations. If this is not the case, go back to step 2 and correct the mistakes in the C# code. Then continue through steps 3, 4, and so on.

  7. Time to celebrate your first C# program.

Let's take a closer look at each of these steps.

Opening and Using the Command Console (Step 1)

The command console is like a control center. It allows the user to operate the computer completely by typing special commands with the keyboard.

Throughout this book, you will be using the command line console to launch Notepad, create directories, copy files, initiate the C# compiler, execute your programs, and view their output. The source code itself is written inside Notepad.

You can open the command console by first selecting Run option on the Start menu and then typing cmd (see Figure 2.8) in the text field. Click OK. You should then see the command console appear, as shown in Figure 2.9. In general, commands are typed after the C:\> prompt and executed by pressing the Enter key on the keyboard. In this text, we refer to the C:\> prompt, but this could also be D:\> or E:\>, depending on the disk drive you are using.

Figure 2.8. Type cmd in the Run window.
graphics/02fig08.gif
Figure 2.9. The command console window.
graphics/02fig09.gif

Appendix F, "Using DOS Ccommands in the Console Window" explains the basic command console options you will need.

Note

graphics/common.gif

The conventions used in this book for explaining how to give commands in the command console are illustrated by this example:

 C:\>cd mydirectory <enter> 

The C:\> part, and any other text that is printed by the computer, is written with normal text. The part you type, in this case cd mydirectory, is bolded. Any named keys are indicated with <> angle braces. <enter> here for example means that you have to press the Enter key.


Before writing the C# source code, you need to create a directory where it can be stored. Create a directory called MyC#Programs for this purpose with the following command:

 C:\>md MyC#Programs<enter> 

Enter this new directory by typing the following:

 C:\>cd MyC#Programs<enter> 

Typing and Saving the C# Source Code (Step 2)

Use the chosen text editor to type and edit the C# source code and save this text as a .cs file.

First you need to start Notepad by typing the following command in the console window::

 C:\>MyC#Programs>notepad<enter> 

You will see Notepad appear, as shown in Figure 2.10.

Figure 2.10. The Notepad window.
graphics/02fig10.gif

You are now ready to write the C# program in the text editor.

Our first C# program will print out the following lines on the command console:

 Though this be madness yet there is method in it William Shakespeare 

Even though the quote was written many years ago, before the advent of computers, it might still be a good description of what most people feel like when they learn a new programming language.

Listing 2.1 contains a C# program that will print the quote by Shakespeare on the command console.

The line numbers and colons appearing on the left side of Listing 2.1 are not part of the program. They are included to help me refer to specific lines by referring to their numbers and are not to be included when you type in the program with the editor. However, all other details, such as semicolons, parenthesis, and braces, must be entered exactly as shown. The C# compiler is case sensitive, meaning Main, main, and maiN are all different. Consequently, you must capitalize all letters exactly as written in Listing 2.1.

Note

graphics/common.gif

Typing mistakes will likely cause syntax errors and will be reported by the compiler.


You are not meant to understand the C# program shown in Listing 2.1. It is only intended to show you the mechanics of how to write, compile, and run a C# program. However, it can still be enlightening to read through the code and make a guess about what is happening in each line. Later, you can compare your guesses with the brief explanations of each line that I provide.

Listing 2.1 Source code of Shakespeare.cs
01: using System; 02: public class Shakespeare 03: { 04:     public static void Main() 05:     { 06:         Console.WriteLine("Though this be madness"); 07:         Console.WriteLine("yet there is method in it"); 08:         Console.WriteLine("William Shakespeare"); 09:     } 10: } 

After you have finished typing the program, your Notepad window should look like Figure 2.11.

Figure 2.11. Notepad window displaying the Shakespeare.cs source code.
graphics/02fig11.gif

Save the program by selecting Save As on the File menu of Notepad and typing Shakespeare.cs in the File Name text box. Your screen should now look similar to Figure 2.12. Then click the Save button.

You are now ready to activate the C# compiler and turn the brand new Shakespeare.cs file into a PE file.

Note

graphics/common.gif

When your source code is ready to be compiled, you can turn it into a PE file (.exe file extension) or a DLL file (.dll file extension), depending on the command you give the compiler.


Turn the Source Code into a PE (.exe) File (Step 3)

Initiate the C# compiler by giving the following command:

 C:\MyC#Programs>csc Shakespeare.cs<enter> 

Now hold your breath and wait with excitement…if the compiler detected no syntax errors, it will print two lines stating the compiler version and a copyright message, similar to what is shown in Figure 2.13. Notice that the compiler version and other details might vary, the important part is that no error messages or other complaints were printed.

Figure 2.13. No errors detected by the compiler.
graphics/02fig13.gif

Note

graphics/common.gif

A successful compilation does not mean your source code is free of errors (bugs). It merely means that the source code represents a valid C# program free of syntax errors.


In the event of a successful compilation, the compiler creates a file called Shakespeare.exe and saves it in the same folder that contains Shakespeare.cs. Shakespeare.exe is a PE file and is also an assembly containing MSIL and a manifest (metadata).

But what if a typing error had sneaked its way into the source code?

If the Compiler Detects Syntax Errors (Step 4)

Let's have a bit of fun with the compiler and watch how it responds to a syntax error.

  • Start Notepad again by typing the following:

     C:\MyC#Programs>notepad Shakespeare.cs<enter> 

    Create an error in the code by removing the semicolon from the end of line 1 of Listing 2.1. Line 1 should now look like the following:

     01:  using System 
  • Save the Shakespeare.cs file by selecting Save from the File menu of Notepad.

  • Initiate the compiler as shown in the next line and see if it can find the missing semicolon.

     C:\MyC#Programs>csc Shakespeare.cs<enter> 
  • You should see the following line as part of the output from the compiler:

     Shakespeare.cs(2,1): error CS1002:   ;   expected 

    It did, in fact, catch the syntax error! Notice the parentheses (2,1) in the compiler output. This is an indication of where the compiler thinks the error is located and can often be of great help when correcting errors. 2 refers to the line number and 1 to the character number.

    But wait, we removed the semicolon from position (1,13). Shouldn't the compiler have given this position instead? Well, the compiler looks a bit differently at the source code than we humans do. Whether we put a semicolon in position (1,13) or position (2,1) is not significant for the compiler. Its only concern is whether the semicolon is between System in line 1 and public in line 2. For that matter, we could put the semicolon in position (1,14), (1,15), or so on.

To enhance the understanding for humans viewing the source code, various styles are being used when writing it. Listing 2.1 follows a specific style of presenting the source code. For example, putting the semicolon in position (1,13) looks much better and clearer than putting it in position (1,34). Style is discussed further in Chapter 3.

Let's return to our Shakespeare.cs file. Use Notepad to put the missing semicolon back so that the source file looks exactly like Listing 2.1 again. Compile the source code as already shown and verify that the compiler reports no errors. It is now time to execute your Shakespeare.exe file.

Run the Program (Step 5)

To run the PE file Shakespeare.exe, you type the following command:

 C:\MyC#Programs>Shakespeare<enter> 

The JIT compiler is activated and machine language is emitted that is then executed, resulting in the now familiar lines

Though this be madness

yet there is method in it

William Shakespeare

Verify the Output (Step 6)

Did you get the correct output? If the answer is no, then go back to step 2 of the seven steps. Compare your source code with Listing 2.1 and find the mistake. Correct the code with the editor. Even if you only change a semicolon in the source code, you still need to go through step 3, 4 and so on until the program is working.

If the answer is yes, then congratulations! You have successfully executed your first C# program. You can move to the last step.

Time to Celebrate (Step 7)

Now it's time to celebrate !


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net