1. 구조체 2

#include <stdio.h>

typedef int INT;
typedef int * PTR_INT;
typedef unsigned int UINT;
typedef unsigned int * PTR_UINT;
typedef unsigned char UCHAR;
typedef unsigned char * PTR_UCHAR;

int main()
{
	INT num1 = 120;
	PTR_INT pnum1 = &num1;
	UINT num2 = 190;
	PTR_UINT pnum2 = &num2;
	UCHAR ch = 'z';
	PTR_UCHAR pch = &ch;
	printf("%d, %u, %c\n", *pnum1, *pnum2, *pch);
}

#include <stdio.h>

struct point
{
	int xpos;
	int ypos;
};

typedef struct point Point;

typedef struct person
{
	char name[20];
	char phoneNum[20];
	int age;
} Person;

int main(){
	Point pos = {10, 20};
	Person man = {"이승기", "010-1211-1111", 11};
	printf("%d %d \n", pos.xpos, pos.ypos);
	printf("%s %s %d \n", man.name, man.phoneNum, man.age);
}

#include <stdio.h>

typedef struct point
{
	int xpos;
	int ypos;
} Point;

void ShowPosition(Point pos)
{
	printf("[%d, %d] \n", pos.xpos, pos.ypos);
}

Point GetCurrentPosition(void)
{
	Point cen;
	printf("Input current pos : ");
	scanf("%d %d", &cen.xpos, &cen.ypos);
	return cen;
}

int main()
{
	Point curPos = GetCurrentPosition();
	ShowPosition(curPos);
	return 0;
}

#include <stdio.h>
// struct   function call by ref

typedef struct point{
	int xpos;
	int ypos;
} Point;

void OrgSymTrans(Point * ptr)
{
	ptr->xpos = (ptr->xpos) *-1;
	ptr->ypos = (ptr->ypos) *-1;
}

void ShowPosition(Point pos)
{
	printf("[%d %d] \n", pos.xpos, pos.ypos);
}

int main()
{
	Point pos = {7, -5};
	OrgSymTrans(&pos);
	ShowPosition(pos);
	OrgSymTrans(&pos);
	ShowPosition(pos);
}

#include <stdio.h>

typedef struct point{
	int xpos;
	int ypos;
}Point;

Point AddPoint(Point pos1, Point pos2)
{
	Point pos = {pos1.xpos + pos2.xpos, pos1.ypos + pos2.ypos};
	return pos;
}

Point MinPoint(Point pos1, Point pos2)
{
	Point pos = {pos1.xpos - pos2.xpos, pos1.ypos -pos2.ypos};
	return pos;
}


int main()
{
	Point pos1= {5, 6};
	Point pos2 = {2, 9};
	Point result; 
	
	result = AddPoint(pos1, pos2);
	printf("[%d, %d] \n", result.xpos, result.ypos);
	result = MinPoint(pos1, pos2);
	printf("[%d, %d] \n", result.xpos, result.ypos);
}

#include <stdio.h>

typedef enum syllabel
{
	Do = 1, Re = 2, Mi = 3, Fa = 4, So = 5, La = 6, Ti =7
} Syllabel;

void Sound(Syllabel sy)
{
	switch(sy)
	{
		case Do:
			puts("도는 하얀 도라지");
			return ;
		case Re:
			puts("레는 둥근 레코드");
			return ;
		case Mi:
			puts("미는 파란 미나리");
			return ;
		case Fa:
			puts("파는 예쁜 파랑새");
			return ;
		case So:
			puts("솔은 작은솔방울");
			return ;
		case La:
			puts("라는 라디오고요");
			return ;
		case Ti:
			puts("시는 졸졸시냇물");
	}
	puts("다함께 부르세~");
}

int main()
{
	Syllabel tone;
	
	for(tone=1; tone <=Ti; tone+=1)
		Sound(tone);
}

 

 

2. 메모리 동적 할당

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int *ptr1 = (int *)malloc(sizeof(int));
	int *ptr2 = (int *)malloc(sizeof(int)*7);
	int i;

	*ptr1 = 20;
	for(i=0; i< 7; i++)
		ptr2[i] = i + 1;
	
	printf("%d \n", *ptr1);
	for(i=0; i <7; i++)
		printf("%d ", ptr2[i]);
	
	free(ptr1);
	free(ptr2);
}

#include <stdio.h>
#include <stdlib.h>

