C Program Development and Debugging under Unix SEEM 3460

Similar documents
Unix and C Program Development SEEM

Arrays and Pointers in C & C++

The make Utility in Unix SEEM

The make Utility in Unix SEEM

CGS 3460 Summer 07 Midterm Exam

AMCAT Automata Coding Sample Questions And Answers

Subject: Fundamental of Computer Programming 2068

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

CS16 Exam #1 7/17/ Minutes 100 Points total

Functions. Arash Rafiey. September 26, 2017

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

printf( Please enter another number: ); scanf( %d, &num2);

Procedural Programming & Fundamentals of Programming

A Fast Review of C Essentials Part I

We first learn one useful option of gcc. Copy the following C source file to your

Programming in C UVic SEng 265

Compiling and Running a C Program in Unix

Programming in C. What is C?... What is C?

Programming in C. What is C?... What is C?

CSE 351. GDB Introduction

Arrays and Pointers in C. Alan L. Cox

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Lecture 02 C FUNDAMENTALS

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Write a C program using arrays and structure

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Software Development With Emacs: The Edit-Compile-Debug Cycle

Intermediate Programming, Spring 2017*

M1-R4: Programing and Problem Solving using C (JAN 2019)

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

C: How to Program. Week /Mar/05

CS201 - Introduction to Programming Glossary By

Lesson 7. Reading and Writing a.k.a. Input and Output

BLM2031 Structured Programming. Zeyneb KURT

Class Information ANNOUCEMENTS

C Programming Review CSC 4320/6320

Storage class and Scope:

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Programming in C Quick Start! Biostatistics 615 Lecture 4

CS1100 Introduction to Programming

Chapter 2 - Introduction to C Programming

Short Notes of CS201

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CS 314 Principles of Programming Languages. Lecture 9

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Topic 6: A Quick Intro To C

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Array Initialization

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Know your tools. For the GNU/Linux game developer GDB is the BFG. emacs. "a bad workman blames his tools", Cambridge Idioms Dictionary

Programming Studio #9 ECE 190

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

April, 10th /2013

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

n Group of statements that are executed repeatedly while some condition remains true

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

C Syntax Out: 15 September, 1995

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

REPETITION CONTROL STRUCTURE LOGO

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Principles of C and Memory Management

Lecture 07 Debugging Programs with GDB

CS 0449 Sample Midterm

Motivation was to facilitate development of systems software, especially OS development.

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Decision Making -Branching. Class Incharge: S. Sasirekha

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

EL2310 Scientific Programming

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

PROGRAMMAZIONE I A.A. 2017/2018

CSE 124 Discussion (10/3) C/C++ Basics

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Creating a C++ Program

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

C library = Header files + Reserved words + main method

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Single Dimension Arrays

1 A Brief Introduction To GDB

Other Loop Options EXAMPLE

ARRAYS(II Unit Part II)

CSE 303 Lecture 8. Intro to C programming

COMP 2355 Introduction to Systems Programming

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

JTSK Programming in C II C-Lab II. Lecture 3 & 4

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Lecture 03 Bits, Bytes and Data Types

Question 2. [2 points] True False By default, structures are passed-by-reference.

This is CS50. Harvard University Fall Quiz 0 Answer Key

Transcription:

C Program Development and Debugging under Unix SEEM 3460 1

C Basic Elements SEEM 3460 2

C - Basic Types Type (32 bit) Smallest Value Largest Value short int -32,768(-2 15 ) 32,767(2 15-1) unsigned short int 0 65,535(2 16-1) int -2,147,483,648(-2 31 ) 2,147,483,648(2 31-1) unsigned int 0 4,294,967,295 long int -2,147,483,648(-2 31 ) 2,147,483,648(2 31-1) unsigned long int 0 4,294,967,295 SEEM 3460 3

C - Floating Types float single precision floating point double double precision floating point long double extended precision floating point Type Smallest Positive Value Largest Value Precision float 1.17*10-38 3.40*10 38 6 digits double 2.22*10-308 1.79*10 308 15 digits double x; long double x; scanf( %lf, &x); scanf( %Lf, &x); printf( %lf, x); printf( %Lf, x); SEEM 3460 4

C - Character Types char ch; int i; ch = a i= a ; /* iis now 97 */ ch = 65; /* ch is now A */ ch = ch + 1; /* ch is now B */ ch++; /* ch is now C */ if( a <= ch && ch <= z ) for(ch = A ; ch <= Z ; ch++) SEEM 3460 5

C - Char Type a, \t, \n, \0, etc. are character constants strings: character arrays (see <string.h> for string functions) "I am a string" always null ( \0 ) terminated. 'x' is different from "x" SEEM 3460 6

