I'm trying to teach myself C. I've managed to make this program I call HappyToYou.c (inspired by this video):
#include <stdio.h>
//#include <stdlib.h> //malloc would need this.
#include <string.h>
#include <err.h>
void sing(char *start, char *end){
printf("%s%s.\n", start, end);
}
int main(int argc, char *argv[]){
if(argc!=3) errx(1, "Give a holiday and a name as arguments.");
char *happy="Happy ";
char start[strlen(happy)+strlen(argv[1])+1];
strcpy(start, happy);
strcat(start, argv[1]);
char *to=" to you";
char *dear=" dear, ";
char person[strlen(dear)+strlen(argv[2])+1];
strcpy(person, dear);
strcat(person, argv[2]);
sing(start, to);
sing(start, to);
sing(start, person);
sing(start, to);
return 0;
}
I think it is free from bugs (as far as I know). I'm pretty sure it can be trimmed down but I'm not sure how.