Recent Posts
printf("ho_tari\n");
C언어(6) 본문
2025.02.25
함수포인터
메모리 동적 할당
사용자 정의 자료형
파일 입출력
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(int argc, char** argv) {
int i;
/*for (i = 0; i < argc; i++)
{
printf("%s\n", argv[i]);
}*/
/*while (*argv != NULL)
{
printf("%s\n", *argv);
argv++;
}*/
app();
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
/*
void app() {
int** matrix;
// 4개의 행 공간의 할당
matrix = (int**)malloc(4 * sizeof(int*));
// 5개의 열 공간의 할당
for (int i = 0; i < 4; i++)
{
matrix[i] = (int*)malloc(5 * sizeof(int));
}
// 값 대입
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 5; x++)
{
matrix[y][x] = (y + 1) * 10 + (x + 1);
}
}
// 값 인쇄
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 5; x++)
{
printf("%5d", matrix[y][x]);
}
printf("\n");
}
// 공간 반환
for (int y = 0; y < 4; y++)
{
free(matrix[y]);
}
free(matrix);
}
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
// 구조체 선언 (명시적)
struct student
{
int num;
double grade;
};
// 구조체 선언 (암시적)
typedef struct{
int num;
double grade;
} student2_t;
void app() {
struct student s1; // 구조체를 사용한 변수 선언
student2_t s2;
s1.num = 2;
s1.grade = 2.7;
printf("학번 : %d\n", s1.num);
printf("학점 : %.1lf\n", s1.grade);
return 0;
}
*/
/*
struct profile
{
char name[20];
int age;
double height;
char* intro;
};
void app() {
struct profile yuni;
strcpy(yuni.name, "서하윤");
yuni.age = 17;
yuni.height = 164.5;
yuni.intro = (char*)malloc(80);
printf("자기소개 : ");
gets(yuni.intro);
printf("이름 : %s\n", yuni.name);
printf("나이 : %d\n", yuni.age);
printf("키 : %.1lf\n", yuni.height);
printf("자기소개 : %s\n", yuni.intro);
free(yuni.intro);
return 0;
}*/
/*
struct profile
{
int age;
double height;
};
struct student
{
struct profile pf;
int id;
double grade;
};
void app() {
struct student yuni;
yuni.pf.age = 17;
yuni.pf.height = 164.5;
yuni.id = 315;
yuni.grade = 4.3;
printf("나이 : %d\n", yuni.pf.age);
printf("키 : %.1lf\n", yuni.pf.height);
printf("학번 : %d\n", yuni.id);
printf("학점 : %.1lf\n", yuni.grade);
return 0;
}*/
/*
struct student
{
int id;
char name[20];
double grade;
};
void app() {
struct student s1 = { 315, "홍길동", 2.4 },
s2 = { 316, "이순신", 3.7 },
s3 = { 317, "세종대왕", 4.4 };
struct student max;
max = s1;
if (s2.grade > max.grade) max = s2;
if (s3.grade > max.grade) max = s3;
printf("학번 : %d\n", max.id);
printf("이름 : %s\n", max.name);
printf("학점 : %.1lf\n", max.grade);
return 0;
}*/
/*
struct student
{
int id;
char name[20];
double grade;
}s1 = { 315, "홍길동", 2.4 },
s2 = { 316, "이순신", 3.7 },
s3 = { 317, "세종대왕", 4.4 };
void app() {
struct student max;
max = s1;
if (s2.grade > max.grade) max = s2;
if (s3.grade > max.grade) max = s3;
printf("학번 : %d\n", max.id);
printf("이름 : %s\n", max.name);
printf("학점 : %.1lf\n", max.grade);
return 0;
}*/
/*
struct vision
{
double left;
double right;
};
struct vision exchange(struct vision robot);
void app() {
struct vision robot;
printf("시력 입력 : ");
scanf("%lf%lf", &(robot.left), &(robot.right));
robot = exchange(robot);
printf("바뀐 시력 : %.1lf %.1lf\n", robot.left, robot.right);
return 0;
}
struct vision exchange(struct vision robot)
{
double temp;
temp = robot.left;
robot.left = robot.right;
robot.right = temp;
return robot;
}*/
/*
struct book
{
char title[20];
char author[20];
int page;
double price;
};*/
/*
struct cracker
{
int price;
int calories;
};
void app() {
struct cracker c;
printf("바사삭의 가격과 열량을 입력하세요 : ");
scanf("%d %d", &c.price, &c.calories);
printf("바사삭의 가격 : %d원\n", c.price);
printf("바사삭의 열량 : %dkcal\n", c.calories);
return 0;
}*/
/*
struct score
{
int kor;
int eng;
int math;
};
void app() {
struct score yuni = { 90, 80, 70 };
struct score* ps = &yuni;
printf("국어 : %d\n", (*ps).kor);
printf("영어 : %d\n", ps->eng);
printf("수학 : %d\n", ps->math);
return 0;
}*/
/*
struct address
{
char name[20];
int age;
char tel[20];
char addr[80];
};
void app() {
struct address list[5] = {
{"홍길동", 23, "111-1111", "울릉도 독도"},
{"이순신", 35, "222-2222", "서울 건천동"},
{"장보고", 19, "333-3333", "완도 청해진"},
{"유관순", 15, "444-4444", "충남 천안"},
{"안중근", 45, "555-5555", "황해도 해주"}
};
struct address* list;
// list = malloc(5 * sizeof(struct address));
int i;
for (i = 0; i < 5; i++)
{
printf("%10s%5d%15s%20s\n", list[i].name, list[i].age, list[i].tel, list[i].addr);
}
return 0;
}
*/
/*
struct address
{
char name[20];
int age;
char tel[20];
char addr[80];
};
void print_list(struct address* lp);
void app() {
struct address list[5] = {
{"홍길동", 23, "111-1111", "울릉도 독도"},
{"이순신", 35, "222-2222", "서울 건천동"},
{"장보고", 19, "333-3333", "완도 청해진"},
{"유관순", 15, "444-4444", "충남 천안"},
{"안중근", 45, "555-5555", "황해도 해주"}
};
print_list(list);
return 0;
}
void print_list(struct address* lp)
{
int i;
for (i = 0; i < 5; i++)
{
printf("%10s%5d%15s%20s\n", (lp + i)->name, (lp + i)->age, (lp + i)->tel, (lp + i)->addr);
}
}*/
/*
struct list
{
int num;
struct list* next;
};
void app() {
struct list a = { 10, 0 }, b = { 20, 0 }, c = { 30, 0 };
struct list* head = &a, * current;
a.next = &b;
b.next = &c;
printf("head->num : %d\n", head->num);
printf("head->next->num : %d\n", head->next->num);
printf("list all : ");
current = head;
while (current != NULL)
{
printf("%d ", current->num);
current = current->next;
}
printf("\n");
struct list bc = { 25, 0 };
bc.next = b.next;
b.next = &bc;
printf("after added value : ");
current = head;
while (current != NULL)
{
printf("%d ", current->num);
current = current->next;
}
printf("\n");
a.next = b.next;
printf("after delete value : ");
current = head;
while (current != NULL)
{
printf("%d ", current->num);
current = current->next;
}
printf("\n");
return 0;
}*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
/*
union student {
int num;
short num2;
unsigned char num3;
double grade;
};
void app() {
union student s1 = { 0x123456 };
printf("num : %x\n", s1.num);
printf("grade : %.1lf\n", s1.grade);
printf("num2 : %x\n", s1.num2);
printf("num3 : %x\n", s1.num3);
return 0;
}*/
/*
enum season
{
SPRING, // 0
SUMMER, // 1
FALL, // 2
WINTER // 3
};
void app() {
enum season ss;
char* pc = NULL;
ss = 0;
switch (ss)
{
case SPRING: pc = "inline"; break;
case SUMMER: pc = "swimming"; break;
case FALL: pc = "trip"; break;
case WINTER: pc = "skiing"; break;
}
printf("나의 레저 활동 : %s\n", pc);
}
*/
/*
enum season
{
SPRING = 100, // 100
SUMMER, // 101
FALL, // 102
WINTER // 103
};
void app() {
enum season ss;
char* pc = NULL;
ss = 102;
switch (ss)
{
case SPRING: pc = "inline"; break;
case SUMMER: pc = "swimming"; break;
case FALL: pc = "trip"; break;
case WINTER: pc = "skiing"; break;
}
printf("나의 레저 활동 : %s\n", pc);
}
*/
/*
struct student
{
int num;
double grade;
};
typedef struct student Student;
void print_data(Student* ps);
void app() {
Student s1 = { 315, 4.2 };
print_data(&s1);
return 0;
}
void print_data(Student* ps) {
printf("학번 : %d\n", ps->num);
printf("학점 : %.1lf\n", ps->grade);
}*/
/*
struct marriage
{
char name[20];
int age;
char gender;
double height;
};
void app() {
struct marriage m1 = { "Andy", 22, 'm', 187.5};
struct marriage* mp = &m1;
printf("이름 : %s\n", mp->name);
printf("나이 : %d\n", (*mp).age);
printf("성별 : %c\n", mp->gender);
printf("키 : %.1lf\n", mp->height);
}*/
/*
typedef struct train Train;
struct train
{
int seats;
Train* next;
};
void app() {
Train* head = NULL, * tail = NULL;
int i;
// 연결고리 생성
for (i = 0; i < 5; i++)
{
if (head == NULL)
{
head = tail = (Train*)malloc(sizeof(Train));
}
else
{
tail->next = (Train*)malloc(sizeof(Train));
tail = tail->next;
}
}
// 연결고리에 값을 대입
head->seats = 1;
head->next->seats = 2;
head->next->next->seats = 3;
head->next->next->next->seats = 4;
head->next->next->next->next->seats = 5;
// 연결고리 값을 인쇄
Train *current;
current = head;
while (current != NULL)
{
printf("%d ", current->seats);
current = current->next;
}
}
*/
/*
typedef enum { CYAN, MAGENTA, YELLOW = 5, BLACK } COLOR;
typedef enum { UP, DOWN, LEFT, RIGHT } ARROW;
void app() {
COLOR my_color = YELLOW, c;
ARROW direction = UP;
for (c = CYAN; c <= BLACK; c++)
{
direction++;
direction = direction % 4;
if (c == my_color) break;
}
switch (direction)
{
case UP: printf("현재 방향 : 위"); break;
case DOWN: printf("현재 방향 : 아래"); break;
case LEFT: printf("현재 방향 : 왼쪽"); break;
case RIGHT: printf("현재 방향 : 오른쪽"); break;
}
return 0;
}*/
/*
void app() {
FILE* fp;
fp = fopen("c:\\Users\\microsoft\\source\\repos\\structure\\x64\\Debug\\a.txt", "r");
if (fp == NULL)
{
printf("파일이 열리지 않았습니다.\n");
return;
}
printf("파일이 열렸습니다.\n");
// 내용 읽기
for (int i = 0; i < 11; i++)
{
char ch = fgetc(fp);
printf("%c", ch);
}
fclose(fp);
return 0;
}
*/
void app() {
FILE* fp;
fp = fopen("c:\\Users\\microsoft\\source\\repos\\structure\\x64\\Debug\\a.txt", "r");
if (fp == NULL)
{
printf("파일이 열리지 않았습니다.\n");
return;
}
printf("파일이 열렸습니다.\n");
// 내용 읽기
//while (1)
//{
// char ch = fgetc(fp);
// if (ch == EOF) break; // End Of File
// printf("%c", ch);
//}
char line[80];
int count;
fread(line, 1, 80, fp);
printf("%s\n", line);
fclose(fp);
return 0;
}
0225.c
0.00MB
structure.c
0.00MB
file.c
0.00MB
typedef.c
0.01MB