The sample program you are compiling and executing here is the one we have discussed in the previous post under the title c programming rules.
Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor.
Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler.
Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. There are several such IDEs available in the market targeted towards different operating systems.
For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them.
If you are a beginner you would be better off using a simple compiler like Turbo C or Turbo C++.
With Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
For Turbo C++ compiler, you may get an error — The function printf should have a prototype. To get rid of this error, perform the following steps and then recompile the program.
This can be achieved using a function called scanf( ).
This function is a counter-part of the printf( ) function.
printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. This is illustrated in the program shown below.
/* Calculation of simple interest */
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( ) outputs the message ‘Enter values of p, n, r’ on the screen.
Here we have not used 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 a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard.
Note that a blank, a tab or a new line must separate the values supplied to scanf( ). Note that a blank is creating using a spacebar, tab using the Tab key and new line using the Enter key.
IF STATEMENT
MULTIPLE STATEMENTS IN IF
IF AND ELSE
NESTED IF AND ELSE
BREAK
CONTINUE AND DO WHILE IN C LANGUAGE
SWITCH IN C PROGRAMMING
FUNCTIONS IN C PROGRAMMING
Functions and usage in C part two
Coding in C functions
Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor.
Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler.
Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. There are several such IDEs available in the market targeted towards different operating systems.
For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them.
If you are a beginner you would be better off using a simple compiler like Turbo C or Turbo C++.
With Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
- Select New from the File menu.
- Type the program.
- Save the program using F2 under a proper name (say Program1.c).
- Use Ctrl + F9 to compile and execute the program.
- Use Alt + F5 to view the output.
For Turbo C++ compiler, you may get an error — The function printf should have a prototype. To get rid of this error, perform the following steps and then recompile the program.
- Select ‘Options’ menu and then select ‘Compiler | C++ Options’. In the dialog box that pops up, select ‘CPP always’ in the ‘Use C++ Compiler’ options.
- Again select ‘Options’ menu and then select ‘Environment | Editor’. Make sure that the default extension is ‘C’ rather than ‘CPP’.
This can be achieved using a function called scanf( ).
This function is a counter-part of the printf( ) function.
printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. This is illustrated in the program shown below.
/* Calculation of simple interest */
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( ) outputs the message ‘Enter values of p, n, r’ on the screen.
Here we have not used 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 a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard.
Note that a blank, a tab or a new line must separate the values supplied to scanf( ). Note that a blank is creating using a spacebar, tab using the Tab key and new line using the Enter key.
IF STATEMENT
MULTIPLE STATEMENTS IN IF
IF AND ELSE
NESTED IF AND ELSE
BREAK
CONTINUE AND DO WHILE IN C LANGUAGE
SWITCH IN C PROGRAMMING
FUNCTIONS IN C PROGRAMMING
Functions and usage in C part two
Coding in C functions
No comments:
Post a Comment