ex20250602 - rlawjdaks/ex20250310_first GitHub Wiki

#include <stdio.h>
struct Point{
    int x;
    int y;
};
void PrintPoint(Point* p) {
	printf("(%d, %d)\n", p->x, p->y); //์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ p->x ์ •์˜๋ฅผ ์‚ฌ์šฉํ•  ๋–„ pt1.x
}
int main() {
	Point pt1 = { 1, 2 };
	PrintPoint(&pt1);
	printf("(%d, %d)\n", pt1.x, pt1.y);
}

#include <stdio.h>
struct Point {
	int x;
	int y;
};
void PrintPoint(Point* p) {
	printf("(%d, %d)\n", p->x, p->y); 
}
void SetPoint(Point* p, int x, int y) {
	p->x = x;
	p->y = y;
}
int main() {
	Point pt1 = { 1, 2 };

	PrintPoint(&pt1);
	SetPoint(&pt1, 4, 5);
	PrintPoint(&pt1);
	PrintPoint(&pt1);
}

#include <stdio.h>
struct Point {
	int x;
	int y;
};
void PrintPoint(Point* p) {
	printf("(%d, %d)\n", p->x, p->y);
}
void SetPoint(Point* p, int x, int y) {
	p->x = x;
	p->y = y;
}
void ResetPoint(Point* p) {
	p->x = 0;
	p->y = 0;
}
int main() {
	Point pt1 = { 1, 2 };

	PrintPoint(&pt1);
	SetPoint(&pt1, 4, 5);
	PrintPoint(&pt1);
	ResetPoint(&pt1); // 0,0
	PrintPoint(&pt1);
}

#include <stdio.h>
int main() {
	char s1[100] = "ABCD";
	const char* s2 = "ABCD";

	printf("string : %s\n", s1);
	printf("string : %s\n", s2);
}

#include <stdio.h>
int main() {
	char* s1 = NULL;
	const char* s2 = NULL; //const = ์ƒ์ˆ˜

	char c = 'A';
	s1 = &c;
	s2 = &c;

	*s1 = 'Z';
	// *s2 = 'Z'; const๋Š” ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅ, ์“ฐ๊ธฐ ์•ˆ๋จ 
	printf("%c %c %c\n", *s1, *s2, c);
}

#include <stdio.h>
int main() {
	char buf[10] = "ABC"; // R.W ์ฝ๊ธฐ ์“ฐ๊ธฐ 
	char* s1 = buf; // R.W ์ฝ๊ธฐ ์“ฐ๊ธฐ
	const char* s2 = buf; // R ์ฝ๊ธฐ 

	s1[1] = 'Z';
	s2[1] = 'Z'; // ์•ˆ๋จ
	printf("%s %s %s\n", s1, s2, buf);
}

