visual studio 2010에서 int 형과 unsigned int 형은 서로 비교할 수 없는 것인가요?

high385의 이미지

unsigned int vaR = 1;
int off1 = 0;
for (off1 = -2;off1<=2*vaR; ++off1){ //for 문 안으로 안들어감
printf("%d\n", vaR);
}

위의 문장에서 for문 안으로 들어가지 않는데요.

unsigned int vaR = 1; 대신에 int vaR = 1; 로 설정하면 잘 동작하는데요.

왜 unsigned int vaR = 1; 일 때 for문이 제대로 동작하지 않는지 궁금합니다.

도와주세요..

익명 사용자의 이미지

for 문 안의 비교 구문에서
unsigned int와 int는 rank가 같으므로
usual arithmetic conversions 규칙에 의해
unsigned int로 변환되어 비교하게 됩니다.

unsigned형과 int형이 섞인 계산은 형의 변화를 주의깊게 고려해야 합니다.
어떤 분은 아예 unsigned형을 쓰지 말자고도 하던데
비트연산 같은 몇몇 연산의 경우에는 unsigned형이 더 엄밀하게 잘 정의되어 있으므로
그러기도 쉽지가 않습니다.

C프로그래머라면
integer promotion, default argument promotion, usual arithmetic conversions
이 세가지 정도는 알고 있어야 정상입니다.

gilgil의 이미지

-2라는 off1의 값은, unsigned의 입장에서는 INT_MAX-2(열라 큰 값)로 해석되어 질 수도 있습니다.
for 문 안의 "off1<=2*vaR" 비교문에서는 윗분이 말씀하신 대로 unsigned로 해석을 하게 되면, 좌항이 크니까 조건이 false가 되어 버리죠.

shint의 이미지


0과 1을 기록하는 범위가 다를겁니다. 그래서. 0과 1에 위치가 다릅니다.

계산기에서 공학용으로 확인해보세요. dec bin - byte

int
-5 -4 -3 -2 -1 0 1 2 3 4 5

5 00000101
4 00000100
3 00000011
2 00000010
1 00000001
0 00000000
-1 11111111
-2 11111110
-3 11111101
-4 11111100
-5 11111011

unsigned int
0 1 2 3 4 5 6 7 8 9

9 00001001
8 00001000
7 00000111
6 00000110
5 00000101
4 00000100
3 00000011
2 00000010
1 00000001
0 00000000

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

댓글 달기

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