if( (5<x<7) && (7<y<10 ))

GjtRoql의 이미지

#include <stdio.h>

int main()
{
    int x , y ;
    x=y=0;
    if( (5<x<7) && (7<y<10 )) {
    }
    return 0;
}

이코드를
gcc -Wall test.c

로 실행했는데 아무 이상이 없네요!

if( (5<x<7) && (7<y<10 ))
이 구문이 왜 에러가 발생 안하죠?
원래되는건지... 제가 잘못 배운건가!

kslee80의 이미지

에러는 날 거 같지 않네요.
단지, 의도한대로 동작하지는 않을 듯 싶군요..

doldori의 이미지

5<x<7은 (5<x)<7로 해석됩니다.
(5<x)의 값은 0 아니면 1이 되고 따라서 최종 결과는 항상 1입니다.
의도하지 않았을 가능성이 높지만 적법한 식이죠.

GjtRoql의 이미지

감사합니다.
아직 수양이 부족한거 같군요 ^^

--------------
Burning Blue!
--------------

binul의 이미지

똑같은 식을 자바에서 하면은 에러 날겁니다.
bool 이란 형이 만들어져 있으며 bool 과 int와의 대소 비교 연산자가 없기 때문이죠.

c와 c++(c와의 호환)에서는 int형으로 bool을 표현하기 때문에
int vs int의 대소 비교가 되어버리는...

--------------------------------
그래날아보자꾸나

차리서의 이미지

binul wrote:
똑같은 식을 자바에서 하면은 에러 날겁니다.
bool 이란 형이 만들어져 있으며 bool 과 int와의 대소 비교 연산자가 없기 때문이죠.

c와 c++(c와의 호환)에서는 int형으로 bool을 표현하기 때문에
int vs int의 대소 비교가 되어버리는...


똑같은 식을 python에서 하면 의도한 대로 동작합니다. :)
>>> def tran(x, y):
...     if 5 < x < 7 and 7 < y < 10:
...         print "OK"
...     else:
...         print "Out of range"
...
>>> tran(6, 8)
OK
>>> tran(6, 11)
Out of range
>>> tran(3, 9)
Out of range
>>> tran(10, 2)
Out of range
>>>

--
자본주의, 자유민주주의 사회에서는 결국 자유마저 돈으로 사야하나보다.
사줄테니 제발 팔기나 해다오. 아직 내가 "사겠다"고 말하는 동안에 말이다!

cppig1995의 이미지

binul wrote:
똑같은 식을 자바에서 하면은 에러 날겁니다.
bool 이란 형이 만들어져 있으며 bool 과 int와의 대소 비교 연산자가 없기 때문이죠.

c와 c++(c와의 호환)에서는 int형으로 bool을 표현하기 때문에
int vs int의 대소 비교가 되어버리는...

C 는 _Bool ('bool' with <stdbool.h>) 형으로 bool 형을 표현할 수는 있지만 연산자 반환 값으로는 여전히 int 를 사용합니다.
C++ 은 bool 형으로 bool 형을 표현하지만 bool 에서 int 로의 변환이 정의 (Integral Promotion) 되어 있습니다.

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

댓글 달기

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