CSCI 2132 Software Development Lecture 26: Writing Large Programs - - PowerPoint PPT Presentation

csci 2132 software development lecture 26 writing large
SMART_READER_LITE
LIVE PREVIEW

CSCI 2132 Software Development Lecture 26: Writing Large Programs - - PowerPoint PPT Presentation

CSCI 2132 Software Development Lecture 26: Writing Large Programs Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 5-Nov-2018 (26) CSCI 2132 1 Previous Lecture A common mistake with VLA declarations More


slide-1
SLIDE 1

CSCI 2132 Software Development Lecture 26: Writing Large Programs

Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University

5-Nov-2018 (26) CSCI 2132 1

slide-2
SLIDE 2

Previous Lecture

  • A common mistake with VLA declarations
  • More string reading examples
  • Buffer overflow risks
  • String library functions

5-Nov-2018 (26) CSCI 2132 2

slide-3
SLIDE 3

Command-Line Arguments

  • To access the command-line arguments, we use another

way to define the main function: int main(int argc, char* argv[]) { ... }

  • argc is the number of command-line arguments
  • argv is the array of command-line arguments; it is an

array of pointers to char

  • argv[0] is the program name as executed

5-Nov-2018 (26) CSCI 2132 3

slide-4
SLIDE 4

Example: sortwords program

  • Example: sortwords program, which sorts words given in the

command line

  • Usage example:

./sortwords orange apple banana

  • It should produce:

apple banana

  • range

5-Nov-2018 (26) CSCI 2132 4

slide-5
SLIDE 5

Example with argc and argv

  • In the example:

./sortwords orange apple banana

  • argc would be 4, and argv could be represented as follows

argv argv[0] -------> ./sortwords\0 argv[1] -------> orange\0 argv[2] -------> apple\0 argv[3] -------> banana\0 argv[4] stores a NULL pointer

  • Code (insertion sort):

˜prof2132/public/sortwords.c

5-Nov-2018 (26) CSCI 2132 5

slide-6
SLIDE 6

Writing Large Programs

  • A large program consists of many modules
  • Different programmers may work on different

modules

  • Logical to use one or more files for each

module – Facilitates collaboration and reusing code

  • Reading: C textbook: Chapter 15

5-Nov-2018 (26) CSCI 2132 6

slide-7
SLIDE 7

Header Files

  • Files that allow different source files (*.c) to

share – Function prototypes – Type definitions – Macro definitions – etc.

  • Naming convention: *.h

5-Nov-2018 (26) CSCI 2132 7

slide-8
SLIDE 8

The #include Directive

  • Tells the preprocessor to open a specified file and inserts

its content into the current file

  • Form 1: #include <file_name>

– Search the directories in which system header files reside – On bluenose: /usr/include, . . .

  • Form 2: #include "file_name"

– First search the current directory, if not found then – directories in which system header files reside

  • Question: Which form for your own header files?

5-Nov-2018 (26) CSCI 2132 8