Date post: | 14-Apr-2018 |
Category: |
Documents |
Upload: | tanujkarnik49 |
View: | 214 times |
Download: | 0 times |
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 1/51
C Programming
Course Agenda
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 2/51
2Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Agenda
• Day 1
– Refresher• Arrays and Functions
• Pointers
• Structures
– Lists
• Static and Dynamic lists
• Memory Management in C
• Dynamic Lists – Singly Linked Lists
– Concepts of linked lists
– Creation and traversal of a single linked list
– gcc and gdb
– Assignments for the day
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 3/51
3Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Agenda…
• Day 2
– Unions– Lists
• Stacks and Queues
– Command Line Arguments
– File handling in C
– Assignments for the day
• Day 3
– Storage classes (auto, static, extern, register)
– Enumerated Data
– Preprocessor directives– makefile
– Assignments for the day
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 4/51
4Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Agenda…
• Day 4
– More on Pointer• Constant Pointers
• Pointers and Array
• Pointer Arithmetic
• Function Pointers
• Pointer to pointer
– Golden rules for memory management
– Violation of the golden rules
– Assignments for the day
• Day 5
– Test (50 marks)
– Project briefing (OS stream POST project briefing)
– Building and executing the project
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 5/51
5Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Agenda…
• Day 6
– Submission of all assignments– Building and executing the project
• Day 7
– Building and executing the project
– Project Evaluation – 50 marks
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 6/51
6Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
References
• Kernighan & Ritchie, The C programming language, Prentice Hall
India.
• Yeshwant Kanetkar, Let Us C, BPB publication.
• Mullish Cooper, The Spirit of C, Jaico.
• Herbert Schildth, Teach Yourself C, Obsorne MCGraw – Hill.
• Yeshwant Kanetkar, Understanding Pointers in C, BPB publication.
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 7/51
7Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Course Evaluation
• 50 marks for the test to be held on day 5
• 50 marks for the successful completion of the project
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 8/51
C Programming
Day 1
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 9/51
9Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher
• Built in data types of C
– char, int, float, double• Variables
– Named memory location that can hold values
– Has to be declared before it can be used
– Declared with the type of value that it stores (Hungarian notation)
– Variable name should clearly indicate what it‟s use • Conditional control structures
– If, if-else, & nested if-else
– switch-case statements
• Iterative control structures - Loops
– for
– while
– do while
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 10/51
10Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Arrays in C
– Array is a collection of similar data types– The size of the array must be specified while declaring an array
– In C there is no check on array boundaries
Example
– int iaNumbers[10];
• Declares an array of 10 integers
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 11/51
11Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Functions in C
– Functions are the building blocks of a C program– Every C Program has at least one function i.e. main()
– First function that is called is main()
• Every function
– Prototype declaration
– Body definition– Declaration must be given before the first use of the function
• Function arguments can be passed by value or by reference
– Pass by value, a copy of the argument is passed to the function
– Pass by reference, address of the argument is passed to the function
• Best Practices
– Function name should be prefixed with fn
– Name of the function should indicate what the function is doing
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 12/51
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 13/51
13Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• To declare a pointer variable,
– Syntax: data type *pointer_var_name;
• Example
1. int *piCount;
piCount will store the address of an integer value
2. float *pfBasic;pfBasic contain the address of a float variable
Note:
The size of the pointer variable is platform dependent.
Size of Pointer on Windows: 4 Bytes
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 14/51
14Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• How to store the address of an integer into an integer pointer?
– Syntax: int *pointer = & integer_variable;
• Example
– iCount is an integer variable
– piCount is an integer pointer
– & is the “address of ” operator
int iCount = 8;int *piCount;
piCount = &iCount;
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 15/51
15Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
0 8
( A d d r e s s
)
2A 3 0 1 8
I n t e g e r v a
r i a b l e
( i n t i C o u n t )
A P o i n t e
r t o a n
I n t e g e r
( i n t * p i C
o u n t )
( A d d r e s s
)
2A 3 0 1 8
P o i n t
e r t o I n t e
g e r
i n t * p i C o u n
t ;
00
00
00
08
2A 3018
2A 3019
2A 301A
2A 301B
I n t e
g e r
Memory
Address
Contentsof MemoryLocation
Integer VariableintiCount;
00
00
00
08
2A 3018
2A 3019
2A 301A
2A 301B
I n t e
g e r
00
2A
30
18
01 2C70
01 2C71
01 2C72
01 2C73
P O I N T E R
Memory
AddressContentsof
MemoryLocation
...
...
...
...
Pointsto…
Integer VariableintiCount;
Pointer toInteger int * piCount;
int iCount = 8;
int *piCount;
piCount = &iCount;
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 16/51
16Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Accessing the value at the address stored in the pointer, use
*pointervariable
Example
– “*” is called the indirection operator
– It fetches the value at the address stored in piCount
• An uninitialized pointer should be set to NULL
Example
/* Initializing pointer to NULL */
int *piCount = NULL;
printf(“%d”, *piCount);
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 17/51
17Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Pointers and Arrays
– Array name is the address of the 0th
element of the array– Array elements are allocated memory contiguously
– The name of the array acts like a pointer to the first element in
the array
5040
30
20
10
09D409D0
09CC
09C8
09C4
AddressElements
09C4
aiMarks
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 18/51
18Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• If we extract the base address of array into a pointer we can use it toaccess the elements of the array
NOTE: The above printf statement can be written using *(piArr + iCount) also
void main(){
int iCount; /* Loop Counter */int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */int *piArr=iaAr; /* Pointer to array */
for(iCount=0;iCount<5;iCount++){
/* Print the content of array using the pointer */
printf("\n%d",*piArr);
piArr++;}}
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 19/51
19Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Structures in C
– A structure is a set of interrelated data– Represented in C with the keyword “struct”
– Different primitive data types are grouped together to form a new data
type
– A structure declaration ends with a semicolon
Example
– iDay, iMonth and iYear are member variables of the „date‟ structure
– The member variables cannot be initialized within a structure
struct date{
short iDay;short iMonth;short iYear;
};
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 20/51
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 21/51
21Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• typedef Keyword
– C allows one type of data to be renamed with a different name using the„typedef‟ keyword
Example
/* Declare the structure _date */struct _date
{ short iDay;short iMonth;short iYear;
};/* Define struct _date as a new data type „date‟ */
typedef struct _date sDate;
/* Create an instance of date structure */sDate today;
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 22/51
22Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Pointer to a structure
2A3080
Memory
Address
2A3081
2A3082
2A3083
2A3084
2A3085
short iDay;
short iMonth;
short iYear;
typedef struct _date {short iDay;short iMonth;short iYear;
} date;…
…
/* instance of date */date sToday;…
/* date pointer! */date* psToday;
/* Address of sToday
is populated into psToday */ psToday = &sToday;
00
2A
30
80
2A3090
2A3091
2A3092
2A3093
sToday
psToday
…
…
…
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 23/51
23Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Refresher…
• Accessing member variables of structure using pointers
Example date *psToday;psToday = &sToday;
… psToday->iDay = 30;psToday->iMonth = 6;psToday->iYear = 2005;
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 24/51
24Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Lists
• A “node” is the basic element that makes up a list
• A “node” can be a single primitive data type or a structure
– For example:
• In a list of numbers every node is a number (int)
• In a list of students every node is a structure with the details of each student
• A “list” is a collection of nodes where all nodes are of the same data
type
– A list of numbers
– A list of students in class
– A list of books in the library– A list of conference rooms in the building
• A list can be static or dynamic
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 25/51
25Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Static Lists
• Static list is one which does not grow but has a fixed size
• To create a static list, a node has to be defined and then the list is
created
• Static lists have a cap on the maximum number of nodes
• This will either cause
– Lack of nodes if there is increase above the maximum
– Waste of space if there are very less number of nodes
30499
Abishek B.
asStudents[0]
30500
Donald D.
asStudents[1]
30501
Janette
asStudents[2]
30502
Karim
asStudents[3]
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 26/51
26Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Static Lists…
/*Structure to define a node of the list*/struct studentProfile
{int iEmpID;char cName[20];
};
/* Structure defines list of students*/
struct Batch{int iNumStudents;struct studentProfile asStudents[100];
};
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 27/51
27Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Dynamic Lists
• Dynamic lists
– Can grow when the need arises and
– There is no memory space wasted
• Creating a dynamic list would involve the following steps
– The structure of the node has to be defined
– New node has to be created as required
• Space is allocated in memory for the new node
• The data is updated to the node
• The node is appended to the list
• We have already seen how to create the structure of a node
• We need to understand how to
– manage memory space i.e. allocate and free memory
– then create the dynamic list
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 28/51
28Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Memory Management in C
• When using arrays in C the drawbacks are
– Size of the array has to be specified while declaring– Not always possible to predict the size requirements before its usage
– Once the array is declared we cannot increase/decrease its size at runtime
• In certain circumstances, allocation of memory should happen at
runtime based on inputs from the user
• Some of the ANSI C library functions used to manage memory are:
– malloc(), calloc(), realloc() and free()
• The functions are defined in the ANSI header „stdlib.h‟ which has to be
included when these functions are used
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 29/51
29Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
Memory Management in C…
• malloc()
– Syntax: void* malloc (size);
– accepts the amount of the memory to be allocated at runtime
– allocates memory on the heap
– returns starting address of the allocated memory block
– Returns NULL if memory is not available
• Address returned is a void pointer, it must be type-cast to the
required type
• Example:
–char* pcName = (char*) malloc (200 * sizeof (char));
– Allocates memory for 200 characters and returns pointer to first element
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 30/51
30Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
• calloc()
– similar to malloc(), used to allocate memory
– Syntax: void *calloc(numMembers, size);
• calloc() accepts 2 parameters
– first one specifies the no of elements (numMembers)
– second one specifies the size of each element (size)
• calloc() returns a void pointer
– needs to be type-cast to the appropriate pointer type
• Example:
– calloc(10,4)• will allocate an array of 10 integers where each integer occupies 4 bytes
Memory Management in C…
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 31/51
31Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003
Version No. 1.0
• realloc()
– used to expand/shrink the memory that was earlier allocated
– Syntax: void *realloc(void *ptr, size);
• realloc() accepts 2 arguments,
– first one specifies pointer to previously allocated memory block (ptr)
– second one specifies the new size in bytes (size)
– If ptr is NULL, the call is equivalent to malloc(size);
– if size is set to 0, the call is equivalent to free(ptr);
• Unless ptr is NULL, it must have been returned by an earlier call to
malloc(), calloc() or realloc()
• Copies the values from the old memory block to the new block
Memory Management in C…
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 32/51
32Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Memory Management in C…
• Memory allocated must be released if no longer needed
• Syntax: void free (void* ptr); • free() accepts a void pointer,
– the starting address of the memory block dynamically allocated
• Freeing an invalid pointer is illegal and the results are unpredictable
Example
int main (int argc, char** argv){char *pcName; /* Pointer to charecter */
/* Space for storing string of 25 charecters */pcName = (char *)malloc(25 * sizeof(char));
.../* Free the pointer */free(pcName);return 0;
}
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 33/51
33Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Memory Leak: An example
Memory Management in C…
int main(int argc, char* argv[]){
Allocate(5);/* some code*/return 0;
}void Allocate(int size){
int *p;p=(int*)malloc(size*sizeof(int));
… }
The pointer is a local
variable which is on the
stack.
However, it points to spaceallocated on the heap.
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 34/51
34Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
CONCEPTS
• A linear data structure which stores data in nodes• Every node has 2 sections
– Data (stores the data)
– Pointer (stores the address of the next node)
• To traverse the list we need
– the address the first node (Head)• The last node of the list (Tail) points to null
• If address to next node is NULL then it is the end of linked list
Node
DataPointer tonext node
Node
DataPointer to
next nodeData
Pointer to
next nodeData
Pointer to
next nodeData
Pointer to
next nodeNULL
(Head) (Tail)
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 35/51
35Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
CONCEPTS: Inserting a node
• Insertion of a new node in a linked list requires setting of twopointers
To insert a node between the second and third node1. Pointer in the new node has to be replaced
2. Pointer in the second node points to new node
Node
DataPointer tonext node Data
Pointer tonext node Data
Pointer tonext node Data
Pointer tonext node NULL
DataPointer tonext node
21
New node to beinserted.
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 36/51
36Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
CONCEPTS : Deleting a node
• Deleting a node from a linked list requires simply changing thepointer to point to the node next to the deleted node
To delete third node (between the second and fourth node)
1. Pointer in the second node has to be removed2. Pointer in the second node points to fourth node
3. Pointer of third node is removed
Node
Data Pointer tonext node
Data Pointer tonext node
Data Pointer tonext node
Data Pointer tonext node
NULL
1
2
3
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 37/51
37Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
Discussion: Implementation of singly linked list
typedef struct _node {
int iEmpID;
struct _node *psNextNode;
}node;
• This structure definition clearly indicated that “psNextNode” is a
pointer of the same type as the structure because it will be pointing
to a structure which is of the same type
Node
iEmpID psNextNode
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 38/51
38Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
int main (int argc, char** argv)
{
/* Create a new node for the linked list*/
psLinkedList=(node*)malloc(sizeof(node));
/* Assign the Head of the linked list*/
psHead = psLinkedList;
/* User adds data for first node */
printf("Please enter the first ID in the list: ");
scanf("%d",&psLinkedList->iEmpID);
/* Assign next node to NULL */psLinkedList->psNextNode = NULL;
iEmpID psNextNode
p s Li n k e d Li s t
iEmpID psNextNode
p s Li n k e d Li s t
p s H e a d
iEmpID NULL
p s Li n k e d Li s t
p s H e a d
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 39/51
39Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
printf ("\nDo you want to add more elements? (y/n): ");
scanf ("%c", &cOption);
fflush (stdin);
/* Traversing the list and adding new nodes to the list */
while (cOption == 'y' || cOption == 'Y')
{
/*Allocate memory for new node*/
psNewNode = (node *) malloc(sizeof(node));
/* Get input from user to add the data for new node*/
printf("Please enter the new ID for the list: ");
scanf("%d",&psNewNode->iEmpID);
iEmpID NULL
psLinkedList
psHead
iEmpID psNextNodepsNewNode
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 40/51
40Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
/* Traverse the list */
psLinkedList->psNextNode = psNewNode;
psNewNode->psNextNode = NULL;
psLinkedList = psNewNode;
/* Choice to grow linked list */
printf ("\nDo you want to add more elements? (y/n): ");
scanf ("%c", &cOption);
fflush (stdin);
/* Increment the number of nodes */
iCount++;
} /* End of While Loop */
psLinkedList
iEmpID NULLpsHead
psNewNode
iEmpID psNextNode
psNewNode
iEmpID NULL
psLinkedList
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 41/51
41Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
Dynamic Lists – Singly Linked List
/* Start with the first node */psLinkedList = psHead;
for(iLoop=0; iLoop<iCount; iLoop++){
/* Iterate till the last node */printf ("%d\n", psLinkedList->iEmpID);psLinkedList = psLinkedList->psNextNode;
}
/* Free the linked list*/free(psLinkedList);
/* Return a success code to the operating system */return 0;
} /* End of the main() function */
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 42/51
42Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
C compiler : gcc
• GNU Compiler Collection (earlier it was GNU C Compiler)
– Collection of compliers for different programming languages
– gcc for C, g++ for C++, gnat for ADA etc.
• gcc myprogram.c
– Compile the program myprogram.c
– Generate an executable file
– Default executable file is a.out• Steps in gcc processing
– Pre-processing
– Compiling and assembly (if required)
– Linking
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 43/51
43Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
C compiler : gcc
• gcc is used with many options
– Some options control the preprocessor
– Some control the compiler, assembler and linker
– Most are rarely used.
• Some commonly used options:
–c
– Says not to run the linker– Output consists of object files
– Example: gcc –c myprogram.c
• Will generate the object file myprogram.o
–o FILE
– This option places the output in a file FILE – Example: gcc –o myprogram.exe myprogram.c
• Generate executable file myprogram.exe instead of a.out
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 44/51
44Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
C compiler : gcc
• Compiling and executing multiple files
– Example: gcc main.c add.c sub.c product.c
– main.c, add.c, sub.c and product.c are part of the same program
– gcc compiles the three files
– Generate the default executable file a.out
• What will the following command do?
– gcc –o final.exe main.c add.c sub.c product.c
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 45/51
45Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
The Debugger: gdb
• GNU symbolic debugger
• Helps the programmer to debug the program
• gcc -g try1.c add1.c sub1.c -o final1
– -g option prepares the output for debugging the three C files
– -o option is for having output in specific file “final1”
Executing the program and debugging the program• Execute the above output with the command final1
• To debug the program
– run final1 in debug mode
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 46/51
46Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
The Debugger: gdb
Debug Mode
• Like in any compiler debugging can be in
– step by step
– break point
• To run the output file in debug mode
gdb final1• gdb prompt will be displayed
• To initiate the execution in debug mode the run command has to be
used
– run [command line parameters]
• Example:
gdb> run “hello” “hi”
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 47/51
47Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
The Debugger: gdb
Breakpoints
• break try1.c:4
– breaks at line 4• break add
– breaks at function add()
• break main
– breaks at function main()
Clear breaks
• clear main
– will clear the break point at function main()
• Disable or enable option is also available
To continue• c
– will continue the execution from the breakpoint.
Th D b db
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 48/51
48Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
The Debugger: gdb
Stepwise execution
• n
– will execute the next instruction
– A function call is taken as one instruction so pressing n will not debug
inside the function, it will step over
• s
– Similar to n but gets steps into the function being executed
watch
• watch a
• watch b
• All the variables in the current context will be displayed
Th D b db
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 49/51
49Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
The Debugger: gdb
List
• Helps in listing the contents of the file
• Useful in case of adding breakpoints
• list: will list the file ten lines at a time
• list main
– main is a function name
• list try1.c:1,20 will list 20 lines from line 1• list add1.c:add will list the function from file add1.c
• Online watching a variable can be done using print variable name
Th D b db
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 50/51
50Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0
The Debugger: gdb
Frames
• Whenever a function is called a stack is created
– These are called as activation records
• If we step into a called function
– then we will not be able to watch or
– print the variables in the calling function
• The way to handle this situation is through frames
– frame 0 is the frame corresponding to the presently running function (called
function)
– Frame 0
• print a will print the value of the variable in called function
– Frame 1
• print c will print the value of the variable in calling function.• Always frame 0 corresponds to the last called function in the nested
sequence
7/30/2019 CProgramming_LA07_Day1
http://slidepdf.com/reader/full/cprogrammingla07day1 51/51