전처리기
#define 전 처리기 지시문으로 프로그램에서 상수 값을 정의하는데 사용된다.
#pragma -> 컴파일 옵션 조절 키워드.
#pragma pack -> 메모리 패킹(펜딩) 처리.
#pragma once -> 헤더 중복 방지.
지시문 구문
#define macro_name value
기본 사용 예
#include<stdio.h>
#define PI 3.14 // 3.14 PI 정의
void main()
{
int r;
float area;
printf("Enter radius : ");
scanf("%d",&r);
area = PI * r * r;
printf("\nArea of circle is : %f",area);
}
Output :
Enter radius : 5
Area of circle is : 78.5
기본 사용 예
#include<stdio.h>
#define SIZE 5 // 5를 SIZE
void main()
{
int arr[SIZE];
for(int i=0;i<SIZE;i++)
{
printf("Enter any number : ");
scanf("%d",&arr[i]);
}
for(i=0;i<SIZE;i++)
printf("%d, ",arr[i]);
}
Output :
Enter any number : 78
Enter any number : 45
Enter any number : 12
Enter any number : 89
Enter any number : 56
78, 45, 12, 89, 56,
기본 사용 예
#include<stdio.h>
#define SQUARE(x) x*x // 가로 * 세로 = 넓이, 함수와 사용시 연산자 우선 순위 확인
void main()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
printf("\nThe square is : %d",SQUARE(num));
}
Output :
Enter any number : 5
The square is : 25
#include 전 처리기 지시문의 구문
#include "filename.h"
or
#include <filename.h>
# if- # else- # endif 전처리 지시문
조건이 true이면 #if와 #else 사이에 제공된 명령문이 실행.
조건이 거짓이면 #else와 #endif 사이에 주어진 명령문이 실행
#if(condition)
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif
# if- # else- # endif 전처리 지시문의 예
#include<stdio.h>
#define MAX 45
void main()
{
#if MAX > 40
printf("Yes, MAX is greater then 40.");
#else
printf("No, MAX is not greater then 40.");
#endif
}
Output :
Yes, MAX is Greater then 40.
#elif 전 처리기 지시문
여러 조건을 검사하는 데 사용
첫 번째 조건이 만족스럽지 않으면 컴파일러는 #else 블록으로 건너 뛰고 다른 조건이 참인지 여부 등을 확인
# if- # elif- # else- # endif 전 처리기 지시문의 구문
#if(condition)
- - - - - - - - - -
- - - - - - - - - -
#elif(condition)
- - - - - - - - - -
- - - - - - - - - -
#elif(condition)
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif
# if- # elif- # else- # endif 전처리 지시문의 예
#include<stdio.h>
#define MKS 65
void main()
{
#if MKS>=78
printf("\nGrade A");
#elif MKS>=50
printf("\nGrade B");
#elif MKS>=25
printf("\nGrade C");
#else
printf("\nGrade D");
#endif
}
Output :
Grade B
#ifdef 전처리 지시문
매크로 이름이 이전에 정의되었는지 여부를 확인하는 데 사용
정의 된 경우 #ifdef와 #else 사이에 제공된 명령문이 실행
#ifdef 전 처리기 지시문의 구문
#ifdef macro-name
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif
#ifdef 전 처리기 지시문의 예
#include<stdio.h>
#define MKS 65
void main()
{
#ifdef MONTH
printf("\nMONTH is defined.");
#else
printf("\nMONTH is not defined.");
#endif
}
Output :
MONTH is not defined.
#ifndef 전처리 지시문
매크로 이름이 이전에 정의되었는지 여부를 확인하는 데 사용되며 정의되지 않은 경우 #ifndef와 #else 사이에 제공된 명령문이 실행
#ifndef 전처리 지시문의 구문
#ifndef macro-name
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif
#ifndef 전 처리기 지시문의 예
#include<stdio.h>
#define MKS 65
void main()
{
#ifndef MAX
printf("\nMAX is not defined.");
#else
printf("\nMAX is defined.");
#endif
}
Output :
MAX is not defined.
#line 전처리 지시문
#line 전처리 지시문은 사전 정의 된 매크로 이름 __LINE__ 및 __FILE__의 기본값을 덮어 쓰는 데 사용
defalut으로 __LINE__은 현재 줄 번호를 표시하고 __FILE__은 현재 파일 이름을 표시
#line 전 처리기 지시어 1의 예 :
#include<stdio.h>
void main()
{
printf("\nLine number %d",__LINE__);
printf("\nFile name %s",__FILE__);
}
Output :
Line number : 4
File name : LineDemo.c
#line 전 처리기 지시어 2의 예 :
#include<stdio.h>
#line 25 Demo.c
void main()
{
printf("\nLine number %d",__LINE__);
printf("\nFile name %s",__FILE__);
}
Output :
Line number : 25
File name : Demo.c
#error 전 처리기 지시문
컴파일러가 컴파일을 중지하고 치명적인 오류를 발생하게 함.
#error 전 처리기 지시어 1의 예 :
#include<stdio.h>
void main()
{
#ifndef MAX
printf("\nMAX is not defined."); //Statement 1
#else
printf("\nMAX is defined."); //Statement 2
#endif
printf("\nEnd of program."); //Statement 3
}
Output :
MAX is not defined.
End of program.
#error 전처리 지시어 2의 예 :
#include<stdio.h>
void main()
{
#ifndef MAX
#error MAX is not defined. //Statement 1
#else
printf("\nMAX is defined."); //Statement 2
#endif
printf("\nEnd of program."); //Statement 3
}
예제 1과 2의 유일한 차이는 문장 하나다.
예제 1에서 문장 1은 메시지를 표시하고 나머지 코드를 계속 컴파일합니다.
예제 2에서, 문장 1은 컴파일러에게 치명적인 에러를 주게하고 나머지 코드의 컴파일을 중단합니다.
댓글 없음:
댓글 쓰기