#include <stdio.h>
void printStars(int n) {
int i ;
for (i=0; i<n; ++i) {
putchar('*') ;
}
}
#include <stdio.h>
#include <stdlib.h>
void printStars(int) ;
int main(void) {
int n ;
printf("How many stars to print?\n") ;
scanf("%d", &n) ;
printStars(n) ;
return(EXIT_SUCCESS) ;
}
#include <stdio.h>
void printChars(char c, int n) {
int i ;
for (i=0; i<n; ++i) {
putchar(c) ;
}
}
void printChars(char c, int n) ;
void printStars(int n) {
printChars('*', n) ;
}
void printChars(char c, int n) ;
void printRect(char c, int width, int heigth) {
int i ;
for (i=0; i<heigth; ++i) {
printChars(c, width) ;
}
}
Suppose you wanted to write a function that received three values and returned the largest and smallest?
You can't.
Pointers are C values that contain addresses.
int *pInteger ; double *pDouble ; char *pChar ; int ***pConfusing ;
& |
unary operator that returns the address of a lvalue |
* |
unary operator that deferences a pointer, that is, returns value stored at an address |
int i, j ; int *p, *q ; p = &i ; q = &j ; j = 5 ; *p = *q + 1 ; p = q ; *p = *q + 1 ;
void MinMax(int x, int y, int z, int *lo, int *hi) {
int t ;
if (x > y) {
t = x ;
x = y ;
y = t ;
}
if (y > z) {
t = y ;
y = z ;
z = t ;
}
if (x > y) {
t = x ;
x = y ;
y = t ;
}
*lo = x ;
*hi = z ;
}
unsigned long factorial(unsigned int n) {
int f, i ;
f = 1 ;
for (i=1; i<=n; ++i) {
f = f*i ;
}
return f ;
}
unsigned long permutations(unsigned int m, unsigned int n) {
int p, i ;
p = 1 ;
for (i=1; i<=m; ++i) {
p = p * n ;
--n ;
}
return p ;
}
unsigned long combinations(unsigned int m, unsigned int n) {
int c, i ;
c = 1 ;
for (i=1; i<=m; ++i) {
c = c * n / i ;
--n ;
}
return c ;
}
unsigned long factorial(unsigned int n) {
if (n==0) {
return 1 ;
} else {
return n*factorial(n-1) ;
}
}
unsigned long permutations(unsigned int m, unsigned int n) {
if (m==0) {
return 1 ;
} else {
return n*permutations(m-1,n-1) ;
}
}
unsigned long combinations(unsigned int m, unsigned int n) {
if (m==0) {
return 1 ;
} else {
return n*combinations(m-1,n-1)/m ;
}
}
Put your prototypes declarations in a header (or include or prototype) file whose name ends with the .h extension.
unsigned long combinations(unsigned int m, unsigned int n) ;
Put your function in a separate file and include the prototype file to make sure both agree.
#include "combinations.h"
unsigned long combinations(unsigned int m, unsigned int n) {
if (m==0) {
return 1 ;
} else {
return n*combinations(m-1,n-1)/m ;
}
}
Any program that uses your function should also include the header file.
#include <stdio.h>
#include <stdlib.h>
#include "combinations.h"
int main(void) {
printf("There are %lu different poker hands\n",
combinations(5,52)) ;
return(EXIT_SUCCESS) ;
}
If you are not using an IDE, such as Netbeans, you'll have to compile the programs separately.
gcc -c -ansi -pendantic -Wall numhands.c gcc -c -ansi -pendantic -Wall combinations.c gcc -o numhands numhands.o combinations.o
Try out the following to see how arguments are stored on the stack.
#include <stdio.h>
#include "combinations.h"
unsigned long combinations(unsigned int m, unsigned int n) {
printf("m=%d, n=%d, &m=%p, &n=%p\n",
m, n, (int) &m, (int) &n) ;
if (m==0) {
return 1 ;
} else {
return n*combinations(m-1,n-1)/m ;
}
}