char * ReadUserName()
{
	char * name = (char *)malloc(sizeof(char) * 30);
	printf("what's your name? ");
	gets(name);
	return name;
}


int main()
{
	char *name1;
	char *name2;
	name1 = ReadUserName();
	printf("name1 : %s \n ", name1);
	name2 = ReadUserName();
	printf("name2 : %s \n\n", name2);
	
	printf("again name1 : %s \n", name1);
	printf("again name2 : %s \n", name2);
	free(name1);
	free(name2);
}

 

 

 

3. 전처리

#include <stdio.h>
#define SQUARE(X) X*X

int main(){
	int num = 20;
	printf("sq of num : %d \n", SQUARE(num));
	printf("sq of -5 : %d \n", SQUARE(-5));
	printf("sq of 2.5 : %g \n", SQUARE(2.5));
}

#include <stdio.h>
#define PI 3.14
#define PRODUCT(X,Y) ((X) * (Y))
#define CIRCLE_AREA(R) (PRODUCT(R, R) * PI)

int main()
{
	double rad = 2.1;
	printf("반지름이 %g인 원의 넓이 : %g \n", rad, CIRCLE_AREA(rad));
}

 

#include <stdio.h>
#define ADD 1
#define MIN 0

int main()
{
	int num1, num2;
	printf("두 개의 정수 입력 :");
	scanf("%d %d", &num1, &num2);
	
#if ADD
	printf("%d + %d = %d \n", num1, num2, num1+num2);
#endif

#if MIN
	printf("%d - %d = %d \n", num1, num2, num1 - num2);
#endif	
}

#include <stdio.h>
//#define ADD 1
#define MIN 0

int main()
{
	int num1, num2;
	printf("두 정수 입력 : ");
	scanf("%d %d", &num1, &num2);
	
#ifdef ADD
	printf("%d + %d = %d \n", num1, num2, num1 + num2);
#endif

#ifdef MIN
	printf("%d - %d = %d \n", num1, num2, num1 - num2);
#endif
}

 

4. 파일 분할

4.1 산술 연산 예제

- main.c

- basicArith.h, basicArith.c

- areaArith.h, areaArith.c

- roundArith.h, roundArith.c

#include <stdio.h>
#include "areaArith.h"
#include "roundArith.h"

int main()
{
	printf("삼각형 넓이 (밑변 10, 높이 5) : %g \n"
			, TriangleArea(10, 5));
	printf("원 넓이 (반지름 : 10) : %g \n"
			, CircleArea(5));
	printf("직사각형 둘레(밑변 2.6, 높이 : 5.5) : %g \n"
			, RectangleRound(2.6, 5.5));
	printf("정사각형 둘레(변 길이 : 7) : %g \n"
			, SquareRound(3));
}
#define PI 3.1415
double Add(double num1, double num2);
double Min(double num1, double num2);
double Mul(double num1, double num2);
double Div(double num1, double num2);
double Add(double num1, double num2)
{
	return num1 + num2;
}

double Min(double num1, double num2)
{
	return num1 - num2;	
}

double Mul(double num1, double num2)
{
	return num1 * num2;
}

double Div(double num1, double num2)
{
	return num1 / num2;
}
double TriangleArea(double base, double height);
double CircleArea(double rad);
#include "basicArith.h"

double TriangleArea(double base, double height)
{
	return Div(Mul(base, height), 2);
}

double CircleArea(double rad)
{
	return Mul(Mul(rad, rad), PI);
}
double RectangleRound(double base, double height);
double SquareRound(double side);
#include "basicArith.h"

double RectangleRound(double base, double height)
{
	return Mul(Add(base, height), 2);
}

double SquareRound(double side)
{
	return Mul(side, 4);
}

4.2 몫, 나머지 연산(조건부 컴파일 이용 - 중복 인클루드 방지)

- main.c

- stdiv2.h

- intdiv4.h

- intdiv4.c

#include <stdio.h>
#include "stdiv2.h"
#include "intdiv4.h"

int main()
{
	Div val = IntDiv(5, 2);
	printf("몫 : %d \n", val.quotient);
	printf("나머지 : %d \n", val.remainder);
}
#ifndef __STDIV2_H__
#define __STDIV2_H__

typedef struct div
{
	int quotient;
	int remainder;
} Div;

