Sunday, October 21, 2007

Using the Microsoft Visual Studio C/C++ Integrated Development Environment (IDE)

Why C ?
C and C++ are both procedural languages. A procedural language is one in which you must specify the order in which the computer should execute instructions. C is a particular form of procedural programming language known as a block structured programming language. As a block structured programming language C allows you to structure a program into groups of instructions, or blocks, that can be repeatedly used. Exactly what this means will become clear later in the course. If you have programmed before then you will understand that the ability to define block structured program code is an important property of C. C is in many ways similar to the programming language Pascal. If these concepts are new to you don’t worry they will be explained during the course. You might have heard of another programming language, C++. C++ differs from C in the way that data and programs are associated. C uses program modules and structured data types. Structured data types allow different types of data such as a floating point value, an integer and groups of characters to be associated and given a user-defined name. This coupled with the block structuring of the program improves program clarity. C++ uses what are called Objects. Objects give C++ a number of advantages. The main advantage is to simplify the development of large, complex programs. This simplicity of program design comes at the cost of an increase in the size of the executable program and, in many cases, a reduction in the speed of execution. There is also a small increase in the complexity of the way that object-oriented programs are written, compared to block structured procedural programs.
C was introduced to program telephone systems. A telephone exchange must respond to diverse demands from users and is therefore an example of a real-time system. A telephone exchange is a real-time system of modest complexity in which speed is critical. Many real-time applications, especially those of low to modest complexity and those where speed of execution is important are more suited to implementation using a simple block structured procedural programming language such as C. In addition C allows the programmer to easily combine low-level operations that manipulate single bits in hardware and high-level operations that are concerned with searching or ordering databases in a single program. C++ preserves these benefits and makes it easier to implement complex programs and re-use software. As a result many embedded system applications are still programmed in C but this is changing as the performance of embedded computers increases and the complexity of the tasks that embedded systems are required to perform increases. It is becoming more common for embedded systems to be programmed in C++.
C++ is not the only object-oriented programming language. Java is another, much publicised, object-oriented programming language. Java is commonly used for high-level applications, to implement user interfaces and create World Wide Web based applications. Java is normally used as an interpreted language. This means that instructions are translated into computer actions line by line. Interpreted programs operate more slowly than compiled programs. However, interpreted programs are more readily moved between computer systems than compiled programs because hardware specific aspects of the actions associated with a program are determined in-situ. The syntax of both C++ and Java has much in common with C.
What is an Integrated Development Environment (IDE) ?
An integrated development environment is a set of tools for software development which commonly combine: a text editor, compiler, linker, debugging tools, profiler, version control and dependency maintenance. Consequently an IDE can appear to be complex on first encounter, DON’T PANIC.
Most people however only use a small part of the tool set and develop their understanding progressively, as needed. The essential basics are to understand how:
1.Programs are entered - the editor
2.Programs are compiled and linked
3.To manage program dependency
Getting started
Visual Studio gives you less control over the detailed setup of the C programming environment than some other IDE’s. This is both a blessing and a potential problem. It is a blessing because it simplifies initial use of the IDE. It is a problem because it is harder to achieve specific configurations when they are required. For example it is not obvious how to set the compiler, from the IDE to treat a source file as an ANSI standard source file, i.e., one which conforms strictly to the ANSI C standard definition of the C programming language syntax. This is important because compilers produced by each software house tend to adopt particular variations from the ANSI standard. Compliance to the ANSI standard can be used to ensure the portability of applications development between software platforms. The differences between individual vendors syntax for C and the ANSI standard are normally small but significant. A far greater and more common confusion is between the syntax rules for C++ and C. If you adopt the syntax rules for C++ when compiling a C program many detailed errors and potential errors of syntax will not be flagged. This will lead you into a false sense of confidence about the correctness of the program that you are developing and you may find that a program which appears to compile ok misbehaves when executed because you have used an inappropriate programming construct for C which is acceptable for C++ and therefore you are not warned of a potential error.
For most of the programming that you will perform in this course you will produce what are called “Console” applications. These are programs that do not use the windows programming interface and require a dos- or unix-like command line prompt to operate. This style of programming is being adopted so that we can concentrate on the principles of programming in C. Learning to program for Windows requires a separate course. In addition “Console” application programs are the type of programs most commonly used in embedded systems.
Starting the Microsoft Visual Studio IDE
The Microsoft Visual Studio IDE is a complex suite of software development tools for creating C, C++, Java and web-based programs, plus component software for a range of application packages. When writing C programs you will use a small subset of this functionality. It is important not to be confused or distracted by the complexity and diversity of tools in the IDE. Fig. 1. shows how the Visual Studio IDE is launched from the Windows 2000 screen. Using the left mouse button select in sequence: the “start” button on the bottom left of the screen, “Programs”, “Microsoft Visual Studio 6.0”, “Visual Studio C/C++”. In a few moments you should see a window headed “Microsoft Visual Studio C/C++ Vers 6.0”. In the following notes we will indicate a chain of selections like this using the following notation: “Start|Programs| Microsoft Visual Studio 6.0|Visual Studio C/C++”.
Creating a New Project
A project is a collection of files and compilation instructions. The project file defines how files depend on one another so that when one file is changed the appropriate files are compiled and linked together. Initially you will create simple projects with a single source file and a single target executable file. The equivalent facility is provided under Unix by a “makefile”. The Build process in Microsoft Visual Studio can generate a makefile that can be invoked from the command line. A make file can be useful when you want to define to the program build process for another person to use.
To create a New Project select “File|New” (Fig. 2) and selecting the “Project” tab, as shown in Fig. 3. You will then see a window like that in Fig. 3(a). Select “Win32 Console application”. Also enter a path to where you wish your project folder to be created and a project name. All files will be stored under a folder created with the name of the project. When you have dismissed this window by clicking on OK a window like that in Fig. 3(b) will appear. It is simplest to create an empty project folder. Dismiss the “New Project Information” screen that appears.
Files (and folders) are added to a project from the “File View” aspect of the left hand pane either by specifying existing files or a new file name. Note that under the named project files are folders for “source”, “header” and “resource files”. You will not need to add any resource files for Console applications and initially will not need to add any header files until you use separate compilation to create user defined libraries and large application programs. Primarily you will create a single source file that contains the main program. When creating the source file remember to use the file name extension “.c” to signify a C program file. This will cause the compiler to use the C programming language syntax. To do this, in the “File View” of the left pane, expand the Project folder and right click on the “Source Files” folder then select “Add files”, as shown in Fig. 4. Enter the file name in the “Insert Files into Project” window (use the “.c” filename extension) and click on “OK”. If creating a new file click “OK” on the file creation prompt that appears. To enter content into the file, select the “File View” pane, expand the “Source File” folder then double click the left mouse button on the program source file name. A window will appear in the pane on the right. Click the left mouse button in this window and type in the contents.
You are now ready to enter the text of your first program. In this window type the text shown in Fig. 6(a). Save the file to disk and left click on the icon on the toolbar (or select “Build|Build All”) to compile and link the program. Click on the “!” icon to execute the program. This program opens a simple “Console” window to run the program in, prints a line of greeting then waits for you to press any key. You must also select the “Console” window as the focus by clicking the left mouse button in that window. This will cause the next key that you press to be input for the program in that window. After you have pressed the key the program will terminate and close the “Console” window.
When compilation or linking errors occur a separate message window opens. If you click on a warning or error message for compilation the corresponding line in the program source file will be highlighted.
Opening an Existing Project
To open an existing project and associated files select “File|Open Workspace”. A file browser window will appear opened to the location of the workspace last opened. To look for another workspace navigate down the file system from here and select the appropriate workspace file, “*.dsw”, for opening.
On-line Help
On line help is available by selecting “Help|Index”. In the searchable index enter and select for display “C Language Reference”. This will allow you to browse documentation on the C programming language. Then select “Organisation of the C Language Reference” in the right hand, display pane. This is a very formal description of the C language syntax. It is a useful reference but not designed the early stages of learning to program in C. In addition pressing “F1” when the cursor is in a function name or data type definition will provide help on that topic.
When you first start programming there is no substitute for careful preparation and having a good text book at your side.
A Brief Glossary
ASCII Formally this is an abbreviation for The American Standards Committee II. In particular it is a set of integer codes used to define the characters used for formatted input and output on a computer. The character set includes the normal printable characters and a number of special control codes.
Compiler: A compiler is a program that converts a source file into a binary set of machine (computer) readable instructions.
Console Window:
This is a window that displays simple text with no graphics. It is also referred to as an MS-DOS Window because it looks like the screen presented by the now obsolete MS-DOS operating system.
Executable File: This is the executable set of binary machine readable instructions produced by a linker.
Linker: A program that connects together a set of machine readable binary files to form a program that can be executed.
Linking: The process of connecting a set of machine readable binary files to form a single program file that can be executed.
MS-DOS: An abbreviation for the MicroSoft Disk based Operating System. Some programs emulate this mode of operation using a “Console” window. The MS-DOS operating system was replaced by Windows 95.
Object File: This is the set of binary machine readable instructions produced by compiling a source file.
Source File This is a file of readable characters that define the operations of a computer.


