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. 개요 (0) | 2022.07.21 |
---|---|
MINT64OS - 1. 시작 및 개발환경 구축 삽질기 (0) | 2022.07.04 |
프로그래밍연습 - 1.[C] 함수 포인터, 문자스트림, 구조체 1 (0) | 2022.06.21 |
리눅스 프로그래밍 4 - 메모리, 프로세스, 시그널, 환경, 로그인 (0) | 2022.06.01 |
리눅스 프로그래밍 3 - grep 구현, 리눅스 디렉터리구조, 파일시스템관련 명령어 구현 (0) | 2022.05.31 |