#endif
#ifndef __INTDIV4_H__
#define __INTDIV4_H__

#include "stdiv2.h"

Div IntDiv(int num1, int num2);

#endif
#include "stdiv2.h"

Div IntDiv(int num1, int num2)
{
	Div dval;
	dval.quotient = num1/num2;
	dval.remainder= num1%num2;
	return dval;
}

1. 함수 포인터

 

#include <stdio.h>


void SimpleAdder(int n1, int n2)
{
	printf("%d + %d = %d \n", n1, n2, n1+n2);
}

void ShowString( char *str)
{
	printf("%s \n", str);
}

int main()
{
	char *str = "Function ptr";
	int num1=10, num2=20;
	
	void (*fptr1)(int, int) = SimpleAdder;
	void (*fptr2)(char *) = ShowString;
	
	fptr1(num1, num2);
	fptr2(str);
}

 

#include <stdio.h>

int WhoIsFirst(int age1, int age2, int (*cmp)(int n1, int n2))
{
	return cmp(age1, age2);
}

int OlderFirst(int age1, int age2)
{
	if (age1>age2)
		return age1;
	else if (age1 < age2)
		return age2;
	else
		return 0;		
}

int YoungerFirst(int age1, int age2)
{
	if (age1< age2)
		return age1;
	else if (age1 > age2)
		return age2;
	else
		return 0;	
}

int main()
{
	int age1=20;
	int age2=30;
	int first;
	
	printf("입장순서 1 \n");
	first = WhoIsFirst(age1, age2, OlderFirst);
	printf("%d세와 %d세 중 %d세가 먼저 입장!! \n\n", age1, age2, first);
	
	printf("입장순서 2 \n");
	first = WhoIsFirst(age1, age2, YoungerFirst);
	printf("%d세와 %d세 중 %d세가 먼저 입장! \n\n", age1, age2, first);
}

 

 

#include <stdio.h>

void SoSimpleFunc(void)
{
	printf("I'm so simple");
}

int main()
{
	int num=20;
	void *ptr; // void* 포인터 변수 함수  담을 수 있따. 
	
	ptr = &num;
	printf("%p \n", ptr);
	ptr = SoSimpleFunc;
	printf("%p \n", ptr);
	return 0;
}

 

#include <stdio.h>

int main(int argc, char *argv[])
{
	int i = 0;
	printf("전달된 문자열 수 : %d\n", argc);
	for(i=0; i<argc; i++)
		printf("%d 번째 문자열 : %s \n", i + 1, argv[i]);
}

 

 

 

2. 문자 스트림

#include <stdio.h>

int main()
{
	int ch;
	
	while(1)
	{
		ch = getchar();
		if ( ch ==EOF)
			break;
		putchar(ch);
	}
}
#include <stdio.h>

int main()
{
	char *str = "simple string";
	
	printf("1. puts test ----\n");
	puts(str);
	puts("so simple string");
	
	printf("2. fputs test ----\n");
	fputs(str, stdout);
	printf("\n");
	printf("so simple string", stdout);
	printf("\n");
	printf("3. end of main ----\n");
}
#include <stdio.h>

int main()
{
	char perID[7];
	char name[10];
	
	fputs("주민번호 앞 6자리 입력 : ", stdout);
	fgets(perID, sizeof(perID), stdin);
	
	//6자리 입력시 fgets는 \n까지 읽고, \n이 남음
	// -> 나머지 fgets에서도 버퍼에 남은 \n에 바로 종료되어 입력 못함 
	fputs("이름 입력 : ", stdout);
	fgets(name, sizeof(name), stdin);
	
	printf("주민번호 : %s \n", perID);
	printf("이름 : %s \n", name);
}
#include <stdio.h>

void ClearLineFromReadBuffer()
{
	while(getchar() != '\n');	
} 

int main()
{
	char perID[7];
	char name[10];
	
	fputs("주민번호 앞 6자리 입력 : ", stdout);
	fgets(perID, sizeof(perID), stdin);
	//getchar()로 버퍼내 \n를 읽으면 무한루프 탈출!, 버퍼가 비워짐. 
	ClearLineFromReadBuffer();
	
	fputs("이름 입력 : ", stdout);
	fgets(name, sizeof(name), stdin);
	
	printf("주민번호 : %s \n", perID);
	printf("이름 : %s \n", name);
}

 

 

 

 

3. 구조체 1

