0% found this document useful (0 votes)
5 views

SE 314 Lab-4 Advanced C-programming

c p

Uploaded by

msiddiquesamia
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SE 314 Lab-4 Advanced C-programming

c p

Uploaded by

msiddiquesamia
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

College of Engineering

Software Engineering Department


SE 314: Operating System

Fall 2023

Lab 4- Advanced C-programming

Objectives:

 To learn how to write C-Program using command line arguments


 To know how to use pointers
 To know how to use an Array
 To know how to handle Strings

Part 1: [10 Marks] Using command line arguments in C programs:

What are Command line arguments in C?


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].

Save the following code under name ex1.c and compile it


#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);
}

/* Loop to count characters */


while(argv[1][++n] != '\0')
{

}
/*Print result */
printf("The world %s has %d characters .\n",argv[1],n);
}
Run the code by typing: ex1 YourName in terminal and attache screen shot of the output of
this code in the space provided.
Screen Shot:

Answer the following questions after running code above:


1- Explain the function of this program:

2- What is the purpose of this code:


if(argc==1)
{
printf("No world to examine. \n");
exit(0);
}

3- If I run the program using: ex1 Ahmed, what is will be the values inside argc
and argv?
Exercise 1: Write a C program that does simple mathematical operations (+
- / *) when run in the terminal as follows:
>myCalc 3 + 4
>7
> myCalc 3 * 4
>12
Include a copy of your code here:

Include a screen shot of sample run here:


Part 2: [10 Marks] Accessing Addresses and using Pointers C programs:

How to get the address of a variable in C?


If you have a variable x in your program, &x will give you its address in the
memory.
Save the following code, compile, and run it
#include <stdio.h>
int main()
{
int x = 5;
printf("var: %d\n", x);

// Notice the use of & before x


printf("address of x: %p", &x);
return 0;
}

Attache screen shot of the output of this code in the space provided.
Screen Shot:

What are Pointers?


Pointers (pointer variables) are special variables that are used to store addresses
rather than values.

Pointer Syntax
Here is how we can declare pointers in any of the following ways:
int* p;
int *p1;
int * p2;

Assigning addresses to Pointers


Let's take an example.
int* pc;
int c;
c = 5;
pc = &c;

Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc


pointer.
Get Value of Thing Pointed by Pointers
To get the value stored in address in pointer pc, we used *pc

* is called the dereference operator (when working with pointers). It operates on a


pointer and gives the value stored in that pointer.

Changing Value Pointed by Pointers

Let's take an example.


int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1

We have assigned the address of c to the pc pointer.

Then, we changed the value of c to 1. Since pc and the address of c is the same, *pc
gives us 1.

Let's take another example.


int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
We have assigned the address of c to the pc pointer.

Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is the
same, c will be equal to 1.
Save the following code, compile, and run it
#include <stdio.h>
int main()
{
int* pc, c;

c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22

pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11

*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}

Attache screen shot of the output of this code in the space provided.
Screen Shot:

You might also like