Sale!

Lab 1 REVIEW C/C++ PROGRAMMING

$30.00

Category:
5/5 - (2 votes)

Lab 1
REVIEW C/C++ PROGRAMMING
C is similar to C++. There are a few differences. The following program shows some of the differences.
Note use of comments, header files, and printf() statement.
/****************************************************************/
/* PROGRAM NAME: lab1.c */
/* DESCRIPTION: This lab assignment introduces C syntax and use of */
/* command line arguments */
/****************************************************************/
# include <stdio.h>
# include <stdlib.h>
int main(int argc, char *argv[])
{
int i,j,n;

if (argc !=2) {
printf(“\n Usage: Need filename and one argument\n”, argv[0]);
exit(1);
}
n = atoi(argv[1]);
srand(n);
for (i=0; i<10;++i){
j = rand()%100;
printf (“\n %d”,j);
}
printf (“\n”);
exit(0);
}
Command Line:
A UNIX command line consists of tokens (the arguments) which are separated by blanks, tabs, or
backslash (\) just before a newline character. Each token is a string of characters. When a user enters a
command corresponding to a C executable program, the shell parses the command line and passes the result
to the program in the form of an argument array.
Example: A program lab2 has the start line int main(int argc, char *argv[]).
The command line lab2 ab 10 4.5 results in argc = 4 (four tokens) and the argv is an array of pointers to the
command-line tokens as follows:
argv[0] Æ ‘l’ ‘a’ ‘b’ ‘2’ ‘\0’
argv[1] Æ ‘a’ ‘b’ ‘\0’
argv[2] Æ ‘1’ ‘0’ ‘\0’
argv[3] Æ ‘4’ ‘.’ ‘5’ ‘\0’
To convert string to integer, use the function atoi( ).
3
Telnet session:
Note: Bold face indicates what you type in followed by a return.
You can log on heart by running telnet. A typical telnet session may look like the following.
telnet heart.cecs.csulb.edu
heart login: username (type in your user name)
Password: password (type in your password)
. . .
heart:1> telnet linux
. . .
login: username
Password: password
. . .
LINUX prompt: linenumber>
For example:
cecs416lin216.cecs.csulb.edu: 1>
Compiler:
The gcc and cc are available for C source code. The g++ is available for C++ source code. First write the
program using any text editor. At the UNIX prompt, type gcc or g++ followed by filename with proper
extension.
cecs416lin216.cecs.csulb.edu: 1> gcc filename.c
This will compile the object file for filename, link it with standard libraries, then produce an output file
called a.out which is a binary executable file. To run the executable, type the executable
cecs416lin216.cecs.csulb.edu: 2> a.out
Common command line options: To change the name of the executable file, use the –o switch followed by
the desired executable name:
cecs416lin216.cecs.csulb.edu: 3> gcc –o filename filename.c
This will produce an output file called filename which is a binary executable file. To run the executable,
type the desired executable name.
cecs416lin216.cecs.csulb.edu: 4> filename
Debugger:
To enable debugging code so that you can use gdb debugger, use the –g switch
cecs416lin216.cecs.csulb.edu: 5> gcc –o filename –g filename.c
cecs416lin216.cecs.csulb.edu: 6> gdb filename
The debugger gdb has the following commands. You must type the gdb filename first before using the
debugger commands. The commands can be abbreviated. For example, you can type step or s.
<gdb> l(ist)
Lists the program.
<gdb> l(ist) start_line_number, end_line_number
Lists the program from start_line_number to end_line_number
<gdb> b(reak) line_number/ function_name
Sets break point at line_number or function_name
<gdb> r(un) command line argument if required
4
Executes the program from the beginning
<gdb> s(tep)
Move to the next instruction in the program and executes it. Stops at completion. Also single steps
through functions.
<gdb> n(ext)
Move to the next instruction in the program and executes it. Stops at completion. Treats function
calls as single steps and will not trace through them.
<gdb> c(ont)
Continues execution of the program from the current execution point. Stops at breakpoint.
<gdb> cl(ear) line_number
Clears a breakpoint at line_number
<gdb> p(rint) variable_name
Displays the value of the variable_name
<gdb> q(uit)
Exits the debugging session
ASSIGNMENT:
Write a C program named lab1 to store N random numbers in an array named randA[ ] and determine the
largest, smallest, and average values of this array. Display the array elements that store these values. The
random numbers range from -100 to +100. N is a command line argument. Use the debugger to go
through the program. Use all the debugger commands.
END OF LAB 1

PlaceholderLab 1 REVIEW C/C++ PROGRAMMING
$30.00
Open chat
Need help?
Hello
Can we help?