0% found this document useful (0 votes)
187 views3 pages

Lab 8

This document provides instructions on getting started with C programming. It introduces the basics of writing, compiling, and running a simple C program to calculate simple interest. It demonstrates how to take input from the user, perform calculations, and print output. Exercises are provided to modify the simple interest program and write new programs to calculate mathematical operations and the area of a circle.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views3 pages

Lab 8

This document provides instructions on getting started with C programming. It introduces the basics of writing, compiling, and running a simple C program to calculate simple interest. It demonstrates how to take input from the user, perform calculations, and print output. Exercises are provided to modify the simple interest program and write new programs to calculate mathematical operations and the area of a circle.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Page1 of 3 Birla Institute of Technology & Science, Pilani Computer Programming-I (TA C162) Second Semester 2008-2009 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 -----------------------------------------------------------------------------------------------------------------

Getting Started with C


Communicating with a computer requires speaking in the language that computer can understands, which immediately rules out English as the language of communication with computer. C is a programming language which we will be using for it. It was developed at AT&Ts Bell Laboratories of USA in 1972 and was designed and written by a man named Dennis Ritchie. Armed with the knowledge about the type of variables and keywords, lets try our first C program. Each instruction in a C program is written as a separate statement. These statements must appear in the same order in which we wish them to be executed; unless the logic of the problem demands transfer of control to a statement which is out of sequence.

Your First C Program


The following C program calculates the simple interest. /*Program to calculate the simple interest*/. This is how a comment is written. Comments are just to improve the readability of the program. Make a habit of writing comments form the very beginning.
#include<stdio.h> int main(void) { int pri, time; /*variable declarations*/ float rate, si; pri = 2000; /* Initializations */ time = 4; rate = 9.5; si = pri * time * rate / 100; /* assignment statement */ printf ("The Calculated Simple Interest is %f", si); return (0); }

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

Directing output to a new file


The default executable output of gcc is "a.out". It is also possible to specify a name for the executable file at the command line by using the syntax gcc filename o outputfile Type the following: f2007999@atlas:~/cp1$ gcc sinterest.c -o sinterest.out So, now the output file is sinterest which you can execute by following command f2007999@atlas:~/cp1$ ./sinterest

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 -------------------------------------------------

You might also like