Reading Input from Console //reading input from console #include <stdio.h> int main() { int num1; int num2; printf( "Please enter two numbers: " ); scanf( "%d %d", &num1,&num2 ); printf( "You entered %d %d", num1, num2 ); return 0; } SEEM 3460 7

C - Control Flow blocks: {... } if (expr) stmt; if (expr) stmt1 else stmt2; switch (expr) {case... default } while (expr) stmt; for (expr1;expr2;expr3) stmt; do stmt while expr; break; continue (only for loops); goto label; SEEM 3460 8

C - Loops(for) #include <stdio.h> int main() { int x; /* The loop goes while x < 10, and x increases by one every loop*/ for ( x = 0; x < 10; x++ ) { /* Keep in mind that the loop condition checks the conditional statement before it loops again. consequently, when x equals 10 the loop breaks. x is updated before the condition is checked. */ printf( "%d\n", x ); } return 0; } SEEM 3460 9

C - Loops(while) #include <stdio.h> int main() { int x = 0; /* Don't forget to declare variables */ while ( x < 10 ) { /* While x is less than 10 */ printf( "%d\n", x ); x++; /* Update x so the condition can be met eventually */ } return 0; } SEEM 3460 10

C - Loops(do while) #include <stdio.h> int main() { int x; x = 0; do { /* "Hello, world!" is printed at least one time even though the condition is false*/ printf( "%d\n", x ); x++; } while ( x!= 10 ); return 0; } SEEM 3460 11

C - function #include <stdio.h> //function declaration int mult ( int x, int y ); int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x, y ) ); return 0; } //define the function body //return value: int //utility: return the multiplication of two integer values //parameters: take two int parameters int mult (int x, int y) { return x * y; } SEEM 3460 12

#include <stdio.h> //function declaration, need to define the function body in other places void playgame(); void loadgame(); void playmultiplayer(); int main() { int input; printf( "1. Play game\n" ); printf( "2. Load game\n" ); printf( "3. Play multiplayer\n" ); } printf( "4. Exit\n" ); printf( "Selection: " ); scanf( "%d", &input ); switch ( input ) { case 1: /* Note the colon, not a semicolon */ playgame(); break; //don't forget the break in each case case 2: loadgame(); break; case 3: playmultiplayer(); break; case 4: printf( "Thanks for playing!\n" ); break; default: printf( "Bad input, quitting!\n" ); break; } return 0; switch case SEEM 3460 13

C - struct #include <stdio.h> //group things together struct database { int id_number; int age; float salary; }; int main() { struct database employee; employee.age = 22; employee.id_number = 1; employee.salary = 12000.21; } SEEM 3460 14

C - Type Definitions typedef int BOOL BOOL flag; /* same as int flag; */ typedef struct {int age; char *name} person; person people; SEEM 3460 15

Definition Array A collection of objects of the same type stored contiguously in memory under one name May be type of any kind of variable May even be collection of arrays! For ease of access to any member of array For passing to functions as a group SEEM 3460 16

Examples int A[10] An array of ten integers A[0], A[1],, A[9] double B[20] An array of twenty long floating point numbers B[0], B[1],, B[19] Arrays of structs, unions, pointers, etc., are also allowed Array indexes always start at zero in C SEEM 3460 17

Examples (continued) int C[] An array of an unknown number of integers (allowable in a parameter of a function) C[0], C[1],, C[max-1] int D[10][20] An array of ten rows, each of which is an array of twenty integers D[0][0], D[0][1],, D[1][0], D[1][1],, D[9][19] Not used so often as arrays of pointers SEEM 3460 18

Array Element May be used wherever a variable of the same type may be used In an expression (including arguments) On left side of assignment Examples: A[3] = x + y; x = y A[3]; z = sin(a[i]) + cos(b[j]); SEEM 3460 19

Array Elements (continued) Generic form: ArrayName[integer-expression] ArrayName[integer-expression] [integerexpression] Same type as the underlying type of the array Definition: Array Index the expression between the square brackets SEEM 3460 20

Array Elements (continued) Array elements are commonly used in loops E.g., for(i=0; i < max; i++) A[i] = i*i; sum = 0; for(j=0; j < max; j++) sum += B[j]; SEEM 3460 21

Caution! Caution! Caution! It is the programmer s responsibility to avoid indexing off the end of an array Likely to corrupt data May cause a segmentation fault Could expose system to a security hole! C does NOT check array bounds I.e., whether index points to an element within the array Might be high (beyond the end) or negative (before the array starts) SEEM 3460 22

Single-module Programs Let s examine a C program that performs a simple task: reversing a string. We will learn how to write, compile, link, and execute a program that solves the problem using a single source file. It s better to split a large program up into several independent modules. (will be discussed later) A source code listing of the first version of the reverse program is next presented. SEEM 3460 23

