Tutorial On C - Tutorial On C In this tutorial we are going...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Tutorial On C In this tutorial, we are going to do recall our C programming skill . We will write some simple C programs and learn how to compile and build them in Sun Solaris computers of our lab. We will use Vi, Emacs or Pico for editing the files. Login to one of the following pcs: cs1.utdallas.edu cs2.utdallas.edu Use PuTTY or Xmanager for logging on as you learnt in Tutorial 1 . Hello World example In our first example, we have a very simple and short code which prints the traditional "Hello World" string to the terminal. Open your favorite Unix editor (Vi / Emacs / Pico) and type the code: // hello.c // Include the stdio.h header file, it has the declaration of printf function which we are going to use #include <stdio.h> // The main function main() { /* Call the function printf to print Hello World * The argument to the function is the string to be printed * The special sequence "\n" is used to print a new line after the string * The 'n' in "\n" stands for n ew line */ printf("Hello World!\n"); } Everything on the right of a "//" is a single line comment and everything in between a pair of "/*" and "*/" are multi-line comments. Comments are not part of the program. Save the file as "hello.c" and then from the terminal compile and build it with the following command: {cs1:~} gcc -o hello hello.c
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{cs1:~} ls -rwx--x--x 1 mfh062000 sn 6480 Oct 20 16:38 hello -rw-r----- 1 mfh062000 sn 65 Oct 20 15:40 hello.c We used "gcc" to compile the code. "gcc" stands for GNU C Compiler. The "-o" option determines the name of the output file. The argument following the option is the name of the binary to be produced. After that we provided the name of the source file. As we can see from the output of "ls" a file named "hello" is built with execute permission for all. Now we run it by the following way: {cs1:~} ./hello Hello World! {cs1:~} Note that a new line is printed after the string. Otherwise, the prompt would appear after the string on the same line like: "Hello World!{cs1:~}" Finding root of a quadratic equation This example finds the root of a quadratic equation where the co-efficients are hard- coded. Type the code in the editor and save the file as "quad.c": // quad.c #include<stdio.h> // math.h is needed as it has the declaration of sqrt function which we will use #include<math.h> main() { // Declare float type variables, float variables can hold real numbers float a,b,c,s,root_1,root_2; // Initialize co-efficients a=1;b=1;c=-6; // Recall that the root equation is x = (+/-) (-b + square_root_of(b^2 - 4ac)) / 2a // Calculate the square root term // The sqrt function takes a float type variable and return its square root as a float type variable // Operator precedence in C is just like mathematics, i.e. in the following line is similar to 's = sqrt((b*b) - (4*a*c))' s = sqrt(b*b - 4*a*c); // Calculate the roots root_1 = (-b + s)/(2 * a); root_2 = (-b - s)/(2 * a);
Image of page 2
// Print the roots // We put "%f" in the string where we wish to print a float type variable // After the string, we put the variables in order which we like them to be printed within the string printf("The first root is %f\nThe second root is %f\n", root_1, root_2); } Compile and build the code:: {cs1:~} gcc -o quad quad.c -lm Note the "-lm" option used at the end. We need to link to the math library for the sqrt
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern