책 이름 받기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct book { // 사용자 정의 자료형
char title[80]; // 상수
char* author; // 포인터 변
int page;
int price;
};
void set_book(struct book* b, const char* t, const char* a, int p, int pr) {
strcpy(b->title, t);
b->author = a;
b->page = p;
b->price = pr;
}
int main() {
//struct book mybook = { "C 프로그래밍", "데니스", 180, 18000 };
struct book mybook;
set_book(&mybook, "C 프로그래밍", "데니스", 180, 18000);
//strcpy(mybook.title, "C 프로그래밍");
//mybook.author = "데니스"; // 포인터 변수면 주소에 주소를 대입하는 것이기 때문에 가능
//mybook.page = 180;
//mybook.price = 18000;
printf("도서명 : %s\\n", mybook.title);
printf("저자 : %s\\n", mybook.author);
printf("페이지수 : %d\\n", mybook.page);
printf("가격 : %d\\n", mybook.price);
return 0;
}
크래커 정보 설정하기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct cracker {
int price;
int kcal;
};
int main() {
struct cracker c;
printf("바사삭 가격과 열량을 입력하세요: ");
scanf("%d %d", &c.price, &c.kcal);
printf("바사삭 가격: %d원\\n", c.price);
printf("바사삭 열량: %dkcal", c.kcal);
return 0;
}
명령행 인수 코드
argc: 인수의 개수
argv: 인수 목록. 각 인수를 문자열로 담고 있는 포인터 배열이다.
wsl 에서 ./파일명 hello world 123 이런식으로 하면
#include <stdio.h>
int main(int argc, char** argv) {
for(int i=0; i<argc; i++) {
printf("%s\\n", argv[i]);
}
return 0;
}
구조체로 국어, 영어, 수학 성적 표출하기
#include <stdio.h>
struct score {
int kor;
int eng;
int math;
};
int main()
{
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;
}
구조체 배열을 초기화하고 출력하기
#include <stdio.h>
struct address { // 구조체도 배열이 가능하다
char name[20];
int age;
char tel[20];
char addr[80];
};
int main() {
struct address list[5] = {
{"홍길동", 21, "1111-1111", "울릉도 독도"},
{"이순신", 35, "2222-2222", "서울 건천동"},
{"장보고", 19, "3333-3333", "완도 청해진"},
{"유관순", 19, "4444-4444", "충남 천안"},
{"안중근", 21, "5555-5555", "황해도 해주"},
};
for (int 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;
}
연결리스트
#include <stdio.h>
// 자기 참조 구초제
struct list {
int num;
struct list* next; // 자기 참조 멤버
};
int main() {
// linked list
// struct list a = {10, NULL}, b = {20, NULL}, c = {30, NULL};
struct list a = {10, NULL};
struct list b = {20, NULL};
struct list c = {30, NULL};
// struct list* head, *current;
struct list* head;
struct list* current;
head = &a;
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; // current의 다음을 따라가기
// current++;
}
return 0;
}
공용체 (메모리 절약)
모든 멤버가 같은 메모리 공간을 가진다 (구조체는 모든 멤버가 개별적인 공간을 가짐)
크기는 가장 큰 멤버의 크기이고 (구조체는 모든 멤버 크기의 합)
한번에 오직 하나의 멤버만 유효함
#include <stdio.h>
union student {
int num; // 4바이트 정수
double grade; // 8바이트 정수
};
int main() {
union student s1 = {315};
printf("학번: %d\\n", s1.num);
s1.grade = 4.4;
printf("학점: %.1lf\\n", s1.grade);
printf("학번: %d\\n", s1.num);
return 0;
}
열거형
성능적으로 배열보다 약간 빠름
enum season {SPRING, SUMMER, FALL, WINTER} → 1, 2, 3, 4
enum season {SPRING = 5, SUMMER, FALL=10, WINTER} → 5,6,10,11
#include <stdio.h>
#include <string.h>
enum season {SPRING, SUMMER=5, FALL=10, WINTER};
int main() {
char input_text[10];
enum season ss;
char* pc = NULL;
printf("계절을 입력하세요(SPRING, SUMMER, FALL, WINTER): ");
scanf("%s", &input_text); // & 연산자를 사용하여 변수의 주소를 전달
// ss = SPRING;
if (strcmp(input_text, "SPRING") == 0) {
ss = SPRING;
} else if (strcmp(input_text, "SUMMER") == 0) {
ss = SUMMER;
} else if (strcmp(input_text, "FALL") == 0) {
ss = FALL;
} else {
ss = WINTER;
}
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);
return 0;
}
typedef를 사용한 형 재정의
typedef struct student Student;
#include <stdio.h>
struct student {
int num;
double grade;
};
typedef struct student Student; // 새로운 자료형 제공
void print_data(Student *ps);
int main() {
Student s1 = {315, 4.2};
printf("학번을 입력해주세요: ");
scanf("%d", &s1.num);
printf("학점을 입력해주세요: ");
scanf("%lf", &s1.grade);
print_data(&s1);
return 0;
}
void print_data(Student *ps) {
printf("학번: %d\\n", ps->num);
printf("학점: %.1lf\\n", ps->grade);
}
완전수 구하기
#include <stdio.h>
int main() {
int num, sum = 0;
printf("숫자를 입력해주세요: ");
scanf("%d", &num);
for (int i=1; i<= (num/2); i++) {
if (num % i == 0) {
sum += i;
}
}
if (sum == num) {
printf("완전수입니다\\n");
} else {
printf("완전수가 아닙니다\\n");
}
return 0;
}
소수 구하기
#include <stdio.h>
int main() {
int i=1;
for (int i=2; i<=100; i++) {
int count = 1;
for (int j=2; j<=(i/2); j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 1) {
printf("%d ", i);
}
}
return 0;
}
wsl 실습
wsl -- list --online # 페도라가 레드햇 중에는 가장 빨리 업데이트됨
wsl --install -d Ubuntu
wsl --list --verbose
wsl -l -v
wsl -v -l # 버전이 나옴
clear
wsl -l -v
wsl -d Ubuntu
cd ~/ # 집으로 가기
pwd
gcc
g++
make
sudo apt update
sudo apt install # 리눅스 우분투에서 어플 다운받는 명령 -> 설치하기 위해서는 저장소를 알아야함
sudo apt install build-essential -y
ls
cd work # mkdir work 로 만든 것
ls -l
man man
man ls
ls -l -a
cd basic
nano add.c
#include <stdio.h>
int main() {
int a, b;
printf("두 정수를 입력하세요: ");
scanf("%d %d", &a, &b);
printf("두 정수의 합은: %d\\n", a+b);
return 0;
}
gcc -o add.exe add.c # 실행파일 생성
./add.exe # 실행
코딩
ls -l
rm a.out
rm *.o
rm add.exe app01 hello
mkdir AAA
rm AAA
rm -rf AAA # recursive false 또는 rmdir
vi Ex16-6.c
//명령행 인수 코드
#include <stdio.h>
int main(int argc, char** argv) {
for(int i=0; i<argc; i++) {
printf("%s\\n", argv[i]);
}
return 0;
}
clear
ls
gcc -o 16-6 Ex16-6.c
ls -l # r, x 권한이 있음 / 만든사람 / 계정그룹 / byte / 만들어진 날짜 / 파일명
./16-6 first second # ./16 까지 치고 탭
exit
exit
작업영역에 폴더 추가

'c' 카테고리의 다른 글
| c 언어 문제 (0) | 2025.11.14 |
|---|---|
| 파일처리 (0) | 2025.11.13 |
| 배열, 포인터와 구조 (0) | 2025.11.11 |
| 이중포인터 N차원 배열 (1) | 2025.11.10 |
| 문자열 (0) | 2025.11.07 |