scanf("%d", &num); 버그?

awdxawdx101의 이미지

#include <stdio.h>
 
int main(void){
	int number = 0;
 
	for(int i=0; i<5; i++){
		printf("input number ? ");
		scanf("%d", &number);
		printf("%d. you input %d\n", i+1, number);
	}
 
	printf("last number = %d\n", number);
 
	return 0;
}

int형이 아닌 char형의 데이터를 넣으면 for문이 바로 종료됩니다.

그 이유가,

-> 잘못된 데이터형이라 입력버퍼에서 빼오는데 실패
-> 남아있음
-> 계속 입력버퍼에서 가져오려고함
-> 실패
-> 계속 반복

이렇게 알고있습니다.
그런데 "a"나 "b"가 아닌 "+"나 "-"를 입력하면 똑같은 char형인데도 for()문을 바로빠져나오지 않고, 다음 입력을 받습니다.

뭔가 부호와 관련있을거같은데, 왜 "+","-"만 저렇게 작동하는지 모르겠습니다.

swish95의 이미지

-10, +10.2 이런값도 허용하기 때문에 +, - 만 입력했을때는 +0 -0 으로 해석하죠..
물론 . 도 포함됩니다.
위의 값들이 입력되면 님이 말한 그대로 동작하는건 side effect 이구요

통상 무결성을 위해 그냥 스트링으로 입력받아서 문자인지 숫자인지 검사하는 부분을 넣고 문자이면 해당 부분을 다시 동작시키거나 하겠죠?
scanf 를 쓸경우는 말그대로 데이타 타입이 명확히 정의된 경우에만 쓰시는게 좋겠네요

------------------------------------------------------------
ProgrammingHolic

awdxawdx101의 이미지

+, -만 입력했을 때 +0,-0 으로 인식한다면, number에는 0이 들어있어야하는거 아닌가요?

#include <stdio.h>
 
int main(void){
	int number = 12345;
 
	for(int i=0; i<5; i++){
		printf("input number ? ");
		scanf("%d", &number);
		printf("%d. you input %d\n", i+1, number);
	}
 
	printf("last number = %d\n", number);
 
	return 0;
}

이렇게 코드를 짜고 +,-를 입력해보면

$ ./test
input number ? +
1. you input 12345
input number ? +
2. you input 12345
input number ? 

0이 아니라 그대로 값이 남아있습니다.

Stephen Kyoungwon Kim@Google의 이미지

swish95의 이미지

side effect 는 시스템마다 다름니다.
님의 시스템은 그렇게 동작하도록 되어 있겠지만

제 컴퓨터에서는

input number ? +
1. you input 12345
input number ? 2. you input 12345
input number ? 3. you input 12345
input number ? 4. you input 12345
input number ? 5. you input 12345
last number = 12345

처럼 a,b 와 동일하게 동작합니다.
가비지 값이나 기타 값들에 대한 동작까지 확인하시려는 거면 해당 문서를 읽는게 빠르겟네요 ^^

쓰고 보니 제 답이 일반론에 불과했네요..
신중하지 못한 답변 드린점 사과드립니다.

------------------------------------------------------------
ProgrammingHolic

awdxawdx101의 이미지

아.. 시스템마다 다른거였군요.
감사합니다.

댓글 달기

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