#include <stdio.h>
int main() {
	char buf[100];

	printf("๋ฌธ์ž์—ด ์ž…๋ ฅ: ");
	gets_s(buf, 100);
	printf("string : %s\n", buf);
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	//strcpy(), strcat(), strcmp(), strlen()
	char buf[100];
	const char* s = "Hello!";

	strcpy(buf, s); // ๋ฌธ์ž์—ด์„ ๋ณต์‚ฌํ•œ๋Š” ๊ฒƒ ์ถœ๋ ฅํ•˜๋ฉด Hello!, Hello!
	//strcpy(&buf[0], s);
	printf("%s, %s\n", buf, s);
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	//strcpy(), strcat(), strcmp(), strlen()
	char buf[100];
	const char* s = "Hello!";

	strcpy(buf+1, s); //๋ฌธ์ž์—ด 0๋ง๊ณ  1๋ถ€ํ„ฐ ์‹œ์ž‘ 0์€ ๋น„์›Œ๋‘  ์ถœ๋ ฅํ•˜๋ฉด HHello!, Hello!
	buf[0] = 'H';
	printf("%s, %s\n", buf, s);
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	//strcpy(), strcat(), strcmp(), strlen()
	char buf[100] = "ABC";
	const char* s = "Hello!";

	strcat(buf, s); //buf์˜ ๋์— s๋ฅผ ๋ถ™์ด๋Š” ํ•จ์ˆ˜, ์ถœ๋ ฅํ•˜๋ฉด ABCHello, Hello
	printf("%s, %s\n", buf, s);
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	//strcpy(), strcat(), strcmp(), strlen()
	char buf[100] = "ABC";
	const char* s = "Hello!";

	strcat(buf, s);
	printf("len : %d\n", strlen(buf)); // ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค์คŒ ์ถœ๋ ฅํ•˜๋ฉด len : 9 len : 6
	printf("len : %d\n", strlen(s));
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	//strcpy(), strcat(), strcmp(), strlen()
	const char* s1 = "ABC";
	const char* s2 = "abc";

	printf("%d\n", strcmp(s1, s2)); // ๋‘ ๋ฌธ์ž์—ด์„ **์‚ฌ์ „ ์ˆœ์„œ(์•ŒํŒŒ๋ฒณ ์ˆœ์„œ)**๋กœ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜
0: ๋‘ ๋ฌธ์ž์—ด์ด ๊ฐ™์Œ
์–‘์ˆ˜ (>0): ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ๋” ํผ (๋‚˜์ค‘์— ์˜ด)
์Œ์ˆ˜ (<0): ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ๋” ์ž‘์Œ (๋จผ์ € ์˜ด)
	printf("%d\n", strcmp(s2, s1));
	printf("%d\n", strcmp("12345", "12345"));
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	while (1) {
		char buf[100];
		printf("๋ฌธ์ž์—ด ์ž…๋ ฅ :");
		gets_s(buf,100);
		//if (buf == "exit")->์•ˆ๋จ,  ์ฃผ์†Œ๋น„๊ต 
		if (buf[0] == "exit"[0] &&
			buf[1] == "exit"[1] &&
			buf[2] == "exit"[2] &&
			buf[3] == "exit"[3] &&
			buf[4] == "exit"[4] 
			)
			break;
		printf("string : %s\n",buf);
	}
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	while (1) {
		char buf[100];
		printf("๋ฌธ์ž์—ด ์ž…๋ ฅ :");
		gets_s(buf, 100);
		//if (buf == "exit")->์•ˆ๋จ,  ์ฃผ์†Œ๋น„๊ต 
		if (strcmp(buf, "exit") == 0)
			break;

		printf("string : %s\n", buf);
	}
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
	char* sarr[1000] = { 0 };
	int countStr = 0;

	while (1) {
		char buf[100];
		printf("๋ฌธ์ž์—ด ์ž…๋ ฅ :");
		gets_s(buf, 100);

		if (strcmp(buf, "exit") == 0)
			break;
		else {
			printf("string : %s\n", buf);
			sarr[countStr++] = buf;
		}
	}
	for (int i = 0; i < countStr; ++i)
		printf("[%d] : %s\n", i, sarr[i]);
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
	char* sarr[1000] = { 0 };
	int countStr = 0;

	while (1) {
		char buf[100];
		printf("๋ฌธ์ž์—ด ์ž…๋ ฅ :");
		gets_s(buf, 100);

		if (strcmp(buf, "exit") == 0)
			break;
		else {
			printf("string : %s\n", buf);
			char* s = (char*)malloc(strlen(buf) + 1);
			strcpy(s, buf);
			sarr[countStr++] = s;
		}
	}
	for (int i = 0; i < countStr; ++i)
		printf("[%d] : %s\n", i, sarr[i]);

	for (int i = 0; i < countStr; ++i)
		free(sarr[i]);
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
	char* sarr[1000] = { 0 };
	int countStr = 0;
	int run = 1;

	while (run) {
		char buf[100];
		printf("๋ฌธ์ž์—ด ์ž…๋ ฅ :");
		gets_s(buf, 100);

		if (strcmp(buf, "exit") == 0)
			run = 0;
		else {
			printf("string : %s\n", buf);
			char* s = (char*)malloc(strlen(buf) + 1);
			strcpy(s, buf);
			sarr[countStr++] = s;
		}
	}
	for (int i = 0; i < countStr; ++i)
		printf("[%d] : %s\n", i, sarr[i]);

	for (int i = 0; i < countStr; ++i)
		free(sarr[i]);
}

#include <stdio.h>
struct Person {
	char name[20];
	int age;
};
int main() {
	Person p1 = { " ํ™๊ธธ๋™ ", 30 };

	printf("name : %s, age : %d\n", p1.name, p1.age);
}

#include <stdio.h>
struct Person {
	//char name[20]; //0
	char* name; //0 => ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž์—ด์„ ์ €์žฅ x
	int age;
};

int main() {
	struct Person p1 = { " ํ™๊ธธ๋™ ", 30 };
	
	printf("name : %s, age : %d\n", p1.name, p1.age);
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