Fig. 1. Starting the Microsoft Visual Studio 6.0 C/C++. Click the left mouse button over the start button and each menu item highlighted above until you reach the Microsoft Visual Studio C/C++ option. Clicking here will launch the Microsoft Visual Studio IDE for C/C++.

Fig 2. Creating a new project: Select “File|New”.

(a) (b)
Fig. 3. Microsoft Visual Studio 6.0 C/C++: To make a new project select “File|New”. a) This window will appear. Select the “Project” Tab to obtain this window. To create a Win32 Console application select that option in the window on the left. Select a directory path and a project name. The project name will be used for another directory and the name of the main program file (e.g. Program 1.c). b) This window will then appear. It is normally best to create an empty project. Click Finish then a summary window will appear. Dismiss this window.

Fig. 4. Adding a file. When the type of program has been selected and a skeleton project created you can start adding files. The first file to add is the main program file. Switch to “File View” using the tab at the bottom of the left pane. In the “File View” pane click on the plus sign to the left of the project files folder to reveal a pre-defined list of folders. Then right click on the “Source Files” folder and enter the filename in the pane in the middle of the screen. Remember to use the file name extension “.c”. This ensures that the C language syntax rules are used for compilation. After clicking “OK” a file creation prompt will appear; click on “OK”.

Fig. 5. Adding file content. When a file tag has been added to the workspace folder, as above, then a window in which content can be added is opened by double clicking the left mouse button over the file name tag in the pane on the left. Content is added by pacing the cursor in the window which appears on the right and clicking the left mouse button.
(b) (c)
Fig. 6. You might like to try the simple programs listed above. Their structure and operation will be discussed in the first lectures. a) This is the simplest possible program. It does not perform any visible action and has a syntactic flaw but will start and end. b) This program is syntactically correct. It has a “return 0” statement at the end. This tells the operating system that it completed ok. However it still does not perform any perceptible action. c) This program displays a message, using “printf()”, then waits for a key press, using “getch()” before exiting.

No comments:

Post a Comment