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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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);
Background image of page 2
// Print the roots // We put "%f" in the string where we wish to print a float type variable
Background image of page 3

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

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

This note was uploaded on 12/12/2009 for the course CS 3375 taught by Professor Kendracooper during the Fall '08 term at University of Texas at Dallas, Richardson.

Page1 / 13

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online