1 /* reverse.c */ 2 #include <stdio.h> 3 #include <string.h> 4 /* Function prototype */ 5 void reverse (char before[], char after[]); 6 7 /*******************************************/ 8 int main() 9 { 10 char str[100]; /* buffer to hold reversed string */ 11 reverse("cat",str); /* reverse the string "cat" */ 12 printf ("reverse(\"cat\") = %s\n", str); 13 reverse("noon",str); 14 printf ("reverse(\"noon\") = %s\n", str); 15 } 16 17 /*******************************************/ 18 void reverse (char before[], char after[]) 19 { 20 int i,j,len; 21 22 len = strlen(before); 23 i=0; 24 for (j=len-1; j>=0; j--) 25 { 26 after[i] = before[j]; 27 i++; 28 } 29 after[len] = \0 ; 30 } SEEM 3460 24

C String - Revision In C, a string is represented as an array of characters For example: noon is represented as: 0 1 2 3 4 5 n o o n \0 (null char) X (garbage) SEEM 3460 25

Syntax and Semantics The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program The semantics of a program statement define what that statement means (its purpose or role in a program) A program that is syntactically correct is not necessarily logically (semantically) correct A program will always do what we tell it to do, not only what we intend to tell it to do SEEM 3460 26

Program Errors A program can have three types of errors The compiler will find syntax errors and other basic problems (compile-time errors) If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors) SEEM 3460 27

Program Errors In Unix, many runtime errors and logical errors only provide the following error messages: Segmentation fault Bus error The above messages do not provide good hints for the causes of the error In other words, it is almost not useful to rely on this message for debugging Therefore, debugging techniques or tools are needed Inserting printing statements to display the content of variables Use a debugger utility SEEM 3460 28

Debugging by Inserting printing Statements A good way for debugging is to insert printing statements in the program. The purpose is to print the content of some important variables to see if the execution follows our design. Suppose we inserted the following line after the statement i++ in the reverse function: printf( i=%d j=%d\n,i,j) The execution would look like: sepc92:>./reverse run the executable reverse i= 1 j = 2 i= 2 j = 1 i= 3 j = 0 reverse ( cat ) = tac i= 1 j = 3 : SEEM 3460 29

Debugging by Inserting printing Statements (cont) When the output text to the screen is very long, we can re-direct the output text to a file instead of the screen so that we can examine the content in a more convenient way. To re-direct in Unix the output from the screen to a text file, we can use the character > meaning that the text output is re-directed from the system output (screen) to a specified text file. In the following example, it is re-directed to a text file called debug.txt sepc92:>./reverse > debug.txt run the executable reverse sepc92:> more debug.txt i= 1 j = 2 i= 2 j = 1 i= 3 j = 0 reverse ( cat ) = tac i= 1 j = 3 : SEEM 3460 30

The Unix Debugger : gdb Preparing a Program for Debugging To debug a program, it must have been compiled using the -g option to gcc, which places debugging information into the object module. sepc92:> gcc g o reverse reverse.c The UNIX debugger, gdb, allows you to debug a program symbolically. It includes the following facilities: single stepping breakpoints editing from within the debugger accessing and modifying variables searching for functions tracing SEEM 3460 31

The Unix Debugger : gdb (con t) Utility: gdb executablefilename gdb is a UNIX debugger. The named executable file is loaded into the debugger and a user prompt is displayed. To obtain information on the various gdb commands, enter help at the prompt. Entering the Debugger Once a program has been compiled correctly, you may invoke gdb, with the name of the executable file as the first argument. gdb presents you with a prompt. You enter help at the prompt to see a list of all the gdb commands: sepc92:> gdb reverse (gdb) help SEEM 3460 32

The Unix Debugger : gdb (con t) (gdb) break reverse Breakpoint 1 at 0x1077c: file reverse.c, line 22 (gdb) run Starting program: reverse Breakpoint 1, reverse(before=0x11360 cat, ) at reverse.c:22 22 len = strlen(before) (gdb) display i 1: i=0 (gdb) display j 2: j = -4197908 (gdb) step 23 i = 0; 2: j = -4197908 1: i = 0 (gdb) step 24 for (j=len-1; j>=0; j--) 2: j = -4197908 1: i = 0 (gdb) step 26 after [i] = before [j] 2: j = 2 1: i = 0 SEEM 3460 33

The Unix Debugger : gdb (con t) (gdb) continue Continuing. reverse("cat") = tac Breakpoint 1, reverse (before=0x4006e1 "noon", ) at reverse.c:22 22 len = strlen(before); 2: j = -668855928 1: i = 62 (gdb) continue Continuing. reverse("noon") = noon Program exited with code 027. (gdb) quit sepc92:> SEEM 3460 34