반응형

다음 소스에 대한 질문에 답하라.


1
2
3
4
5
6
7
8
9
10
11
double power(int x, int y)
{
    double result=1.0;
    int i;
    for(i=0;i<y;i++)
    {
        printf("result=%f\n", result);
        result*=x;
    }
    return result;
}
cs

(a)전처리기 지시자 #ifdef를 사용하여 DEBUG가 정의되어 있는 경우에만 화면 출력이 나오도록 하라.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define DEBUG  
double power(int x, int y)
{
    double result=1.0;
    int i;
    for(i=0;i<y;i++)
    {
#ifdef DEBUG // #if defined(DEBUG)
        printf("result=%f\n", result);
#endif
        result*=x;
    }
    return result;
}
cs

(b)#if를 사용하여 DEBUG가 2일 경우에만 화면 출력이 나오도로 수정하라.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define DEBUG 2
double power(int x, int y)
{
    double result=1.0;
    int i;
    for(i=0;i<y;i++)
    {
#if (DEBUG ==2)
        printf("result=%f\n", result);
#endif
        result*=x;
    }
    return result;
}
cs
(c)#if를 사용하여 DEBUG가 2이고 LEVEL이 3인 경우에만 화면 출력이 나오도록 수정하라.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define DEBUG 2
#define LEVEL 3
double power(int x, int y)
{
    double result=1.0;
    int i;
    for(i=0;i<y;i++)
    {
#if ((DEBUG ==2)&&(LEVEL==3))
        printf("result=%f\n", result);
#endif
        result*=x;
    }
    return result;
}
cs
(d)문장 1을 수정하여 소스파일에서 현재의 행 번호가 함께 출력되도록 하라.

1
2
3
4
5
6
7
8
9
10
11
double power(int x, int y)
{
    double result=1.0;
    int i;
    for(i=0;i<y;i++)
    {
        printf("result=%f line number=%d\n", result,__LINE__);  //1
        result*=x;
    }
    return result;
}
cs
(e)POWER_TYPE이라는 매크로를 정의하여 POWER_TYPE이 0이면 결과값을 int형으로 반환하도록 하고 POWER_TYPE이 1이면 결과값을 double형으로 
반환하도록 하라.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define POWER_TYPE 1
 
double power(int x, int y)
{
    int i;
#if(POWER_TYPE==0)
    int result=1;
    for(i=0;i<y;i++)
    {
        printf("result=%d\n", result);
        result*=x;
    }
#elif(POWER_TYPE==1)
    double result=1.0;
    for(i=0;i<y;i++)
    {
        printf("result=%f\n", result);
        result*=x;
    }
#endif
    return result;
}
 
cs


(f)#if를 이용하여 문장 1을 주석처리하여 보라.


1
2
3
4
5
6
7
8
9
10
11
12
13
double power(int x, int y)
{
    double result=1.0;
    int i;
    for(i=0;i<y;i++)
    {
#if 0
        printf("result=%f\n", result);
#endif
        result*=x;
    }
    return result;
}
cs


반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C언어' 카테고리의 다른 글

배열 원소 일괄 초기화  (0) 2017.01.14
3개 정수 비교  (0) 2017.01.14
생명 게임(game of life - John H. Conway)  (0) 2017.01.13
2차원 배열 복사  (0) 2017.01.06
디지털 영상  (0) 2017.01.06

+ Recent posts