Compare commits

..

10 Commits

Author SHA1 Message Date
fd6449c05c bit flag for structure 2025-04-25 12:20:09 +09:00
3a1d21f8cf bit flag 2025-04-25 11:50:10 +09:00
ede38f800e bit calculation 2025-04-25 11:30:12 +09:00
7d7ac8ec82 function pointer 2025-04-24 17:17:38 +09:00
b554018319 pointer arithmetics 2025-04-24 17:06:46 +09:00
ccba676af4 recursion 2025-04-24 16:17:53 +09:00
799b2e2588 array and pointers 2025-04-24 16:14:57 +09:00
2e9eb14783 function arguments by reference 2025-04-24 15:38:31 +09:00
d6b2615c7e structure 2025-04-24 15:31:09 +09:00
793beddc19 lc_pointer 2025-04-24 14:20:40 +09:00
11 changed files with 327 additions and 2 deletions

13
.vscode/tasks.json vendored
View File

@@ -70,13 +70,22 @@
"type": "cppbuild",
"label": "C/C++: gcc.exe 활성 파일 빌드",
"command": "C:/mingw64/bin/gcc.exe",
// main 다수 (테스트)
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}\\*.c",
"${file}",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
],
// // main 단일 프로젝트
// "args": [
// "-fdiagnostics-color=always",
// "-g",
// "${fileDirname}\\*.c",
// "-o",
// "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
// ],
"options": {
"cwd": "C:/mingw64/bin"
},

37
practice/g_bitCalc.c Normal file
View File

@@ -0,0 +1,37 @@
#include <stdio.h>
int main()
{
int a = 6; // 0110
int b = 3; // 0011
int result;
printf("result size: %d\n", sizeof(int));
// AND(&)
result = a & b; // 0010 => 2
printf("[AND] %d : %x\n", result, result);
// OR(|)
result = a | b; // 0111 => 7
printf("[OR] %d : %x\n", result, result);
// XOR(^): 두 비트가 다르면 1 반환
result = a ^ b; // 0101 => 5
printf("[XOR] %d : %x\n", result, result);
// NOT(~): 비트 반전
result = ~a; // 1111 1001 => -7
printf("[NOT] %d : %x\n", result, result);
// Left Shift(<<): 비트 시프트
result = a << 1; // 1100 => 12
printf("[<<] %d : %x\n", result, result);
// Right Shift(<<): 비트 시프트
result = a >> 1; // 0011 => 3
printf("[>>] %d : %x\n", result, result);
return 0;
}

30
practice/g_bitFlag.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#define FLAG_A 0x01 // 0000 0001
#define FLAG_B 0x02 // 0000 0010
#define FLAG_C 0x04 // 0000 0100
int main()
{
unsigned char flags = 0;
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X'));
// A 활성화
flags |= FLAG_A;
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X'));
// C 활성화
flags |= FLAG_C;
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X'));
// B 활성화
flags |= FLAG_B;
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X'));
// B 비활성화
flags &= ~FLAG_B;
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X'));
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <stdio.h>
typedef struct
{
unsigned int data : 10; // 10비트, 0~1023
unsigned int error_code : 6; // 6비트 플래그
} DataPacket;
int main()
{
DataPacket packet;
packet.data = 108;
packet.error_code = 0;
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
// 에러 감지
// 0번째 비트 켜기
packet.error_code |= 1;
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
// 3번째 비트 켜기
packet.error_code |= (1 << 3);
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
// 5번째 비트 켜기
packet.error_code |= (1 << 5);
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
return 0;
}

View File

@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
char vowels[] = { 'A', 'E', 'I', 'O', 'U' };
char* pvowels = vowels;
printf("Point the addresses\n");
for (int i = 0; i < 5; i++)
{
printf("&vowels[%d]: %p, pvowels + %d: %p, vowels + %d: %p\n", i, &vowels[i], i, pvowels + i, i, vowels + i);
}
printf("\nPoint the values\n");
for (int i = 0; i < 5; i++)
{
printf("vowels[%d]: %c, *(pvowels + %d): %c, *(vowels + %d): %c\n", i, vowels[i], i, *(pvowels + i), i, *(vowels + i));
}
// Dynamic malloc for array
int n = 4;
char* parr = (char*)malloc(n * sizeof(char));
parr[0] = 'L';
parr[1] = 'o';
parr[2] = 'V';
parr[3] = 'e';
for (int i = 0; i < n; i++)
{
printf("%c", parr[i]);
}
printf("\n");
free(parr);
return 0;
}

