CIS 17 C Programming - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CIS 17 C Programming

Description:

We will now pass data directly to the program using the command line ... argv Table of addresses. 9/23/09. 5. Command-Line Arguments ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 20
Provided by: academi3
Category:
Tags: cis | argv | programming

less

Transcript and Presenter's Notes

Title: CIS 17 C Programming


1
CIS 17 - C Programming
2
Lecture 13
  • Command-Line Arguments
  • Additional Features
  • Bitwise Operators
  • Macros
  • Multi-file programming
  • Abstraction
  • Reusable code
  • Conditional Compilation

3
Command-Line Arguments
  • Passing Arguments to Main()
  • Up to now we have used redirection to enter data
    into our programs lt
  • We will now pass data directly to the program
    using the command line
  • C\powerc\my_program.exe arg1 arg2 arg3

4
Command-Line Arguments
  • Information on the command line is stored in
    memory.
  • Each string terminates with a \0 character
  • Main() is only allowed to receive 2 arguments,
    they must be part of the function declaration.
  • int argc The number of arguments
  • argv Table of addresses

5
Command-Line Arguments
  • If you would like to receive information from the
    command line you must declare your main() with
    these arguments
  • void main(int argc, char argv)
  • This will take as many arguments as you can fit
    on a line.
  • my_prog.exe in_file out_file1.txt err_file.txt

6
Additional Features Cont.
  • Conditional Expressions - ?
  • Alternate way of expressing simple if-else
  • exp1 ? exp2 exp3
  • If the values of exp1 is true exp2 is evaluated
    otherwise exp3 is evaluated
  • Rate (hours gt 40) ? 0.045 0.02

7
Additional Features Cont.
  • The goto Statement
  • Provides an unconditional transfer of control to
    some other statement in a program
  • goto err
  • err printf(blah blah)
  • Dont use them!!!!

8
Bitwise Operators
  • , , , , ltlt, gtgt
  • Used to perform bit operations
  • AND Operator -
  • Performs bit by bit and comparison
  • 1 0 1 1 0 0 1 1
  • 1 1 0 1 0 1 0 1
  • 1 0 0 1 0 0 0 1
  • Rule The results of an AND operation is a 1 if
    one when both bits being compared is a 1s,
    otherwise the result is 0.

9
Bitwise Operators Cont.
  • Inclusive OR Operator -
  • Performs a bit-by-bit comparison
  • 1 0 1 1 0 0 1 1
  • 1 1 0 1 0 1 0 1
  • 1 1 1 1 0 1 1 1
  • Rule The results of an OR operation is a 1 if
    either of the bits being compared is a 1,
    otherwise the result is 0

10
Bitwise Operators Cont.
  • Exclusive OR Operator -
  • Performs a bit-by-bit comparison
  • 1 0 1 1 0 0 1 1
  • 1 1 0 1 0 1 0 1
  • 0 1 1 0 0 1 1 0
  • Rule The results of an XOR operations is a 1 if
    one and only one of the bits being compared is a
    1, otherwise the result is 0

11
Bitwise Operators Cont.
  • Complement Operator -
  • Performs a bit-by-bit switch
  • 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0
  • Rule The results of a complement operation is to
    change ones to zeros and zeros to ones.

12
Bitwise Operators Cont.
  • Shift operators
  • ltlt causes the bits in an operand to be shifted to
    the left by a given mount.
  • gtgt causes the bits in an operand to be shifted to
    the right by a given amount.
  • op1 op2 ltlt 4
  • Shifts op2 to the left and stores it in op1

13
Macros
  • The direct inline expansion of one word into
    many.
  • define FORMAT The answer is f\n
  • Enables us to write
  • printf(FORMAT, 15.2)

14
Macros
  • Other macro examples
  • define SQUARE(x) x x
  • Can be used with any data type argument
  • Use \ to continue on a new line

15
Multi file programming
  • Personal Libraries
  • Header files
  • Implementation

16
Abstraction
  • The process of separating the inherent qualities
    or properties of something from the object that
    they belong
  • Procedural abstraction
  • separate the concern of what is to be achieved by
    a function from the detail of how it is to be
    achieved.
  • Break down the program into solvable chunks

17
Abstraction
  • Data abstraction
  • Specify data objects and operations to be
    performed on these objects.
  • A logical view of data object as opposed to its
    physical view
  • Information hiding
  • Data can only be accessed using operators
  • Protects implementation details of the lower
    level functions

18
Reusable Code
  • Encapsulation
  • Packaging data and operators in to a library
  • Standard library modules
  • Stack
  • Queue
  • Hide the internal details from the programmer
  • Programmer uses the tools provided in the package

19
Conditional Compilation
  • Programmer selective compile method
  • define TRACE (turns on statements)
  • if defined (TRACE)
  • printf(This will be printed if trace is
    defined)
  • endif
Write a Comment
User Comments (0)
About PowerShow.com