Section 1: Intro to C - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Section 1: Intro to C

Description:

TA: Chris Stocker. e-mail:stocker2_at_.uiuc.edu. TA Office: 0228 Siebel Center (in the ... Homepage Resources Tutorials accessing the CS newsserver from pine... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 18
Provided by: csU70
Category:
Tags: chris | intro | pine | section

less

Transcript and Presenter's Notes

Title: Section 1: Intro to C


1
Section 1 Intro to C
  • CS 225 Data Structures Software Principles

2
Administrative Details
  • TA Chris Stocker
  • e-mailstocker2_at_.uiuc.edu
  • TA Office 0228 Siebel Center (in the basement)
  • Office Hours Thursdays 1-3
  • Section Web Page
  • Go to http//www.cs.uiuc.edu/class/fa07/cs225/
  • Click Section on the left, then click my name

3
Newsgroups
  • Course newsgroups
  • Get an account http//news.cs.uiuc.edu
  • Homepage ? Resources ? Tutorials ? accessing the
    CS newsserver from pine
  • Pine is always there for you
  • but you dont want to use it. Get another news
    reader. (Outlook Express, Mozilla Thunderbird,
    )
  • class.cs225, class.cs225.announce
  • Testing goes on cs.test!

4
C Code Layout
  • Two main types of C files
  • Header files (.h)
  • Contains class and function declarations (the
    interface)
  • Source files (.cpp)
  • Contains class and function definitions (the
    implementation)
  • Driver
  • int main() where the action begins
  • Convention return 0 on success, non-zero for
    error

5
C Code Layout
  • Classes are usually constructed using both a
    header file and a source file
  • Ex. "myclass.h" contains class declaration,
    listing of member variables and functions
    "myclass.cpp" contains implementation of member
    functions
  • This isn't a requirement!
  • The compiler doesnt care what your files are
    named
  • Can have functions that dont belong to any class

6
Using include
  • include works like copy-and-paste
  • Ex. include myclass.h would insert the
    contents of file myclass.h
  • Need to include a classs header file before
    referencing the class
  • include always goes at the top of the file
  • System libraries are referenced with ltgt, local
    headers with , e.g.
  • include ltiostreamgt, but
  • include "asserts.h (here asserts.h is your
    code)

7
Using include
  • Beware of double inclusion!
  • Same file included twice
  • Compiler sees two sets of declarations, and
    freaks out (defining two identical
    classes/functions/whatever is a no-no!)
  • This can be tricky(file A includes file C file
    B includes A and C. Now B includes C twice!)
  • Solution put this in every header file --
  • ifndef MYFILE_Hdefine MYFILE_H// code goes
    hereendif

8
Main()
  • Just like in Java, every program starts with the
    function "main"
  • main (for the purposes of this class) takes no
    arguments, and returns an int
  • main does not belong to a class (unlike in Java)
  • We will usually put main in its own file

9
C Input/Output
  • Input and Output are defined on streams
  • A stream is an abstraction of an I/O device
  • Some common streams, defined in the iostream
    system library
  • cin (standard in, i.e. the keyboard)
  • cout (standard out, i.e. the terminal)
  • cerr (standard error used for debugging
    output)
  • Write to a stream with "ltlt", read from one with
    "gtgt

10
C versus Java
  • Java
  • System.out.println(Hello World)
  • System.out.print(Hello, my name is )
  • C
  • cout ltlt Hello World ltlt endl
  • cout ltlt Hello, my name is
  • Also, input is EASY, for example if x is an
    integer we can say
  • cin gtgt x
  • and get a value for x from the command line.

11
C Input/Output
  • The streams defined in iostream use the "std"
    namespace
  • How this affects you
  • EITHER put "using namespace std" near the top of
    any file in which you use I/O,
  • OR write "stdcout" instead of "cout",
    "stdcin" instead of "cin", etc.
  • OR put "using stdcout", "using stdcin", etc.
    near the top of the file
  • The first option is easiest, but the third is
    preferred!
  • Namespaces are weird dont worry too much about
    them

12
Sample Program
  • include ltiostreamgt
  • int main( )
  • int a, b
  • stdcin gtgt a gtgt b
  • stdcout ltlt ab ltlt stdendl
  • return 0

13
Compiling C
  • How to compile code using g
  • Compile each .cpp file into an object (.o) file
  • Usage g -c -g file1, file2,
  • e.g. g -c -g foo.cpp compiles foo.cpp into
    foo.og -c -g .cpp compiles all .cpp files in
    the working directory
  • Link all the object files together to produce an
    executable
  • Usage g -o outFile obj1, obj2,
  • e.g. g -o myProgram foo.o bar.o links foo.o
    and bar.o into executable myProgramg -o
    myProgram .o links together all .o files in the
    working directory

14
Compiling C
  • This can be tedious!
  • To save time, we use the UNIX utility make
  • make processes commands from a file called a
    Makefile
  • Given a working Makefile, you can build
    everything with the command make
  • make can also automate common tasks, e.g.
    deleting all .o files (so that you can compile
    from scratch)
  • Read the Makefile tutorial on the course website

15
C vs Java
  • A decent webpage for Java vs C comparison
  • http//www-ali.cs.umass.edu/mckinley/377/labs/c
    _for_java_programmers.html
  • Remember to declare your classes first in the
    header file.
  • If(ab) will compile and return false iff b0.
  • Different .cpp files are just like seperating
    classes into different .java files.
  • The .h file just allows you to split up your
    function declarations and the rest of your code.
  • Semicolons are needed after class declarations
    (but only in the declaration).

16
C vs Java
  • Default access to class members is private,
    declare parts to be public with
  • public
  • code
  • private
  • code
  • Java has standards on order for some code like
  • ii will be consistant (this is BAD code)
  • 8/-5 for integers will always round down to -1
  • C can work differently on different systems
  • ii may or may not increment I
  • 8/-5 may round down to 1 or truncate to -2

17
MP0
  • Find and do MP0
  • Start at http//cs.uiuc.edu/class/fa07/cs225
  • Click on Assignments
  • And finally MP0
  • The MP0 files are at cs225/src/lab/lab1/mp0
  • Read the Makefile tutorial if you havent already.
Write a Comment
User Comments (0)
About PowerShow.com