Name _________________________________
Section ___________________
| 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.
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 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.
Printf
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?
__________________________________________________________________________________
__________________________________________________________________________________
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.out.
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 file entitled favnum.c.
#include <stdio.h>
int main(void)
{
int num;
printf ("Enter your favorite number:");
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?
__________________________________________________________________________________
__________________________________________________________________________________
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.
The structure of a java file looks like the following :
import java.io.*;
public class sample {
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.java.
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 java program. The following segment of code demonstrates its usage. Type the following into a file entitled hello2.java.
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 hello2.java [return]
java hello2 [return]
You will be asked to "Enter Your name:" Type in your first and last name.
NOTE: For the program to execute successfully, there should be no errors contained in your file!
What was the result of running this program?
__________________________________________________________________________________
__________________________________________________________________________________
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.
___________________________________________________________________________________
___________________________________________________________________________________
06/04/03