9.1 함수가 필요할 때

공기훈·2021년 10월 25일
0

홍정모의 따배씨

목록 보기
49/49
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define WIDTH 40
#define NAME "Gi-hun Gong"
#define ADDRESS "Seoul, Korea"

void print_chmutlple_chars(char c, int n_stars, bool endl)
{
	for (int i = 0; i < n_stars; ++i)
		printf("%c", c);

	if (endl == true)
		printf("\n");
}

void print_centered_str(char str[])
{
	int n_blanks = 0;

	n_blanks = (WIDTH - strlen(str)) / 2;
	print_chmutlple_chars(' ', n_blanks, false);
	printf("%s\n", str);
}

int main(void) 
{
	print_chmutlple_chars('*', WIDTH, true);

	print_centered_str(NAME);
	print_centered_str(ADDRESS);
	print_centered_str("I love you");

	print_chmutlple_chars('*', WIDTH, false);

	return 0;
}

코드를 일반화시킬 때 다양한 함수를 사용하는 것이 좋다.

profile
be a coding master

0개의 댓글