Lab 12 - Unix Lab#12 10/ Page 1 of 5 ‘ f0 Lab #12 CS 2351...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

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

Unformatted text preview: Unix Lab#12 10/ Page 1 of 5 ‘ f0 Lab #12 CS 2351 UNIX Operating System Section 9! l) 2:. This lab will cover basic input and output features of the C and Java programming language. The C statements covered are printf and scanf. The java statements covered are println and readln. The printf and println functions/methods are used to send data to the standard output stream to be displayed to the user. The scanf function accepts numeric data from the standard input and readln functions/methods accept data from the standard input stream to be manipulated by the executing c and java program, respectively. C Information JAVA Information Unlike Unix commands, C and java statements are entered into a file, compiled, and then executed to produce results. A file containing C statements for execution should be saved with a .c extension. Likewise, a file containing java statements for execution should be saved with a .java extension. Once the file has been saved, it must be compiled with the C or java compiler and executed with the C or java interpreter. Structure of a C File : The structure of a C file looks like the following : #include<stdio.h> int main(void) { //series of statements return 0; } // end main declaration The first line #include <stdio.h> is of great interest as it will be included in almost all programs you will write. As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from the source file. In this case, the directive #include tells the preprocessor to include code from the file stdio.h, the standard I/O library. This file contains declarations for functions that the program needs to use such as printf, scanf, etc. correctly. Next, we have the declaration of the main method. This is essential to any executing C program. A C http://cs.okstate.edu/~cs235 l/unix12.html 4/21/2011 Unix Lab#12 Page 2 of 5 program can contain many functions, but must always have one main function. A function is a self- contained module of code that can accomplish some task. The main method is where all program execution begins and ends. The "void" specifies the return type of main. In this case, nothing is returned to the operating system. Finally, we have the open ( { ) and closing ( } ) braces for the main function. The opening bracket denotes the start of the program and the closing bracket denotes the end of the program. All programming statements should be contained within these brackets. mm The printf function is from a standard C library that is used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable of the C program. The following segment of code demonstrates its usage. Type the following into a file entitled hello.c. #include <stdio.h> int main (void) { printf (" Hello \n World \n "); return 0; } // end main class declaration To test the program, save it, and issue the following commands : cc hello.c [return] a.out [return] NOTE: For the program to execute successfully, there should be no errors contained in your file! What was the result of running this program? Why did you receive this output? Haifa fierluui {vials baggy: “HM: if ssiilr'lcd Emilia a», - 5 :(.;3 / r\ w I; v 5‘ , x 3. 3 , Wfl‘fifl Wise-x: fl: int 35",»? 'mg ye, E {griffin Jéwgi. 77 7“ There are generally two steps in the compilation process - the compilation and the linking. The distinction is usually overlooked since the compiler often takes care of initiating the linking step automatically. Typing cc hello.c at the Unix command prompt, requests that the cc (C compiler) be invoked taking its input (the source code) from the hello.c file. The output is stored into an executable program named a.0ut. Scanf The scanf method is one convention used to accept numeric information from the user for use in an executing C program. The following segment of code demonstrates its usage. Type the following into a http ://cs.okstate.edu/~cs23 5 1/unix12.html 4/21/2011 Unix Lab#12 Page 3 of 5 file entitled favnum.c. #include <stdio.h> int main(void) { int num; printf ("Enter your favorite numbers"); scanf ("%d", &num); printf (" You chose %d as your favorite number! \n", num); return 0; } // end main function declaration Test the program by saving it, compiling, and executing it. NOTE: For the program to execute successfully, there should be no errors contained in your file! After issuing the last command, the computer should be waiting on input from you. Enter your favorite number. What commands did you enter to compile and execute the program? CC- fiiflU/flifib ntofi What was the result of running this program? / The first part of this lab was to show you how to type in and compile a C program. The next part is entering a Java program and compiling it. Structure of a Java File : The structure of a java file looks like the following : import java.io.*; public class sample { http://cs.okstate.edu/~csZ351/unix12.html 4/21/2011 Unix Lab#12 Page 4 of 5 public static void main ( String args ) throws IOException { } // end main method declaration } // end main class declaration The first line above contains a very useful feature, import. This keyword prevents the user from repeatedly typing the full package name of the classes used in a program. In the above example, the package is java.io. The asterisk is stating that all classes in this package should be introduced. The second line is declaring a class. All java programs are contained Within some class declaration, and eventual definition. Next, we have the declaration of the main method. This is essential to any executing java program. The main method is where all program execution begins and ends. Finally, we have the closing braces for the main method and class declaration respectively. Println The println method is one convention used to output information from an executing java program. The following segment of code demonstrates its usage. Type the following into a file entitled hello.j ava. import java.io.*; public class hello { public static void main ( String args ) throws IOException { System.out.println (" Hello \n World "); } // end main method declaration } // end main class declaration To test the program, save it, and issue the following commands : javac hello.java [return] java hello [return] NOTE: For the program to execute successfully, there should be no errors contained in your file! What was the result of running this program? / ‘ Readln The readln method is one convention used to accept information from the user for use in an executing http://cs.okstate.edu/~cs235 l/unix12.html 4/21/201 1 Unix Lab#12 Page 5 of 5 java program. The following segment of code demonstrates its usage. Type the following into a file entitled he1102.j ava. import java.io.*; public class hello2 { public static void main ( String args ) throws IOException { String user_input; BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in)); System.out.print ( "Enter your name : ") ; user_input = stdin.readLine(); System.out.println (” Hello : " + user_input); } // end main method declaration } // end main class declaration To test the program, save it, and issue the following commands : javac he1102.java [return] java 1161102 [return] You will be asked to "Enter Your name:" Type in your first and last name. NOTE: For the program to execute successflilly, there should be no errors contained in your file! What was the result of running this program? v 461 Z 5%}49 “r. #41,! This is the end of the UNIX labs. These are meant to help you get acquainted with UNIX and help you learn the commands you will need in order to use UNIX when you are doing programs. If you need to know more about UNIX, you can find many text books that can help you. Please indicate any parts of this lab that were hard to understand. FallO7 http://cs.okstate.edu/~c5235 l/unix12.html 4/21/2011 ...
View Full Document

This note was uploaded on 01/10/2012 for the course CS 2315 taught by Professor Staff during the Fall '11 term at Oklahoma State.

Page1 / 5

Lab 12 - Unix Lab#12 10/ Page 1 of 5 ‘ f0 Lab #12 CS 2351...

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

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