Lecture 12 Speak Shout Code
speak.h
/*
* speak.h - interface to module to write messages to stdout
*/
#ifndef SPEAK_H_
#define SPEAK_H_
/* Write message m to stdout followed by a newline */
void speak(char m[]);
#endif // SPEAK_H_
speak.c
/*
* speak.c - implementation of module to write messages to stdout
*/
#include <stdio.h>
#include "speak.h"
/* Write message m to stdout followed by a newline */
void speak(char m[]) {
printf("%s\n", m);
}
shout.h
/*
* shout.h - interface to module to write messages to stdout loudly
*/
#ifndef SHOUT_H_
#define SHOUT_H_
/* Write message m in uppercase to stdout followed by a newline */
void shout(char m[]);
#endif // SHOUT_H_
shout.c
/*
* shout.c - implementation of module to write messages to stdout loudly
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "speak.h"
#include "shout.h"
/* Write message m in uppercase to stdout followed by a newline */
void shout(char m[]) {
int len; /* message length */
char* mcopy; /* copy of original message */
int i;
len = strlen(m);
mcopy = (char*)malloc(len*sizeof(char)+1);
strcpy(mcopy, m);
for (i = 0; i < len; i++)
mcopy[i] = toupper(mcopy[i]);
speak(mcopy);
free(mcopy);
}
main.c
/*
* main.c - demo/test program for speak and shout functions
*/
#include "speak.h"
#include "shout.h"
/* Say HELLO and goodbye */
int main(int argc, char* argv[]) {
shout("hello");
speak("goodbye");
return 0;
}
Makefile
# Very simple Makefile for talk/speak/shout program
CC = gcc
CFLAGS = -Wall -g -std=c11
# default target
all: talk
talk: main.o speak.o shout.o
$(CC) $(CFLAGS) -o talk main.o speak.o shout.o
# individual source files
speak.o: speak.c speak.h
$(CC) $(CFLAGS) -c speak.c
shout.o: shout.c shout.h speak.h
$(CC) $(CFLAGS) -c shout.c
main.o: main.c speak.h shout.h
$(CC) $(CFLAGS) -c main.c
# a "phony" target to remove built files and backups
clean:
rm -rf talk *.o *~