Compilation and Execution

Compilation and Execution

Compilation and Execution | After you have written the program, you must type it and train the machine to execute it. To type C, you need another program called Editor. After the program has been typed, it must be converted into the machine language (0s and 1s) before the machine can execute it. To perform this conversion we need another program called Compiler. The Compiler Opinion provides an integrated development environment (IDE) consisting of a publisher as well as a compiler.

There are several such IDEs available on the market targeted by different operating systems. For example, Turbo C, Turbo C ++ and Microsoft C are some of the most popular compilers running under MS-DOS; Visual C ++ and Borland C ++ are compilers running under Windows, while the gcc compiler runs under Linux.

Note that Turbo C ++, Microsoft C ++, and Borland C ++ software also contain a C compiler. If you’re a beginner, you’d better use a simple compiler like Turbo C or Turbo C ++. Once you’ve mastered the language elements, you can switch to more sophisticated compilers such as Visual C ++ under Windows or gcc under Linux. Most of the programs in this book will work with all compilers.

Wherever there is a deviation, I would emphasize this time.

Assuming you use a Turbo C or Turbo C ++ compiler here are the steps you need to follow to compile and run the first C program.

  • Start the compiler at the C> prompt. The compiler (TC.EXE is usually present in directory C: \ TC \ BIN).
  • Select New from the File menu.
  • Type the program.
  • Save the program using F2 under an appropriate name (say Program1.c).
  • Use Ctrl + F9 to compile and run the program.
  • Use Alt + F5 to view the output.

Note that when compiling the program, the equivalent of its machine language is stored as an EXE (Program1.EXE) file on disk. This file is called executable file. If we copy this file to another machine, we can execute it there without having to recompile it. In fact, the other machine does not even have to have a compiler to run the file.

A word of caution! If you run this program in the Turbo C ++ compiler, you may get an error – “The printf function should have a prototype”. To get rid of this error, perform the following steps and then recompile the program.

  • Select the “Options” menu, then select “Compiler | C ++ options ”. In the dialog box that appears, select “Always CPP” in the “Use C ++ compiler” options.
  • Select the “Options” menu again, and then select “Environment | Editor’. Make sure the default extension is “C” and not

Receiving Input

In the program discussed above, we assumed that the values ​​p, n and r were 1000, 3 and 8.5. Each time we run the program, we get the same value for a simple interest. If we want to calculate simple interest for another set of values, then we are asked to make the relevant change of the program and to compile and execute it again. Thus, the program is not general enough to calculate the simple interest for any set of values ​​without having to make a change in the program.

Furthermore, if you are distributing the EXE file of this program to someone, they may not even make changes to the program. Therefore, it is good practice to create a program that is general enough to work for any set of values.

To do the general program, the program itself should ask the user to provide the p, n and r values ​​through the keyboard during Compilation and Execution. This can be done using a function called scanf ().

This function is a counterpart of the printf () function. printf () issues the values ​​on the screen, while scanf () receives them from the keyboard. This is illustrated in the program presented below.

Compilation and Execution

/* Calculation of simple interest */
/* Author gekay Date 25/05/2004 */
main( )
 int p, n ;
 float r, si ;
 printf ( "Enter values of p, n, r" ) ;
 scanf ( "%d %d %f", &p, &n, &r ) ;
 si = p * n * r / 100 ;
 printf ( "%f" , si ) ;

The first printf () sends the message “Enter the values ​​p, n, r” on the screen. Here I did not use any expression in printf () which means that using expressions in printf () is optional.

Note that the ampersand (&) before the variables in the scanf () function is required. & is an “address” operator. Yes
the location number used by the variable in memory. When we say & a, we say scanf () in which memory location the user-supplied value of the keyboard should store.

Note that a new blank, tab, or line must separate the values ​​provided in scanf (). Note that a blank is created using a space bar, tab using the Tab key and a new line with the Enter key. This is shown below:

Ex.: The three values separated by blank
 1000 5 15.5
Ex.: The three values separated by tab.
 1000 5 15.5
Ex.: The three values separated by newline.

So much for tips. How about another program that gives you an idea of ​​things

/* Just for fun. Author: Bozo */
main( )
 int num ;
 printf ( "Enter a number" ) ;
scanf ( "%d", &num ) ;
 printf ( "Now I am letting you on a secret...!!!!!" ) ;
 printf ( "You have just entered the no. %d" , num ) ;

You can get more C related topics

Leave a Comment