You are on page 1of 10

C Programming Revision

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

159.234

OO Programming

#include <stdio.h> #include <math.h> 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(i=0;i<5;i++){ /* 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; si = 16383; li = (int)( pow( 2, 31 ) ); /* 2 to the power of 31 */ fprintf(stderr,"li = %ld\n", li ); while( (ch = getc( stdin )) != EOF ){ putc(ch, stdout); } return 0; }

Example output: (terminated with ^D or ^Z)


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

159.234

OO Programming

#include <stdio.h> void printArray( int *iarr, int n ){ /* function to print out an int array */ int i; /* print to stderr, could have used stdout instead */ for(i=0;i<n;i++){ fprintf(stderr, "%d\t", iarr[i] ); /* 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 iarray[4]; /* 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; iarray[i] = 42; printArray( iarray, 4 ); for(i=0;i<4;i++){ iarray[i] = i * 10; } printArray( iarray, 4 ); ipointer = &i; printf("ipointer = %u\n", ipointer ); /* print an unsigned decimal */ printf("contents of ipointer = %d\n", *ipointer ); for(ipointer = iarray;ipointer < (iarray + 4); ipointer++){ *ipointer = *ipointer + 1; } printArray( iarray, 4 ); return 0; }

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

159.234

OO Programming

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
159.234

In C and C++ arrays are implemented via pointer arithmetic int iarray[10]; 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!

OO Programming

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 the is a part of the computers memory organised like a stack of cards.
159.234

Variables set up on the stack typically last only for the duration of execution of the function that declares them.

OO Programming

Stack example
int myfunc(); /* prototype */ int main(){ int i; myfunc(); return 0; } int myfunc(){ int j = 10; }
159.234

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()
OO Programming 6

Global Variables - use with Caution


int k = 42; int myfunc(){ int j = 10; k = k + j; return j; } int main(){ int i = 3; int k = 7; i = myfunc(); printf(i = %d, k = %d\n, i, k ); return 0; }
159.234

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
OO Programming 7

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
159.234 OO Programming

int *ia; /* a global pointer */ int myfunc(){ ia = (int *)malloc( 10 * sizeof(int) ); ia[2] = 7; } int main(){ myfunc(); ia[0] = 8; ia[9] = 87; free( ia ); return 0; }

Miscellany of 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
159.234

Need #include <maths.h> to use the maths library functions eg sqrt() printf()/scanf() & fprintf()/fscanf() I/O routines putc()/getc() for single character I/O
9

OO Programming

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
159.234

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
10

OO Programming

You might also like