View File

@@ -0,0 +1,51 @@
#include <stdio.h>
void addone(int n)
{
n++;
}
void addoneByRef(int* n)
{
(*n)++;
}
typedef struct
{
int x;
int y;
} Point;
void move1(Point* p)
{
(*p).x++;
(*p).y++;
}
// 약어
void move2(Point* p)
{
p->x++;
p->y++;
}
int main()
{
int n = 0;
printf("Step0: %d\n", n);
addone(n);
printf("Step2: %d\n", n);
addoneByRef(&n);
printf("Step2: %d\n", n);
printf("\n");
Point pt = { 1, 1 };
printf("Step0 pt.x: %d, pt.y: %d\n", pt.x, pt.y);
move1(&pt);
printf("Step1 pt.x: %d, pt.y: %d\n", pt.x, pt.y);
move2(&pt);
printf("Step2 pt.x: %d, pt.y: %d\n", pt.x, pt.y);
return 0;
}

View File

@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
void someFunction(int arg)
{
printf("This is someFunction being called and arg is: %d\n", arg);
printf("Now, leaving the funciton!\n");
}
int compare(const void* left, const void* right)
{
return (*(int*)right - *(int*)left);
}
int main()
{
void (*pf)(int);
pf = &someFunction;
printf("We are about to call someFunction() using a pointer!\n");
(pf)(5);
printf("Wow that was cool!");
int (*cmp)(const void*, const void*);
cmp = &compare;
int iarray[] = {1,2,3,4,7,5,6,9,8};
qsort(iarray, sizeof(iarray) / sizeof(*iarray), sizeof(*iarray), cmp);
int c = 0;
while (c < sizeof(iarray) / sizeof(*iarray))
{
printf("%d\t", iarray[c]);
c++;
}
return 0;
}

View File

@@ -0,0 +1,28 @@
#include <stdio.h>
int main()
{
int intarray[5] = { 10, 20, 30, 40, 50 };
for (int i = 0; i < 5; i++)
{
printf("intarray[%d] has value %d - and address @ %x\n", i, intarray[i], &intarray[i]);
}
int* intpointer = &intarray[3];
printf("address %x - has value %d\n", intpointer, *intpointer);
intpointer++;
printf("address %x - has value %d\n", intpointer, *intpointer);
intpointer--;
printf("address %x - has value %d\n", intpointer, *intpointer);
intpointer -= 2;
printf("address %x - has value %d\n", intpointer, *intpointer);
intpointer += 2;
printf("address %x - has value %d\n", intpointer, *intpointer);
return 0;
}

14
practice/lc_pointers.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main()
{
int a = 1;
int * p = &a;
a += 1;
*p += 1;
printf("Value of a: %d\n", a);
return 0;
}

19
practice/lc_recursion.c Normal file
View File

@@ -0,0 +1,19 @@
#include <stdio.h>
unsigned int multiply(unsigned int x, unsigned int y)
{
if (x == 1)
return y;
if (x > 1)
return y + multiply(x - 1, y);
return 0;
}
int main()
{
printf("3 times 100 is %d", multiply(3, 100));
return 0;
}

30
practice/lc_structures.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
struct Point
{
int x;
int y;
};
typedef struct
{
char* brand; // 동적 할당을 위한 처리
int model;
} Vehicle;
int main()
{
struct Point pt;
pt.x = 10;
pt.y = 5;
Vehicle vc;
vc.brand = "Ford";
vc.model = 2025;
printf("pt.x: %d\n", pt.x);
printf("pt.y: %d\n", pt.y);
printf("\n");
printf("vc.brand: %s\n", vc.brand);
printf("vc.model: %d\n", vc.model);
}