5/5 - (2 votes)

Lab 1
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]);
n = atoi(argv[1]);
for (i=0; i<10;++i){
j = rand()%100;
printf (“\n %d”,j);
printf (“\n”);
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( ).
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.
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: 1>
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. 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 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: 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. 4> filename
To enable debugging code so that you can use gdb debugger, use the –g switch 5> gcc –o filename –g filename.c 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
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
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.

Open chat
Need help?
Can we help?