본문 바로가기
개발자

두들낙서 C/C++ 44강~45강 상수 만들기 const, 매크로, enum, 매크로 자세히 알아보기

by ⁖⁝‥ 2022. 8. 12.

44강. 상수를 지정하는 세가지 방법

상수 : 변하지 않는 수 

변수 : 변할 수 있는 수

상수를 지정하는 방법은 세가지가 있다. const, 매크로, enum

1) const

int main() {
	float a = 3.14159;
	float b = 3.14159 * 2;
}

3.14159를 매번 치기 귀찮으니까 

int main() {
	float pi = 3.14159;
	float a = pi;
	float b = pi * 2;
}

π 입력하려면 ㅎ 누르고 한자 누르면 된다.

int main() {
	float pi = 3.14159;
	float a = pi;
	float b = pi *= 2;

	printf("π= %.2f\n", pi);
}

결과

파이값이 변해버렸다. 그러면 안됨. 왜냐면 파이는 상수니까

float pi 앞에 const 를 붙여준다.

그러면 pi는 상수가 됨. 그래서 float b = pi *= 2; 는 오류가 나고 실행이 안된다.

상수는 PI 이렇게 대문자로 쓰는 경향이 있다.

상수로 선언을 한 뒤에는 값을 바꿀 수 없다.

그러면 PI 값은 메모리상에 저장이 될까?

int main() {
	const float PI = 3.14159;
	float a = PI;
	
	printf("π= %.2f\n", PI);
	printf("&PI = %d\n", &PI);
}

결과 : π = 3.14  &PI = 2389234

메모리상에 직접 저장이 됨을 알수 있음.

 

2) 매크로

 

3.14159 라는 숫자 자체를 별칭을 정해줄 수도 있다.

맨 위에 #define PI 3.14159 를 적으면 된다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define PI 3.14159

int main() {
	float a = PI;
	
	printf("π= %.2f\n", PI);
	printf("&PI = %d\n", &PI);
}

그래서 &PI 는 오류가 난다. 왜냐하면 숫자는 메모리에 저장되지 않으니까 당연히 주소값이 없다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define PI 3.14159

int main() {
	float a = PI;
	
	printf("π= %.2f\n", PI);
}

여기서 #define을 매크로 라고 부른다.

 

3) enum

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

const int GAMESTATE_MAINMENU = 0;
const int GAMESTATE_PLAYING = 1;
const int GAMESTATE_PAUSED = 2;
const int GAMESTATE_GAMEOVER = 3;


int main() {
	int currState = GAMESTATE_MAINMENU;
	while (true) {
		switch (currState) {
		case GAMESTATE_MAINMENU:
			break;
		case GAMESTATE_PLAYING:
			break;
		case GAMESTATE_PAUSED:
			break;
		case GAMESTATE_GAMEOVER:
			break;
		}
	}
}

enum 을 사용

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

enum EGameState {
	GAMESTATE_MAINMENU = 0,
	GAMESTATE_PLAYING = 1,
	GAMESTATE_PAUSED = 2,
	GAMESTATE_GAMEOVER = 3,
};

int main() {
	int currState = GAMESTATE_MAINMENU;
	while (true) {
		switch (currState) {
		case GAMESTATE_MAINMENU:
			break;
		case GAMESTATE_PLAYING:
			break;
		case GAMESTATE_PAUSED:
			break;
		case GAMESTATE_GAMEOVER:
			break;
		}
	}
}

enum 안에 숫자 생략해도됨.

enum EGameState {
	GAMESTATE_MAINMENU,
	GAMESTATE_PLAYING,
	GAMESTATE_PAUSED,
	GAMESTATE_GAMEOVER,
};

int main() {
	int currState = GAMESTATE_MAINMENU;
	while (true) {
		switch (currState) {
		case GAMESTATE_MAINMENU:
			break;
		case GAMESTATE_PLAYING:
			break;
		case GAMESTATE_PAUSED:
			break;
		case GAMESTATE_GAMEOVER:
			break;
		}
	}
}

45강. 매크로 자세히 알아보기

1) 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define PRINT_HELLO printf("Hello");

int main() {
	PRINT_HELLO
}

 

띄어쓰기가 있어도 됨

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define PRINT_HELLO printf("Hello, World!");

int main() {
	PRINT_HELLO
}

2) 매개변수가 있는 매크로 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int square(int a) {
	return a * a;
}
int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",square(a));
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SQUARE(X) X*X

int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",SQUARE(a));
}
int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",100/ SQUARE(a));
}

여기서 5를 입력하면 결과값은 100이 나온다.

SQUARE(a) 는 a*a 니까 

100/a*a 는 100에서 a 나눗셈을 먼저 계산이 되므로 100/5 = 20 

20*5하니까 100이 나온다.

이걸 고치려면 

int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",100/ (SQUARE(a)));
}

이렇게 해도 되고 아예 define에서 (X*X) 에 괄호를 쳐도 된다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SQUARE(X) (X*X)

int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",100/ SQUARE(a));
}

예제2)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SQUARE(X) (X*X)

int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",SQUARE(a-1));
}

여기서 6을 입력하면 25가 아니라 -1이 출력된다.

왜냐하면 (X*X) 니까 (a-1 * a-1) 이 되어서 1*a 가 먼저 계산되어 

a-a-1 해서 -1 이 나오게 된다.

그러므로 매개변수에다가 ((X)*(X)) 이렇게 괄호를 각각 쳐주고 맨 양 끝에도 쳐줘야 한다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SQUARE(X) ((X)*(X))

int main() {
	int a;
	scanf("%d", &a);
	printf("%d\n",SQUARE(a-1));
}

예제 3) 두 숫자중에 큰 숫자 고르기 

int max(int a, int b) {
	if (a > b) return a;
	return b;
}

이렇게 해도 되고 삼항 연산자 활용해서

int max(int a, int b) {
		return (a > b) ? a : b;
}

이 식을 매크로로 올리면

#define MAX(A,B) (((A)>(B))? (A) : (B))

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX(A,B) (((A)>(B))? (A) : (B))

int main() {
	int a = 5, b = 10;
	printf("%d\n", MAX(a, b));
}

결과 : 10

예제 4) for문을 매크로로 바꾸기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define FOR(I,S,E) for(int I= S;I<=E;I++)

int main() {
	FOR(i,1,10) {
		printf("%d ", i);
	}
}

 

예제 5) 무한루프 만들기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define FOR(I,S,E) for(int I= S;I<=E;I++)
#define LOOP while(true)

int main() {
	FOR(i,1,10) {
		printf("%d ", i);
	}
	LOOP{
		printf("*");
	}
}

 

 

반응형

댓글