King Fahd University of Petroleum and Minerals: Information & Computer Science Department
King Fahd University of Petroleum and Minerals: Information & Computer Science Department
King Fahd University of Petroleum and Minerals: Information & Computer Science Department
Objectives:
9 To learn how to write C-Program using command line arguments
9 To know how to use pointers
9 To know how to declare and define a structure
9 To know how to use 1-D Array
9 To know how to use 2-D array
9 To know how to handle Strings
The arguments for main( int argc, char *argv[] ) are argc(argument count) and argv(argument
vector). The argument argc is an integer that contains the number of arguments being passed to
main ; argv is an array of strings. Each string is an argument. The computer is able to count the
number of arguments and assign the value to argc. The array argv[1] contains the first argument,
argv[2] contains the second argument and so on . The program’s name is stored in argv[0].
#include<stdio.h>
/*Sample program to use command line arguments */
main(int argc, char *argv[])
{
int n=0;
/* Findout if any world passed to program */
if(argc==1)
{
printf("No world to examine. \n");
exit(0);
}
}
/*Print result */
printf("The world %s has %d characters .\n",argv[1],n);
}
Sample Output:
Example#2(Use command line arguments):
#include<stdio.h>
#include<stdlib.h>
if(argc==3)
{
printf("You must input three numbers as command line inputs. \n");
exit(0);
}
sum=atoi(argv[1])+atoi(argv[2])+atoi(argv[3]);
printf("argv[0] is = %s\n",argv[0]);
Sample Output:
What is a Pointer? :
Pointer variable is a special variable, which stores the address of other variable. If a pointer
variable stores the address of a char variable, we call it a character pointer and so on.
Normally a variable directly contains a specific value. A pointer, on the other hand, contains an
address of a variable that contains a specific value. Pointers like any other variables must be
declared before they can be used.
Examples:
int *j --------Æ Means the value at address contained in j is an int OR in other words j is an
integer pointer.
int **k ------Æ Means the value at address contained in *k is an int.
Example#3 (Pointers):
#include<stdio.h>
void test1(int m, int n) //user-defined function
{
m=5;
n=24;
}
int main(void) {
int a=10, b=16;
printf("a=%d, b=%d\n",a,b);
Example#4 (Pointers):
#include <stdio.h>
main()
{
int i1, i2, *p1, *p2;
i1 = 5;
p1 = &i1;
i2 = *p1 / 2 + 10;
p2 = p1;
printf("i1=%d, i2=%d, *p1=%d, *p2=%d\n", i1, i2, *p1, *p2);
}
What is structure?:
Structures are collections of related variables. Structures may contain variables of many different
data types- in contrast to arrays that contain only elements of the same data type; Structures are
commonly used to define records to be stored in files. Pointers and structures facilitate the
formation of more complex data structures such as linked lists, queues, stacks, and trees.
The elements can be of any type including enumerated types, arrays and even other structures.
We can simplify the variable declaration above, if we use typedef in the declaration.
The above can then be defined as:
typedef struct {
int id_no;
STRING name;
STRING dept;
int no_of_books;
USER_STATUS status;
} USER_RECORD;
USER_RECORD user;
Once a variable is declared, we can access the individual elements (called fields) of the record
using the member operator, also called the dot (.) operator.
We can combine declarations and declare structure variables at the same time that the structure
type is declared. For example:
struct date
{
int day, month, year;
} arrival_date, departure_date;
#include<stdio.h>
#define MAX_SIZE 5
void main()
{
int first[MAX_SIZE], second[MAX_SIZE], diff[MAX_SIZE], i;
} // end of main
/*Matrix Addition */
#include<stdio.h>
#define row 10
#define col 10
int main()
{
int i,j,k, a[row][col], b[row][col], s[row][col] = {0};
int rowsa, colsa, rowsb, colsb;
return 0;
} // end of main
Strings:
Strings are implemented as array of characters. The end of the string is always marked with a
null character. Loops are used to manage the string, one character at a time. A test for the null
character detects the end of the string. A string constant is specified by a set of double quotes.
This string constant has an address; the address of the string. The string’s address can be
assigned to a pointer.
Examples:
char course_name[] = “Operating System”;
char *depart_name = “ICS”;
char *colg_name = {‘C’, ‘C’, ‘S’, ‘E’, ‘\0’};
A set of functions in the standard library is designed to perform common operations on strings.
These functions are defined in the header file strings.h.
Exercises
Note:
Lab Problems will be given during the lab based on material covered in this lab manual.