How To Run a C Program?

Problem Analysis

Programs are written in a programming language. There exists many programming languages, C is one of them. C is a high-level language since it uses english character alphabets which makes it user friendly. Programs written in high-level language are not understood by computers. Computers understand low-level language. Low-level language is written in 0 and 1. Thus, programs written in high-level language are converted into programs using low-level language. This conversion is done by the C language Compiler.

Compiler finds program errors, if any error is found, then it lists those errors and if no error is found then compiler converts high level language programs into low-level language programs. This conversion is done on programmer request to C compiler.

Request is made by the compiler by using the Run command given in the Run menu of the Turbo C editor or by pressing Ctrl+F9 on the keyboard or using the Run button in case of online C editor.

Problem Description

Running a program is also known as executing a program. C program is executed to check for errors, if error is found then it is removed otherwise output is obtained. Obtained output is compared with the desired output if they match then the program created is correct and if the obtained output does not match with the desired output then the program contains logical errors.

Logical errors are also known as run time errors, logical errors are detected when the program output is obtained. Logical errors creep into the program when values are assigned to a variable that should not be assigned or using the wrong logic, that is,  we want to add two variable values but we are multiplying two variables. Since logical errors are not syntax errors it is detected when obtained output does not match with desired output.

To check logical errors program debugging is done and to check syntax errors program is executed. Until and unless all syntax errors are removed from the program, a programmer can not take the program into debug mode.

Following are the steps to be carried out to  check for logical error or syntax error:

Steps for syntax errors:

  • Develop a C program.
  • Save the developed program.
  • Compile the C program.

Steps for logical errors:

  • Develop a C program.
  • Save the develop program.
  • Debug the developed program.   

Solution

How to run a C program ??  The answer to this question can be given in two ways: first, illustrating the steps to compile a program and then run the program to obtain Output, second, illustrating the steps to debug the program to check for logical errors. 

First let us understand the way to run a C program by compiling the program and then running the C program.

Compiling a C program

Compilation is done to remove the syntax errors. When compilation is done the program goes into a series of transformations. To compile  a program it can be done either by using the compile menu of Turbo C editor or by pressing the Alt + F9 from the keyboard. Following are the steps to compile a C program:

    • Click on the Start button.
    • Type Run on the startup search box.
    • When rum appears then click on the run.
    • Run window opens.
    • In the run window type cmd.
    • When the command prompt appears type cd c:\TC3\bin press enter.
    • Turbo C editor opens.
    • In the turbo C editor select the File menu then select New.
    • Save the new file by using the save option in the File menu.

Compile the developed program

  • Select Compile menu in the Turbo C editor.
  • From the Compile select Compile.
  • Alternatively press ALT+F9 from the keyboard.
  • When compilation is done the Compiling window appears.
  • The Compiling window gives you the following details about the program.
  •  
  • Name of the program.
  • Total number of lines compiled.
  • Number of warnings that program contains.
  • Number of errors that program contains.
  • Total amount of memory consumed by the program.
  • Run the developed code (implicit compilation is done)
  • Select Run from the Turbo C editor.
  • From the Run menu select Run.
  • Alternatively you can also press Ctrl+F9 from the keyboard.
  • When the program is Run if the program is error free than output window is obtained otherwise compilation window appears giving you the following details:
  • Name of the program.
  • Total number of lines compiled.
  • Number of warnings that program contains.
  • Number of errors that program contains.
  • Total amount of memory consumed by the program.

Above process works in the direction to remove syntax errors from the program. To remove logical errors debugging of programs is done. 

Second way of illustrating How to run a C program is to understand how to Debug a C program. Debugging is done to check the program execution flow and to check the intermediate results.

Following are the debug features of Turbo C compiler:

  • Single Step Debugging:

Single Step Debugging is done to find errors in the program by executing one line at a time. Debugger raises errors where it occurs. Single step debugging enables programmers to isolate errors and remove them. Following are the steps to enable single step debugging:

  • Select Run menu.
  • From the Run menu select Trace. 
  • Alternatively you can press the F7 key from the keyboard.

To check intermediate results Watch is used. Watch is used to analyse intermediate results or the current value that the variable holds. By having a check on variable value the programmer can understand the change in the value of the variable that occurs during the execution of the program. Watches are put on variables during debugging to remove logical errors.

These are the steps to be followed in order to place a watch on variables.

  • Select Debug from the Turbo C editor.
  • When the Debug menu appears, select Watches, a sbmenu will appear.
  • From the submenu select Add Watch a dialog box will appear. In the dialog box type the identifier or expression on which Watch has to be placed.
  • Press enter.
  • Watch window will appear having a message that the identifier or the expression on watch is placed is undefined.
  • When debugging starts the watch window will list values variable will attain during the execution of the program.

When the length of the program is too large then conducting Single Step Debugging is the time consuming process. Thus, the programmer wants to directly jump into the position where the expected errors can occur and then start debugging from there. To start debugging from the specified position in the program Breakpoints are used.

At breakpoint program execution stops and program execution control goes into the hands of the programmer. From the breakpoint programmer can debug the program or execute the program or run the program by pressing F7 or F8. F7 will be used for Step Into and F8 is used for Step Over. 

When Step Into or F7 is used program execution control will go into the programming construct that is the program execution control will be transferred into the function definition or into the loop or into the if-else condition. 

When Step Over or F8 is used then program execution control is not transferred into the function definition or into the loop or into the if-else condition. 

Following are the steps to put Breakpoint into the program:

  • To place a breakpoint on a specific line, put a cursor on it.
  • Select the Debug menu in the Turbo C editor.
  • From the Debug menu select Toggle breakpoint.
  • Alternatively, CTRL+F8 can be used to place breakpoints.

Conclusion

How to run a C program ??  In search of the answer to this question it was revealed that there exist two ways of running a program in C language. First, is by using the Compile and Run option given in the menu of the Turbo C editor and Second, by Debugging the program. Both ways can be used to run C program.

Learning to run a program is a simple process but learning to Debug a program is like developing a skill which is attained over a stipulated period of time and by regular practice.