#include <stdio.h>
#include <math.h>

struct point
{
	int xpos;
	int ypos;
};

int main()
{
	struct point pos1, pos2;
	double distance;
	
	fputs("point1 pos : ", stdout);
	scanf("%d %d", &pos1.xpos, &pos2.ypos);
	
	fputs("point2 pos : ", stdout);
	scanf("%d %d", &pos2.xpos, &pos2.ypos);
	
	distance = sqrt((double)((pos1.xpos - pos2.xpos) *
		(pos1.xpos - pos2.xpos) + (pos1.ypos-pos2.ypos) * (pos1.ypos-pos2.ypos)));
	
	printf("두 점의 거리는 %g 입니다~\n", distance);	
}

#include <stdio.h>
#include <string.h>

struct person
{
	char name[20];
	char phoneNum[20];
	int age;
};

int main()
{
	struct person man1, man2;
	strcpy(man1.name, "무야호");
	strcpy(man1.phoneNum, "010-1544-7676");
	man1.age = 23;
	
	printf("이름 입력 : ");
	scanf("%s", man2.name);
	printf("번호 입력 : ");
	scanf("%s", man2.phoneNum);
	printf("나이 입력 : ");
	scanf("%d", &(man2.age));
	
	printf("이름 : %s \n", man1.name);
	printf("번호 : %s \n", man1.phoneNum);
	printf("나이 : %d \n", man1.age);
	
	printf("이름 : %s \n", man2.name);
	printf("번호 : %s \n", man2.phoneNum);
	printf("나이 : %d \n", man2.age);
}

#include <stdio.h>

struct point
{
	int xpos;
	int ypos;
};

struct person
{
	char name[20];
	char phoneNum[20];
	int age;
};

int main(void)
{
	struct point pos = {10, 20};
	struct person man = {"이승기", "010-1234-5678", 21};
	printf("%d %d \n", pos.xpos, pos.ypos);
	printf("%s %s %d \n", man.name, man.phoneNum, man.age);	
}

#include <stdio.h>
//struct arr

struct point
{
	int xpos;
	int ypos;
};

int main()
{
	struct point arr[3];
	int i;
	
	for(i = 0; i < 3; i++)
	{
		printf("점의 좌표 입력 : ");
		scanf("%d %d", &arr[i].xpos, &arr[i].ypos);
	}
	
	for(i = 0; i < 3; i++)
		printf("[%d %d] ", arr[i].xpos, arr[i].ypos);
}

#include <stdio.h>
//init struct array

struct person
{
	char name[20];
	char phoneNum[20];
	int age;
};

int main()
{
	struct person arr[3] = {
		{"이승기", "010-1111-2222", 21},
		{"홍길동", "010-2222-3333",22},
		{"김길동", "010-3333-4444",33}
	};
	
	int i;
	for(i=0;i<3; i++)
		printf("%s %s %d \n", arr[i].name, arr[i].phoneNum, arr[i].age);
}

#include <stdio.h>

struct point
{
	int xpos;
	int ypos;
};

struct circle
{
	double radius;
	struct point * center;
};

int main(){
	struct point cen = {2, 7};
	double rad = 5.5;
	struct circle ring = {rad, &cen};
	printf("원의 반지름 : %g \n", ring.radius);
	printf("원의 중심 [%d, %d] \n", (ring.center) -> xpos, (ring.center) ->ypos);
}

#include <stdio.h>

struct point
{
	int xpos;
	int ypos;
	struct point *ptr;
};

int main(void)
{
	struct point pos1 = {1, 1};
	struct point pos2 = {2, 2};
	struct point pos3 = {3, 3};
	
	pos1.ptr = &pos2;
	pos2.ptr = &pos3;
	pos3.ptr = &pos1;
	
	printf("점의 연결 관계.. \n");
	printf("[%d, %d]와 [%d, %d] 연결 \n",
		pos1.xpos, pos1.ypos, pos1.ptr->xpos, pos1.ptr->ypos);
	printf("[%d, %d]와 [%d, %d] 연결 \n",
		pos2.xpos, pos2.ypos, pos2.ptr->xpos, pos2.ptr->ypos);
	printf("[%d, %d]와 [%d, %d] 연결 \n",
		pos3.xpos, pos3.ypos, pos3.ptr->xpos, pos3.ptr->ypos);
	
}

 

 

 

 

+ Recent posts