

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for students in the cs f111 computer programming course at birla institute of technology & science, pilani, for the third lab session. It covers getting started with c programming on the prithvi server, creating and compiling a c program using gcc, and introduces unix filters such as grep, head, and tail. Students will learn how to change their password, edit files using the vi editor, and perform basic mathematical operations in c.
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


First Semester 2019-
CS F111 Computer Programming
LAB SESSION # (Getting started with C programming; Unix filters)
Working on the Prithvi server
Starting this lab, you shall now work in your own personal Linux account in the “Prithvi” server. Your login name is your complete ID number. For instance, if your ID number is 2019A2PS0076P, then your login name would be the same. The current password is same as your ID number.
To login to the Prithvi server, type the following command at the dollar prompt of your local Ubuntu machine or command prompt of the Windows PC:
ssh [email protected] //enter your own ID number !!
When prompted for the password, enter your password. Note that this will not be visible as you type it.
Immediately after logging, you must change the password by typing the following command on the shell prompt: passwd
Follow the instructions to change your password to something that you can remember. Also note it down, for your future reference. If you forget your password, you will lose access to your account.
1. Type the following C program in a file named lab3_sum.c using the vi editor: (It’s not necessary to type the comments given with /* …. / ) #include <stdio.h> /Preprocessor directive to include header file/ int main() / main() is the entry point of the program / { int a,b,sum; / a, b and sum are declared as integer variables / a=10; / Assigning values to the variables / b=20; sum=a+b; / storing the sum of the two numbers in sum / printf(“The addition result is %d.\n”,sum); return 0; / main() function returns with a value 0 */ } Use the gcc command to get an executable of the above code in Linux. $ gcc lab3_sum.c If there were no errors in the entire process, a file called a.out would be created in the same directory. Type the following command at the shell prompt to execute the program: $./a.out
There are four main stages through which a source code is passed in order to finally get a runnable executable. They are: pre-processing , compilation , assembly and linking. By invoking gcc command, all these steps are accomplished, and you get the resultant executable in a.out. You can stop this pipeline at a specific stage by giving specific options for gcc. For instance, try gcc – S lab3_sum.c for just invoking the assembler. The assembly code for the C program is now generated and stored in lab3_sum.s that you can
inspect using vi. You may not understand the statements, but you should learn to identify how an assembly program looks like. Instead of having the executable stored in a.out , you can also mention the destination file of the executable by using the – o option with gcc. That is, typing gcc
- o sum_exe lab3_sum.c will not produce the default file a.out, but will store the executable in the file sum_exe which can be run by typing ./sum_exe at the shell prompt. 2. Type the following C program in a file named lab3_product.c using the vi editor: (The line numbers given here are not to be typed by you! You can use the :set nu option in vi editor to see the line numbers automatically on the screen.) 1 #include<stdio.h> 2 int main() 3 { 4 float a,b,prod; 5 printf("Enter value of a: "); 6 scanf("%f",&a); // Reading user input for the variable a 7 printf("Enter the value of b: "); 8 scanf("%f",&b); // Reading user input for the variable b 9 prod= a * b; 10 printf("Product of %f and %f is: %f\n", a, b, prod); 11 return 0; 12 } In the above code, scanf() is a function being used to accept the input from the user through keyboard and printf() is another function being used to print out the output. scanf() has two parts; format specifier "%f", and a variable prefixed with & sign. The & (ampersand) prefixed to the variable fetches the address of the location where the variable is stored in memory. (Note that the printf() does not use & to print the value of the variable.) Format Specifier: Specifies which type of data – integer, character, floating point number or double-precision floating point number should scanf() accept. The following characters listed, after the % character, are valid to be used in scanf(): %d is for int; %f is for float; %c is for char; and %lf for double. Now, modify the printf() statement by typing the following (line #10): printf(“Product of %.2f and %.2f is: %.4f\n”, a, b, prod); Note that the output is now formatted better. 3. Now that you have learned input and output functions and a little bit about data types, you are ready to write your own C program that performs basic mathematical operations. Copy the file lab3_basicmath.c into your home directory by issuing the following commands at the shell, sequentially: cd ~ cp /home/share/lab3_basicmath.c. //note the dot at the end Now complete lab3_basicmath.c so that it takes two floating point numbers from the user as input, calculates their sum, product, quotient and difference, and displays the answers on the terminal, printing suitable messages. 4. Generally, C does not provide nesting of comments, although many compilers provide an option for this. Try the following line in your program, use gcc, and see what happens:
/* This is an attempt /* to nest */ a comment. */ Also check to see the following alternative style of commenting in your program. // This is a single line comment