코린이 c언어 포인터와 함수 질문

asd137485의 이미지

세 변수를 입력받아서 서로 위치를 바꾸는 문제입니다.(num1->num2, num2->num3, num3->num1). 이 문제를 풀다가 발견한건뎅.. 이 수식을 컴파일 하면 자꾸 네 개의 변수를 입력받습니다... 쓰레기값 때문인거 같은데(또 초기화 하면 세 변수만을 입력받더라구용) 어떤 원리 때문에 네 개의 변수를 입력받는 것이죵..?

void swap3(int * str1, int * str2, int * str3 )
{
int temp;
temp = * str3;
* str3 = * str2;
* str2 = * str1;
* str1 = temp;

}

int main(void)
{
int num1, num2, num3;
printf("세 변수에 값을 저장하시오:");
scanf("%d %d %d ", &num1, &num2, &num3);

printf("세 변수 위치 변환 시작\n");
swap3(&num1, &num2, &num3);

printf("세 변수 위치 변환 결과:%d, %d, %d" , num1 , num2, num3);
return 0;
}

라스코니의 이미지

scanf("%d %d %d ", &num1, &num2, &num3);
||
V
scanf("%d %d %d", &num1, &num2, &num3);

asd137485의 이미지

띄어쓰기 때문에 이러한 오류(?)가 왜 생기는건지 원리나 논리를 알 수 있을까요?

익명 사용자의 이미지

scanf의 서식 문자열에 들어가는 공백 문자는 단순한 구분 문자가 아닙니다.

https://en.cppreference.com/w/c/io/fscanf

Quote:
whitespace characters: any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling isspace in a loop). Note that there is no difference between "\n", " ", "\t\t", or other whitespace in the format string.

scanf이 서식 문자열에서 공백 문자를 만날 경우, 표준 입력에서 공백 문자(탭, 스페이스, 개행 문자 등등)를 모두 집어삼켜 버립니다. 공백 문자가 아닌 문자가 들어와야 넘어갈 수 있지요.

따라서, 원래 코드에서 사용자가 숫자 세 개를 입력하고 나면, 엔터를 치든 스페이스를 치든 scanf 서식 문자열 맨 뒤의 공백 문자가 다 집어삼켜 버립니다.

공백 문자가 아닌 문자, 즉 네 번째 숫자가 입력 되어야만 scanf가 반환되는 것이지요.

이 때 네 번째 숫자는 실제로 입력으로 들어온 것이 아니기 때문에, 예컨대 다음에 scanf을 호출하면 그 때 들어오게 됩니다.

asd137485의 이미지

감사합니다! 게속 맘에 찝찝했던 것이 하나 덕분에 해결됐어용 ㅎㅎ. 또한 제가 아직 부족하다고 많이 느껴지네요..ㅜ 따라갈 수 있도록 노력하겠습니다

댓글 달기

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