C언어에서 형이 다른 변수들끼리의 연산에 대해 질문있습니다.

dingkyu의 이미지

C에서 프로그래밍을 할때 다음과 같은 조건으로 연산을 했습니다만,
왜 기대한 대로 결과가 나오지 않는지 도무지 이해를 못해서
질문글 올립니다.

unsigned int A = 100;
unsigned int B = 200;
int C = 10;

if((C + A) - B < 0) {
분기 1 ;
}
else {
분기 2 ;
}

위의 코드대로라면 분명 "분기 1"로 갈꺼 같은데
printf("(C + A) - B = %d\n", (C + A) - B);
로 결과가 - 값이 나오는데도 불구하고 분기 1로는
들어가질 않네요.
이거 왜 그런건지 간단히 설명이나 검색키워드라도
가르쳐주세요. -_ㅠa

auditory의 이미지

unsigned int와 int 간의 연산에서는

연산전에 int가 unsigned int로 conversion됩니다.

Arithmetic conversion을 찾아보시면 되겠습니다.

unsigned int t = (C+(unsigned int)A)-B;

if (t<0) 인가를 테스트하므로 당연히

unsigned int t는 0보다 크겠지요..

semmal의 이미지

컨버전은 문법책을 꼼꼼히 읽어보면 나오는 부분이기는 하지만, 읽었다고 하더라도 실제로 저런 문제에 부딪히기 전까지는 잘 알 수 없는 부분이기도 합니다.

저 부분이 어렵다고 생각한다면, 모든 계산을 long int나 double로 변환한 다음, 계산을 수행하는 버릇을 들이면 굳이 큰 고민없이도 프로그래밍 할 수 있습니다. 물론, 가능한 값이 long int나 double의 범위를 벗어나지 않는지는 확실히 인지하고 있어야 합니다.

하지만 하드웨어에 의존적인 프로그래밍을 하실 거라면, 저런 부분은 허투루 넘겨서는 안됩니다. 문법책과 컴파일러 메뉴얼, 심지어는 C언어 스팩까지도 꼼꼼히 읽으셔야 합니다.
------------------------------
How many legs does a dog have?

------------------------------
How many legs does a dog have?

dingkyu의 이미지

auditory님, semmal님 답변 감사드립니다.
덕분에 명확하게 알게됬습니다.

(-_-) (_ _) (^_^)꾸벅..

고민이 많아 고민인 애늙은이 입니다.

netionics의 이미지

unsigned의 modulo 연산을 이용하는거나 비트 단위 연산이 필요한게 아니라면

무조건 signed int로 하는게 좋습니다.

양수값만 들어간다고해도 signed로 문제가 없다면 signed로 하는게 좋죠

:)

댓글 달기

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