매우 간단한건데요.. 이거 왜 이러는건가요?

yangam의 이미지

/*-----------------------------------------------------------------
 <Euclid Algorithm - Third>

 1. 임의의 두 정수 m, n 을 입력받는다.
 
 2. n 이 0 이라면 m 이 최대공약수이다.
    n 이 0 이 아니라면 m 에 m % n 을 대입한 후, 두 값을 교환한다.
	(함수에 전달할 때 값이 바뀌게끔 전달한다)
 
 3. 2 번으로 돌아간다.

 (c) Sung-Am Yang, 2005-02-08
 -----------------------------------------------------------------*/

#include <stdio.h>

int euclid_3rd(int m, int n);

int main()
{
	int m = 384, n = 30;

	printf("%d 와 %d 의 최대공약수(Greatest Common Divisor)는 %d 입니다.\n",
		m, n, euclid_3rd(m, n));

	return 0;
}

int euclid_3rd(int m, int n)
{
	if (n == 0) {
		printf("최대공약수는 %d 이다.\n", m);
		return m;
	}
	else
		euclid_3rd(n, m % n);
}

<결과>
최대공약수는 6이다.
384와 30 의 최대공약수(Greatest Common Divisor)는 863865 입니다.

GCC 3.4.3 으로 실행한 결과입니다.
Visual C++ 6.0 으로 실행했을 때는 잘 나옵니다.
(그런데 이런 경고가 뜹니다.. warning C4715: 'euclid_3rd' : not all control paths return a value)

저 소스에서 어떤 부분을 고치면 GCC 에서도 제대로 된 결과가 나올 수 있을까요?

Alpha의 이미지

int euclid_3rd(int m, int n)
{
   if (n == 0) {
      printf("최대공약수는 %d 이다.\n", m);
      return m;
   }
   else
      return euclid_3rd(n, m % n);
} 

return을 빠트리신 것 같네요.

tinywolf의 이미지

혹은..

int euclid_3rd(int m, int n) 
{ 
   if (n == 0) { 
      printf("최대공약수는 %d 이다.\n", m); 
      return m; 
   } 

   return euclid_3rd(n, m % n); 
} 

int형 리턴이 있어야 하는데 경우에 따라 리턴을 하지 않는 경우가 생기므로 나오는 경고입니다..

경고 하나하나 확인하는 것 중요하지요..

나중엔 에러를 낳으니까요.. ㅎㅎ

제목이 뭔지 모르게 흥미로워서 눌러보게 되는군요.. :wink:

ㅡ_ㅡ;

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.