๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ C Programming/๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ์ฆ๊ธฐ๋Š” C ์–ธ์–ด ์ฝ˜์„œํŠธ_๊ฐœ์ • 3ํŒ

[๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ์ฆ๊ธฐ๋Š” C ์–ธ์–ด ์ฝ˜์„œํŠธ - ๊ฐœ์ • 3ํŒ] 14์žฅ Programming ํ’€์ด

by A Lim Han 2023. 2. 4.

# 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;
}

์ขŒ์ธก๋ถ€ํ„ฐ add.c, main.c
add.h์™€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

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;
}

์ขŒ์ธก๋ถ€ํ„ฐ hello.h, hello.c
main.c
ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

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;
}

array.c
main.c
์ขŒ์ธก๋ถ€ํ„ฐ array.h, ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

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;
}

์ขŒ์ธก๋ถ€ํ„ฐ ๋ณต์‚ฌ ์ „์˜ two.txt ๊ณผ one.txt ํŒŒ์ผ, ๋ณต์‚ฌ ํ›„์˜ two.txt ์™€ one.txt ํŒŒ์ผ

+ copyfile.exe ์‹คํ–‰ํŒŒ์ผ๊ณผ one.txt, two.txt ํ…์ŠคํŠธ ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ๊ฒฝ๋กœ๋Š” C:\copyfile\x64\Debug

 

 

 

 

+ ์ฐธ๊ณ 

https://ddoddofather.tistory.com/entry/C%EC%96%B8%EC%96%B4-%ED%8C%8C%EC%9D%BC%EC%9E%85%EC%B6%9C%EB%A0%A5-main%EC%97%90%EC%84%9C-%ED%8C%8C%EC%9D%BC%EB%AA%85%EC%9D%84-%EB%B0%9B%EC%95%84-%ED%8C%8C%EC%9D%BC%EB%B3%B5%EC%82%AC%ED%95%98%EA%B8%B0