C Programming Revision - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

C Programming Revision

Description:

double d; /* double precision - about 15 significant figures ... d. d. e. e. hello. hello. 159.234. OO Programming. 3 #include stdio.h ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 17
Provided by: ITS8213
Category:

less

Transcript and Presenter's Notes

Title: C Programming Revision


1
C Programming Revision
  • Simple C program layout
  • C data types
  • printf output
  • C pointers and arrays
  • Stack and Heap memory
  • Global vs Local variables

2
  • Example output
  • (terminated with D or Z)
  • include ltstdio.hgt
  • include ltmath.hgt
  • int main()
  • char ch / is really an
    int from -128 to 0 to 127 /
  • unsigned char uch / is really an int from
    0 to 255 /
  • short int si / can just say
    short si /
  • int i / usually same
    as machine word ie 32 bits /
  • long int li / could be 32
    but may be 64 bits /
  • long long int ll / generally is 64
    bits on gcc systems /
  • float f / IEEE format floating point,
    32 bits usually /
  • double d / double precision - about 15
    significant figures /
  • for(i0ilt5i) / loop from 0...4 ie do
    it 5 times /
  • printf("d 2 d\n", i, i 2 ) /
    print a string with 2 ints /
  • ch '\n' / the newline character
    /
  • uch 255

0 2 0 1 2 2 2 2 4 3 2 6 4 2
8 li 2147483647 a a b b c c d d e e hello hello
3
  • include ltstdio.hgt
  • void printArray( int iarr, int n ) /
    function to print out an int array /
  • int i / print to stderr, could
    have used stdout instead /
  • for(i0iltni)
  • fprintf(stderr, "d\t", iarri ) /
    separate them with tabs /
  • fprintf(stderr,"\n") /
    finish with a newline /
  • int main()
  • int i / an integer takes 32 bits
    of memory on the stack /
  • int iarray4 / will take /
  • int ipointer
  • printf("size of int d\n", sizeof(i) )
  • printf("size of iarray d\n", sizeof(iarray)
    )
  • printf("size of ipointer d\n",
    sizeof(ipointer) )
  • i 0
  • iarrayi 42
  • printArray( iarray, 4 )
  • for(i0ilt4i)
  • iarrayi i 10
  • Example output
  • size of int 4
  • size of iarray 16
  • size of ipointer 4
  • 42 49 6 3
  • 0 10 20 30
  • ipointer 3221224552
  • contents of ipointer 4
  • 1 11 21 31

4
Pointers
  • Pointers in C and C are a special data type
    that is capable of pointing to the memory
    location of a normal data type.
  • Pointers are typed - eg pointer to an int is
    not the same as pointer to a char
  • Pointers let us do address manipulation and
    are a powerful but dangerous feature of the
    language
  • In C and C arrays are implemented via pointer
    arithmetic
  • int iarray10 tells the compiler you want the
    space for 10 integers set aside (not yet
    initialised)
  • int iptr sets aside memory for a pointer but
    not for an actual integer!

5
Working with pointers
  • Address operator
  • float x 2.71828
  • x means address of x
  • Declaring pointers
  • float p x means give me a variable called p
    of type pointer to float and initialize it to
    the address of x
  • float p does the same thing as float p
  • De-reference (or indirection) operator
  • p means give me the object at address p

6
Stack Memory
  • When you write and run a program each function
    can set up its own variable s on what is
    known as the stack - a part of the
    computers memory organised like a stack of
    cards.
  • Variables set up on the stack typically last only
    for the duration of execution of the function
    that declares them.

7
Stack example
  • int myfunc() / prototype /
  • int main()
  • int i
  • myfunc()
  • return 0
  • int myfunc()
  • int j 10
  • i is a stack variable set up by the function
    main()
  • j is a stack variable set up by the function
    myfunc() and will last only for the duration of
    the call to myfunc()

8
Global Variables - use with Caution
  • int k 42
  • int myfunc()
  • int j 10
  • k k j
  • return k
  • int main()
  • int i 3
  • int k 7
  • i myfunc()
  • printf(i d, k d\n, i, k )
  • return 0
  • k is a global variable
  • It is accessible to both main() and myfunc()
  • main() declares its own stack variable k which
    hides the global k
  • Output would be
  • 52, 7

9
DO NOT USE GLOBAL VARIABLES
10
Heap Memory
  • The Heap is a pool of memory available to all
    functions which they must access using
    malloc() and related functions
  • Mallocd memory stays allocated until you free
    it
  • This must be used with great care (otherwise get
    Bus Error or Segmentation Fault)
  • Remember to allocate memory before you try to
    use it
  • Remember to free up memory once you are finished
    with it.
  • C has neater ways of managing this
  • int ia / a global pointer /
  • int myfunc()
  • ia (int )malloc( 10 sizeof(int) )
  • ia2 7
  • int main()
  • myfunc()
  • ia0 8
  • ia9 87
  • free( ia )
  • return 0

11
Arrays and pointers
  • When we create an array, the compiler allocates
    as much memory as requested an remembers the
    address of the first element
  • char s55
  • Allocates 55 bytes starting at the address of
    s0
  • s is shorthand for s0
  • si is shorthand for (s i)

12
Dynamic memory allocation
  • Char s55 declares an array of fixed length, but
    memory can be allocated dynamically
  • int nvalues 55
  • char p (char )malloc( nvalues sizeof(char)
    )
  • malloc(n) allocates a block of memory of n bytes
    and returns the address of the start of the block
  • The return type type is void
  • A raw pointer to a variable of unspecified type
  • Needs to be recast to the appropriate pointer type

13
Miscellaneous C Features
  • if( ) else
  • while( )
  • do while( )
  • Use of ints 0 or not zero in place of
    false/true
  • Functions can call themselves (recursion)
  • No string type in C so use arrays of char,
    terminated by \0 character
  • Need include ltmaths.hgt
  • to use the maths library functions eg
    sqrt()
  • printf()/scanf() fprintf()/fscanf() I/O
    routines
  • putc()/getc() for single character I/O

14
C programming methodology
  • A procedural oriented language
  • Or an action oriented language
  • Based on functions of variables which call one
    another
  • OO goes beyond this

15
Objects in C
  • An object is a named area of memory that holds
    data
  • Eg, consider the statement
  • int num 1234
  • The compiler generates a symbol table that
    associates the name of an object (ie variable)
    with its location (ie address) in computer memory

num
Location in memory (address)
1234
16
Summary
  • Think of pointers as memory addresses
  • Memory your program uses can be stack or heap
    memory
  • Stack variables last only as long as the called
    function does
  • Heap variables last until you free them up
  • Look at the C programming revision material on
    the web site.
  • C is based around the idea of functions which
    call one another
  • OO programming goes beyond this
  • Next - C simple I/O
Write a Comment
User Comments (0)
About PowerShow.com