C Program Structure (and tools) - PowerPoint PPT Presentation

About This Presentation
Title:

C Program Structure (and tools)

Description:

C++ Program Structure (and tools) Today we ll talk generally about C++ development (plus a few platform specifics) We ll develop, submit, and grade code in Windows – PowerPoint PPT presentation

Number of Views:1059
Avg rating:3.0/5.0
Slides: 16
Provided by: cseWustl
Category:

less

Transcript and Presenter's Notes

Title: C Program Structure (and tools)


1
C Program Structure (and tools)
  • Today well talk generally about C development
    (plus a few platform specifics)
  • Well develop, submit, and grade code in Windows
  • Its also helpful to become familiar with Linux
  • E.g., on shell.cec.wustl.edu
  • For example, running code through two different
    compilers can catch a lot more easy to make
    errors

2
Writing a C Program
Visual Studio
Eclipse
emacs
editor
1 source file 1 compilation unit
C source files (ASCII text) .cpp
Also .C .cxx .cc
Programmer (you)
Also .H .hxx .hpp
C header files (ASCII text) .h
readme (ASCII text)
3
What Goes Into a C Program?
  • Declarations data types, function signatures,
    classes
  • Allows the compiler to check for type safety,
    correct syntax
  • Usually kept in header (.h) files
  • Included as needed by other files (to keep
    compiler happy)
  • class Simple typedef
    unsigned int UINT32
  • public
  • Simple (int i) int usage
    (char program_name)
  • void print_i ()
  • private struct
    Point2D
  • int i_ double
    x_
  • double
    y_
  • Definitions static variable initialization,
    function implementation
  • The part that turns into an executable program
  • Usually kept in source (.cpp) files
  • void Simpleprint_i ()

4
A Very Simple C Program
  • include ltiostreamgt // precompiler directive
  • using namespace std // compiler directive
  • // definition of function named main
  • int main (int, char )
  • cout ltlt hello, world! ltlt endl
  • return 0

5
What is include ltiostreamgt ?
  • include tells the precompiler to include a file
  • Usually, we include header files
  • Contain declarations of structs, classes,
    functions
  • Sometimes we include template definitions
  • Varies from compiler to compiler
  • Advanced topic well cover later in the semester
  • ltiostreamgt is the C label for a standard header
    file for input and output streams

6
What is using namespace std ?
  • The using directive tells the compiler to include
    code from libraries that have separate namespaces
  • Similar idea to packages in other languages
  • C provides a namespace for its standard library
  • Called the standard namespace (written as std)
  • cout, cin, and cerr standard iostreams, and much
    more
  • Namespaces reduce collisions between symbols
  • Rely on the scoping operator to match symbols
    to them
  • If another library with namespace mylib defined
    cout we could say stdcout vs. mylibcout
  • Can also apply using more selectively
  • E.g., just using stdcout

7
What is int main (int, char) ... ?
  • Defines the main function of any C program
  • Who calls main?
  • The runtime environment, specifically a function
    often called something like crt0 or crtexe
  • What about the stuff in parentheses?
  • A list of types of the input arguments to
    function main
  • With the function name, makes up its signature
  • Since this version of main ignores any inputs, we
    leave off names of the input variables, and only
    give their types
  • What about the stuff in braces?
  • Its the body of function main, its definition

8
Whats cout ltlt hello, world! ltlt endl ?
  • Uses the standard output iostream, named cout
  • For standard input, use cin
  • For standard error, use cerr
  • ltlt is an operator for inserting into the stream
  • A member operator of the ostream class
  • Returns a reference to stream on which its called
  • Can be applied repeatedly to references
    left-to-right
  • hello, world! is a C-style string
  • A 14-postion character array terminated by \0
  • endl is an iostream manipulator
  • Ends the line, by inserting end-of-line
    character(s)
  • Also flushes the stream

9
What about return 0 ?
  • The main function should return an integer
  • By convention it should return 0 for success
  • And a non-zero value to indicate failure
  • The program should not exit any other way
  • Letting an exception propagate uncaught
  • Dividing by zero
  • Dereferencing a null pointer
  • Accessing memory not owned by the program
  • Indexing an array out of range can do this
  • Dereferencing a stray pointer can do this

10
A Slightly Bigger C Program
  • include ltiostreamgt
  • using namespace std
  • int main (int argc, char argv)
  • for (int i 0 i lt argc i)
  • cout ltlt argvi ltlt endl
  • return 0

11
int argc, char argv
  • A way to affect the programs behavior
  • Carry parameters with which program was called
  • Passed as parameters to main from crt0
  • Passed by value (well discuss what that means)
  • argc
  • An integer with the number of parameters (gt1)
  • argv
  • An array of pointers to C-style character strings
  • Its array-length is the value stored in argc
  • The name of the program is kept in argv0

12
for (int i 0 i lt argc i)
  • Standard (basic) C for loop syntax
  • Initialization statement done once at start of
    loop
  • Test expression done before running each time
  • Expression to increment after running each time
  • int i 0
  • Declares integer i (scope is the loop itself)
  • Initializes i to hold value 0 (not an
    assignment!)
  • i lt argc
  • Tests whether or not were still inside the
    array!
  • Reading/writing memory we dont own can crash the
    program (if were really lucky!)
  • i
  • increments the array position (why prefix?)

13
cout ltlt argvi ltlt endl
  • Body of the for loop
  • I strongly prefer to use braces with for, if,
    while, etc., even w/ single-statement body
  • Avoids maintenance errors when adding/modifying
    code
  • Ensures semantics/indentation say same thing
  • argvi
  • An example of array indexing
  • Specifies ith position from start of argv

14
Lifecycle of a C Program
xterm
An IDE
window
console/terminal/window
Makefile
WebCAT
make
E-mail
turnin/checkin
make utility
Runtime/utility libraries (binary) .lib .a .dll
.so
Eclipse
compile
link
Visual Studio
C source code
Programmer (you)
debugger
precompiler
gcc, etc.
link
compiler
linker
executable program
compiler
object code (binary, one per compilation unit) .o
15
Development Environment Studio
  • Well follow a similar format most days in the
    course
  • Around 30 minutes of lecture and discussion
  • Then about 60 minutes of studio time
  • Except for open studio/lab days, reviews before
    the midterm and final, and the day of the midterm
    itself
  • In the studios, please work in groups of 2 or 3
  • Exercises are posted on the course web page
  • Record your answers to the exercises, and e-mail
    your answers to the course account when youre
    done
  • Well migrate throughout the studio to answer
    questions
  • Use studio time to develop skills and
    understanding
  • A good chance to explore ideas you can use for
    the labs
  • Exams will test understanding of the studio
    material
  • Youre encouraged to try variations beyond the
    exercises
Write a Comment
User Comments (0)
About PowerShow.com