상세 컨텐츠

본문 제목

[c언어] 코딩도장 79.8 정적 변수 선언하기 문제풀이

c언어

by ~지우~ 2021. 7. 19. 22:51

본문

728x90

오늘은 코딩도장 79.8 심사문제 풀이를 해보겠습니다.

<문제> 표준 입력으로 정수 한 개가 입력됩니다. 다음 소스 코드를 완성하여 입력된 정수만큼 2차원 좌표가 이동하게 만드세요. 최초 좌표(x, y)는 10, 20입니다.

 

main함수에서 더하는 값이 누적되려면 구조체를 static 변수로 선언하면 됩니다.

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct Point2D {
	int x, y;
};

struct Point2D movePoint(int offset)
{
    static struct Point2D p = { 10, 20 };
	p.x += offset;
	p.y += offset;

    return p;
}

int main()
{
    int num1;
    struct Point2D p;

    scanf("%d", &num1);

    p = movePoint(num1);
    printf("%d %d\n", p.x, p.y);

    p = movePoint(num1);
    printf("%d %d\n", p.x, p.y);

    p = movePoint(num1);
    printf("%d %d\n", p.x, p.y);

    return 0;
}
728x90

관련글 더보기

댓글 영역