# 14์ฅ Programming ํ์ด
547p_1๋ฒ)
#include <stdio.h>
#define DEBUG // Line 2๋ฅผ ์ฃผ์์ฒ๋ฆฌํ๋ฉด ๋งคํฌ๋ก ์ ์๊ฐ ํด์ ๋จ
void cal(int *ptr)
{
int sum = 0;
#ifdef DEBUG
printf("๋ฐฐ์ด array๋ก๋ถํฐ ๋ฐ์์จ ๋งค๊ฐ๋ณ์: \n");
for (int i = 0; i < 8; i++)
{
if (i == 7)
{
printf("%d ", ptr[i]);
}
else
{
printf("%d, ", ptr[i]);
}
}
printf("\n\n");
#endif
for (int i = 0; i < 8; i++)
{
sum += ptr[i];
#ifdef DEBUG
printf("%d๋ฅผ ๋ํ๋ ์ค\n", ptr[i]);
#endif
}
printf("\n๋ฐฐ์ด์ ํ๊ท : %d\n", sum / 8);
}
int main()
{
int array[] = { 1,2,3,4,6,7,8,9 };
cal(array);
return 0;
}
547p_2๋ฒ)
// add.h
#pragma once
int add(int x, int y);
//add.c
#include <stdio.h>
int add(int x, int y)
{
int result = 0;
result = x + y;
return result;
}
//main.c
#include <stdio.h>
#include "add.h"
int main()
{
int a = 0;
int b = 0;
printf("์ ์ 2๊ฐ๋ฅผ ์
๋ ฅํ์์ค: ");
scanf_s("%d %d", &a, &b);
printf("๋ ์์ ํฉ: %d\n", add(a, b));
return 0;
}
547p_3๋ฒ)
//hello.h
#pragma once
void hello(char* name);
//hello.c
#include <stdio.h>
void hello(char* name)
{
printf("%s๋ ์๋
ํ์ธ์.\n", name);
}
//main.c
#include <stdio.h>
#include "hello.h"
int main()
{
char name[10];
printf("์ฑ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์: ");
gets_s(name, 10);
hello(name);
return 0;
}
548p_4๋ฒ)
//array.h
#pragma once
int sum(int a[], int size);
void print_array(int a[], int size);
//array.c
#include <stdio.h>
int sum(int a[], int size)
{
int result = 0;
for (int i = 0; i < size; i++)
{
result += a[i];
}
return result;
}
void print_array(int a[], int size)
{
for (int k = 0; k < size; k++)
{
printf("%d ", a[k]);
}
printf("\n");
}
//main.c
#include <stdio.h>
#include "array.h"
int main()
{
int array1[5] = { 0,1,2,3,4 };
printf("๋ฐฐ์ด ์์๋ค: ");
print_array(array1, 5);
printf("๋ฐฐ์ด ์์๋ค์ ํฉ: %d", sum(array1, 5));
printf("\n");
return 0;
}
548p_5๋ฒ)
#include <stdio.h>
int main(int argc, char** argv)
{
if (argc == 3)
{
char x;
FILE* fp1;
fopen_s(&fp1, argv[1], "rt");
FILE* fp2;
fopen_s(&fp2, argv[2], "wt");
if (fp1 == NULL || fp2 == NULL)
{
printf("ํ์ผ์ ์ด ์ ์์ต๋๋ค.\n");
return 1;
}
while (1)
{
if (feof(fp1) != 0)
{
printf("๋ณต์ฌ๊ฐ ์๋ฃ๋์์ต๋๋ค.\n");
break;
}
x = fgetc(fp1);
fputc(x, fp2);
}
fclose(fp1);
fclose(fp2);
}
else
{
printf("ํ๋ก๊ทธ๋จ ์ธ์๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.\n");
return 1;
}
return 0;
}
+ copyfile.exe ์คํํ์ผ๊ณผ one.txt, two.txt ํ ์คํธ ํ์ผ์ด ์์นํ ๊ฒฝ๋ก๋ C:\copyfile\x64\Debug
+ ์ฐธ๊ณ