C Fundamentals - PowerPoint PPT Presentation

About This Presentation
Title:

C Fundamentals

Description:

C Fundamentals. CS-240. Dick Steflik. Primitive Types. Integer types ... long : 64 bit. Real Types. float - 32 bit real numbers. double - 64 bit real numbers ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 14
Provided by: dickst
Category:

less

Transcript and Presenter's Notes

Title: C Fundamentals


1
C Fundamentals
  • CS-240
  • Dick Steflik

2
Primitive Types
  • Integer types
  • char 8 bit
  • short 16 bit
  • int 32 bit
  • long 64 bit
  • Real Types
  • float - 32 bit real numbers
  • double - 64 bit real numbers

3
Constants
  • In Java constants are variables that are never
    allowed to change value
  • public static final int abc 100
  • In C constants are defined as
  • const int abc 100 const char plus
  • constants are resolved by the compiler
    preprocessor by doing text substitutions of the
    value for all occurrences of the name.

4
Boolean (false/true)
  • C has no boolean type like Java
  • the value 0 represents false
  • any other value represents true
  • the statement if (50) cout ltltt
  • will always print t as 50 is not 0 so the
    predicate is true
  • the statement if (5-5) coutltltt else cout ltlt
    f
  • will always print f as (5-5) is 0 (i.e. False)

5
Strings
  • C has no built-in String type like Java
  • Strings are implemented as null terminated arrays
    of characters.
  • char 3 abc me
  • array abc consists of m, e, null
  • abc me (shortcut for defining and
    initializing a string)
  • string functions are in ltstring.hgt

6
Derived Types
  • These operators create new types from the basic
    types
  • pointer to
  • const constant pointer
  • reference to (addressing operator)
  • vector of
  • () function returning

7
Null statements
  • the simplest statement is the null statement
  • useful if the syntax requires a statement
  • for instance to introduce a time delay into a
    program
  • for (int j 0 j lt 100 j)

8
C Program Structure
  • A C Program consists a set of file(s)
    consisting of declarations of
  • types
  • functions
  • variables
  • constants

9
externalization
  • To use a name to refer to the same thing in two
    different source files it must be defined as
    external
  • extern double sqrt(double)
  • extern istream cin
  • extern int abcd

10
Header files
  • To guarantee consistency between source files
    externalize definitions into header (.h) files
  • for instance math.h contains the externalized
    definitions for interfacing to the mathematic
    system utilities.
  • To use the functions in math.h include math.h in
    your source file
  • include ltmath.hgt

11
Header files (cont.)
  • math.h is provided as part of C but you can
    make your own header files
  • to include your own header file, place it in the
    same directory as the files to use it and in each
    source file place the line
  • include myfile.h
  • note the instead if lt gt, use lt gt to include
    from the standard include directory use to
    include from elsewhere (full path)

12
Example
header.h
main.cpp
extern char prog_name extern void f( )
include header.h char prog_name
silly, but complete main() f()
f.cpp
include ltiostream.hgt include header.h void
f() cout ltlt prog_name ltlt \n
13
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com