I think this is kind of what you're trying to do. At any rate, maybe it will help you see how functions work:
Code:
itsme@itsme:~/C$ cat card.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int pick_random_card(void)
{
return rand() % 52;
}
char *card_to_str(int card)
{
char *values[] = { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
char *suits[] = { "Hearts", "Spades", "Diamonds", "Clubs" };
int value;
int suit;
static char buf[100];
value = card % 13;
suit = card % 4;
sprintf(buf, "%s of %s", values[value], suits[suit]);
return buf;
}
int main(void)
{
int card;
char *str;
int i;
srand(time(NULL));
// Show 5 random cards
for(i = 0;i < 5;++i)
{
card = pick_random_card();
str = card_to_str(card);
puts(str);
}
return 0;
}
Code:
itsme@itsme:~/C$ ./card
Six of Diamonds
Five of Clubs
Six of Spades
Nine of Spades
Ace of Diamonds
itsme@itsme:~/C$