c언어 관련 질문

linux_chobo의 이미지

원의 중심점과 원의 반지름을 입력받아서 구조체와 함수를 활용해 원의 면적과 원의 둘레를 출력하는 코드 입니다.

#include <stdio.h>
 
struct point
{
        int x,y;
};
 
struct circle
{
        struct point center;
        double radius;
};
 
double area(struct circle c);
double perimeter(struct circle c);
 
int main(void)
{
        struct point center;
        struct circle s;
 
        printf(" 원의 중심점: ");
        scanf("%d %d", &center.x, &center.y);
 
        printf("원의 반지름: ");
        scanf("%f", &s.radius);
 
        printf("원의 면적=%f, 원의 둘레=%f\n", area(s), perimeter(s));
 
        return 0;
}
 
double area(struct circle c)
{
        return 3.14 * c.radius * c.radius;
}
 
double perimeter(struct circle c)
{
        return 6.28 * c.radius;
}

이렇게 코드를 썻는데, 결과가 이상하게 나옵니다...,, 함수쪽에 뭔가 문제가 있는거 같은데 어디를 틀리게 쓴건가요?
File attachments: 
첨부파일 크기
Image icon putty_ktaNTKLhih.png3.31 KB
세벌의 이미지

컴파일 할 때 에러 뿐 아니라 경고 메시지까지 잘 보시면 답을 찾는데 도움될 겁니다.
scanf 함수가 C언어 처음 배울 때 부터 나오는 함수이지만 의외로 어렵습니다.

라스코니의 이미지

debug를 해 보세요. line by line으로 실행해서 변수에 값이 적절하게 들어가는지 확인해 보세요.

sheld2의 이미지

radius가 제대로 받아졌는지 확인해보세요.
디버깅 또는...

printf("%f\n", s.radius);

익명_사용자2의 이미지

첨부하신 스샷을 보니, 어느 운영체제,Compiler로 컴파일하시는지 모르겠네요. 혹시, 리눅스와 GCC라면, 컴파일 할 때, -Wall을 추가해주세요. 왜 동작을 안하는지 나올것 입니다.

$ gcc test.c -Wall
test.c:24:17: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
....

warning메세지에 뭘 어떻게 해야하는지도 나와있지만, 직접 해보시라고 정답을 생략합니다.

익명 사용자의 이미지

printf(" 원의 중심점: ");
scanf("%d %d", &center.x, &center.y);

여기가 잘못된거 같네요

linux_chobo의 이미지

메인함수에서 struct point center;로 다시 정의해버려서 문제가 일어난거 같습니다.

저 부분을 지우고 scanf("%d %d", &center.x, &center.y);를 scanf("%d %d", &s.center.x, &s.center.y);로 바꿔서 해결했습니다.

세벌의 이미지

struct point center 가 문제가 아니고요.
사실 원둘레와 면적 구하는데 중심은 필요 없죠. 반지름만 있으면 됩니다.
제가 이리저리 고친 소스 참고하셔요.

#include <stdio.h>
 
struct point
{
        double x,y;
};
 
struct circle
{
        struct point center;
        double radius;
};
 
double area(struct circle c);
double perimeter(struct circle c);
 
int main(void)
{
        struct point center;
        struct circle s;
 
        printf(" 원의 중심점: ");
        scanf("%lf %lf", &center.x, &center.y);
        printf("%f %f", center.x, center.y);
 
        printf("원의 반지름: ");
        scanf("%lf", &s.radius);
        printf("%f", s.radius);
 
        printf("원의 면적=%f, 원의 둘레=%f\n", area(s), perimeter(s));
 
        return 0;
}
 
double area(struct circle c)
{
        return 3.14 * c.radius * c.radius;
}
 
double perimeter(struct circle c)
{
        return 6.28 * c.radius;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.