Lab 8
Lab 8
(Introduction to C) -----------------------------------------------------------------------------------------------------------------Objectives 1. Getting started with C. 2. Compiling your program. 3. Executing your program. 4. Redirecting output to a new file. 5. Exercises -----------------------------------------------------------------------------------------------------------------
Where to write this program? You need a text editor for this. Recall that in UNIX, we have a text editor, called vi, and you are already armed with the basic knowledge of it. So, to write the program, type: vi filename.c:
Page2 of 3 f2007999@atlas:~/cp1$ vi sinterest.c Remember, all C programs files has an extension .c. After typing, save the file and come to the prompt. Now we're going to need a compiler. What is a compiler? It is a program that turns the C code into an executable, which is in binary format (as opposed to text). Released by the Free Software Foundation, gcc is a Unix-based C compiler usually operated via the command line. It often comes distributed with a Unix installation, so if you are running Unix or a Linux variant you likely have it on your system. Well, understanding this isn't necessary to programming, and if you're confused, you'll pick it up later on. You can invoke gcc on a C source code file simply by typing gcc filename. f2007999@atlas:~/cp1$ gcc sinterest.c Your .c file will compile and gets executed, if there are no errors. If there are errors, then the list of these will be displayed when you type the above command and press enter. Kindly get rid of these errors to proceed. Now type ls on the $ prompt and observe that a new file is been created. f2007999@atlas:~/cp1$ ls a.out sinterest.c By default the name of the executable file will be a.out, to see the output, type the following and observe the output of your C program. f2007999@atlas:~/cp1$ ./a.out Now, let us dissect our above program line by line. Any C program is nothing but a combination of functions. One such function is main(void), and the execution of your C program starts with this function. Parenthesis after main is necessary. Set of curly braces which contain the body of your main function. Any variable used in the program must be declared before using it. First two lines in the braces do the same and declare two variables of type integer and float each. After the execution of the assignment statement (si=pri*time*rate/100) variable si contains the simple interest. printf() is a function which is used to display the output print on the screen. The general form is: printf (<format string>, <list of variables>); <format string could be>, %d for printing integer values. %f for printing real values. %c for printing character values. A message also can be included along with format string as shown in the above program. The message is The Calculated Simple Interest is followed by %f. The message will be printed as it is and as soon as %f will be observed, the value of the variable after comma will be printed at its place. Finally the return (0) statement takes you out to the prompt.
Page3 of 3
Exercises
1. Replace the initialization statements in the given program with the following:
printf (Enter principal amount:); scanf (%d, &pri); printf (Enter time:); scanf (%d, &time); printf (Enter rate of interest:); scanf (%f, &rate);
scanf() function is used to take the input from the user and store it in a variable. So you are actually specifying to the user to enter the values of the three variables. Execute the above modified program using procedure explained above. 2. Write a C program that takes two numbers from the user as an input and it calculates and displays the sum, multiplication, division and subtraction of these numbers. Assume that user enters non zero numbers. Hint: Declare two integer variables to store the numbers and four integer variables for storing sum, multiplication, division and subtraction result. Read the numbers using scanf(). Write arithmetic expressions for the required result. Use printf() to display the results. Note: Modify your program for double data type. 3. Area of a circle of radius R is given by (Area=PI * R*R). Write a C program to calculate the area of a circle. Here PI is a constant having value 3.143 -------------------------------------------------